Compare commits

..

6 Commits

Author SHA1 Message Date
Jeff Carr 854796ba72 add On() & Off() also Fprintln() & Fprintf() 2025-09-06 15:23:22 -05:00
Jeff Carr 8c7078908b more compat for 'fmt' package 2025-08-16 21:50:21 -05:00
Your Name 466a3c6076 tracking down all erroneous log messages 2024-01-01 12:00:00 -06:00
Jeff Carr c1c1b8a75e also add Enable() 2025-02-05 07:25:08 -06:00
Jeff Carr f793a05288 switch to Disabled() 2025-02-05 06:28:51 -06:00
Jeff Carr 1631c72f70 don't use STDERR anymore. also timestamps off by default
Signed-off-by: Jeff Carr <jcarr@wit.com>
2025-01-09 20:42:18 -06:00
11 changed files with 92 additions and 46 deletions

View File

@ -1,6 +1,6 @@
# git remote add github git@github.com:wit-go/log.git
all: vet
all: goimport vet
@#GO111MODULE=off go vet -x
@echo this go library builds ok

12
bool.go
View File

@ -1,12 +0,0 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package log
func Bool(b bool, a ...any) {
if !b {
return
}
realPrintln(a...)
}

View File

@ -4,10 +4,16 @@
package log
import "fmt"
func Error(err error, a ...any) {
if !ERROR.Get() {
if ERROR.Disabled() {
return
}
realPrintln("Error:", err)
realPrintln(a...)
}
func Errorf(f string, a ...any) error {
return fmt.Errorf(f, a...)
}

View File

@ -30,6 +30,8 @@ import (
"sync"
)
var off bool // completely shut up until I tell you to talk again
var INFO *LogFlag // toggles log.Info()
var VERBOSE *LogFlag // toggles log.Verbose()
var SPEW *LogFlag // toggles log.Spew()
@ -56,6 +58,7 @@ type LogFlag struct {
var flags []*LogFlag
var daemonMode bool
var timestamps bool = false
var httpMode http.ResponseWriter
func init() {
@ -141,17 +144,18 @@ func ProcessFlags(callback func(*LogFlag)) {
}
// probably a better name than Get()
// switch to this
func (f *LogFlag) Bool() bool {
// is the output flag disabled?
// if so, don't print anything!
func (f *LogFlag) Disabled() bool {
if !f.Ok() {
return false
return true
}
return f.b
return !f.b
}
// returns the value of the flag
func (f *LogFlag) Get() bool {
// just the opposite of Disabled()
// both are here just for code readability
func (f *LogFlag) Enabled() bool {
if !f.Ok() {
return false
}

10
info.go
View File

@ -5,20 +5,14 @@
package log
func Info(a ...any) {
if !INFO.Ok() {
return
}
if !INFO.b {
if INFO.Disabled() {
return
}
realPrintln(a...)
}
func Infof(s string, a ...any) {
if !INFO.Ok() {
return
}
if !INFO.b {
if INFO.Disabled() {
return
}
realPrintf(s, a...)

6
log.go
View File

@ -24,11 +24,11 @@ In your package, register NETWARN:
func Log(f *LogFlag, a ...any) {
if !f.Ok() {
// if the flag is NULL, notify the user they didn't initialize the flag
a = append([]any{"FLAG = NULL"}, a...)
a = append([]any{"FLAG = NULL. Normal error output. please ignore for now"}, a...)
realPrintln(a...)
return
}
if !f.Get() {
if f.Disabled() {
return
}
a = append([]any{f.short}, a...)
@ -36,7 +36,7 @@ func Log(f *LogFlag, a ...any) {
}
func Logf(f *LogFlag, s string, a ...any) {
if !f.Get() {
if f.Disabled() {
return
}
s = f.short + " " + s

View File

@ -1,5 +1,7 @@
package log
import "io"
/*
import (
"log"
@ -81,6 +83,14 @@ func Sprintln(a ...any) string {
return realSprintln(a...)
}
func Fprintln(w io.Writer, a ...any) (int, error) {
return realFprintln(w, a...)
}
func Fprintf(w io.Writer, s string, a ...any) (int, error) {
return realFprintf(w, s, a...)
}
func Fatalln(a ...any) {
realFatalln(a...)
}

View File

@ -11,10 +11,22 @@ import (
"time"
)
func Off() {
off = true
}
func On() {
off = false
}
func DaemonMode(b bool) {
daemonMode = b
}
func Timestamps(b bool) {
timestamps = b
}
var captureMode io.Writer
func CaptureMode(w io.Writer) {
@ -49,6 +61,9 @@ func DaemonShow() bool {
}
func realPrintln(a ...any) {
if off {
return
}
if daemonMode {
// in daemon mode, don't put timestamps on each line
if captureMode == nil {
@ -59,15 +74,22 @@ func realPrintln(a ...any) {
} else {
// put timestamps on each line
if captureMode == nil {
if timestamps {
reallog.Println(a...)
} else {
fmt.Println(a...)
}
} else {
// TODO: add datestamp
fmt.Fprintln(captureMode, a...)
}
}
if httpMode != nil {
var timestamp string
if timestamps {
now := time.Now()
timestamp := now.Format("2006/01/02 15:04:05") // bummer. other date doesn't work?
timestamp = now.Format("2006/01/02 15:04:05") // todo: fix GO so Nov 5 1955 works here
}
s := timestamp + " " + fmt.Sprint(a...)
fmt.Fprintln(httpMode, s)
if flusher != nil {
@ -77,6 +99,9 @@ func realPrintln(a ...any) {
}
func realPrintf(s string, a ...any) {
if off {
return
}
if daemonMode {
// in daemon mode, don't put timestamps on each line
if captureMode == nil {
@ -87,14 +112,21 @@ func realPrintf(s string, a ...any) {
} else {
// put timestamps on each line
if captureMode == nil {
if timestamps {
reallog.Printf(s, a...)
} else {
fmt.Printf(s, a...)
}
} else {
fmt.Fprintf(captureMode, s, a...)
}
}
if httpMode != nil {
var timestamp string
if timestamps {
now := time.Now()
timestamp := now.Format("2006/01/02 15:04:05") // bummer. other date doesn't work?
timestamp = now.Format("2006/01/02 15:04:05")
}
s := timestamp + " " + fmt.Sprintf(s, a...)
fmt.Fprintln(httpMode, s)
if flusher != nil {
@ -115,6 +147,14 @@ func realSprintln(a ...any) string {
return fmt.Sprintln(a...)
}
func realFprintln(w io.Writer, a ...any) (int, error) {
return fmt.Fprintln(w, a...)
}
func realFprintf(w io.Writer, s string, a ...any) (int, error) {
return fmt.Fprintf(w, s, a...)
}
func realFatalln(a ...any) {
reallog.Fatalln(a...)
}

10
sscan.go Normal file
View File

@ -0,0 +1,10 @@
package log
import "fmt"
// func Sprint(a ...any) string {
// return realSprint(a...)
func Sscanf(f string, thing string, a ...any) (int, error) {
return fmt.Sscanf(f, thing, a...)
}

View File

@ -5,10 +5,7 @@
package log
func Verbose(a ...any) {
if !VERBOSE.Ok() {
return
}
if !VERBOSE.b {
if VERBOSE.Disabled() {
return
}
realPrintln(a...)

View File

@ -5,10 +5,7 @@
package log
func Warn(a ...any) {
if !WARN.Ok() {
return
}
if !WARN.b {
if WARN.Disabled() {
return
}
realPrintln(a...)