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 # git remote add github git@github.com:wit-go/log.git
all: vet all: goimport vet
@#GO111MODULE=off go vet -x @#GO111MODULE=off go vet -x
@echo this go library builds ok @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 package log
import "fmt"
func Error(err error, a ...any) { func Error(err error, a ...any) {
if !ERROR.Get() { if ERROR.Disabled() {
return return
} }
realPrintln("Error:", err) realPrintln("Error:", err)
realPrintln(a...) realPrintln(a...)
} }
func Errorf(f string, a ...any) error {
return fmt.Errorf(f, a...)
}

View File

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

10
info.go
View File

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

6
log.go
View File

@ -24,11 +24,11 @@ In your package, register NETWARN:
func Log(f *LogFlag, a ...any) { func Log(f *LogFlag, a ...any) {
if !f.Ok() { if !f.Ok() {
// if the flag is NULL, notify the user they didn't initialize the flag // 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...) realPrintln(a...)
return return
} }
if !f.Get() { if f.Disabled() {
return return
} }
a = append([]any{f.short}, a...) a = append([]any{f.short}, a...)
@ -36,7 +36,7 @@ func Log(f *LogFlag, a ...any) {
} }
func Logf(f *LogFlag, s string, a ...any) { func Logf(f *LogFlag, s string, a ...any) {
if !f.Get() { if f.Disabled() {
return return
} }
s = f.short + " " + s s = f.short + " " + s

View File

@ -1,5 +1,7 @@
package log package log
import "io"
/* /*
import ( import (
"log" "log"
@ -81,6 +83,14 @@ func Sprintln(a ...any) string {
return realSprintln(a...) 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) { func Fatalln(a ...any) {
realFatalln(a...) realFatalln(a...)
} }

View File

@ -11,10 +11,22 @@ import (
"time" "time"
) )
func Off() {
off = true
}
func On() {
off = false
}
func DaemonMode(b bool) { func DaemonMode(b bool) {
daemonMode = b daemonMode = b
} }
func Timestamps(b bool) {
timestamps = b
}
var captureMode io.Writer var captureMode io.Writer
func CaptureMode(w io.Writer) { func CaptureMode(w io.Writer) {
@ -49,6 +61,9 @@ func DaemonShow() bool {
} }
func realPrintln(a ...any) { func realPrintln(a ...any) {
if off {
return
}
if daemonMode { if daemonMode {
// in daemon mode, don't put timestamps on each line // in daemon mode, don't put timestamps on each line
if captureMode == nil { if captureMode == nil {
@ -59,15 +74,22 @@ func realPrintln(a ...any) {
} else { } else {
// put timestamps on each line // put timestamps on each line
if captureMode == nil { if captureMode == nil {
if timestamps {
reallog.Println(a...) reallog.Println(a...)
} else {
fmt.Println(a...)
}
} else { } else {
// TODO: add datestamp // TODO: add datestamp
fmt.Fprintln(captureMode, a...) fmt.Fprintln(captureMode, a...)
} }
} }
if httpMode != nil { if httpMode != nil {
var timestamp string
if timestamps {
now := time.Now() 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...) s := timestamp + " " + fmt.Sprint(a...)
fmt.Fprintln(httpMode, s) fmt.Fprintln(httpMode, s)
if flusher != nil { if flusher != nil {
@ -77,6 +99,9 @@ func realPrintln(a ...any) {
} }
func realPrintf(s string, a ...any) { func realPrintf(s string, a ...any) {
if off {
return
}
if daemonMode { if daemonMode {
// in daemon mode, don't put timestamps on each line // in daemon mode, don't put timestamps on each line
if captureMode == nil { if captureMode == nil {
@ -87,14 +112,21 @@ func realPrintf(s string, a ...any) {
} else { } else {
// put timestamps on each line // put timestamps on each line
if captureMode == nil { if captureMode == nil {
if timestamps {
reallog.Printf(s, a...) reallog.Printf(s, a...)
} else {
fmt.Printf(s, a...)
}
} else { } else {
fmt.Fprintf(captureMode, s, a...) fmt.Fprintf(captureMode, s, a...)
} }
} }
if httpMode != nil { if httpMode != nil {
var timestamp string
if timestamps {
now := time.Now() 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...) s := timestamp + " " + fmt.Sprintf(s, a...)
fmt.Fprintln(httpMode, s) fmt.Fprintln(httpMode, s)
if flusher != nil { if flusher != nil {
@ -115,6 +147,14 @@ func realSprintln(a ...any) string {
return fmt.Sprintln(a...) 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) { func realFatalln(a ...any) {
reallog.Fatalln(a...) 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 package log
func Verbose(a ...any) { func Verbose(a ...any) {
if !VERBOSE.Ok() { if VERBOSE.Disabled() {
return
}
if !VERBOSE.b {
return return
} }
realPrintln(a...) realPrintln(a...)

View File

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