half working 'daemon mode'

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-12 16:21:47 -05:00
parent 54c0947452
commit 2cf02ee2e5
6 changed files with 59 additions and 34 deletions

View File

@ -4,12 +4,8 @@
package log
import (
origlog "log"
)
func Error(err error, a ...any) {
if ! ERROR.Get() { return }
origlog.Println("Error:", err)
origlog.Println(a...)
realPrintln("Error:", err)
realPrintln(a...)
}

10
log.go
View File

@ -4,10 +4,6 @@
package log
import (
origlog "log"
)
/*
a simple way turn logging messages on and off. The gui config
@ -29,16 +25,16 @@ 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...)
origlog.Println(a...)
realPrintln(a...)
return
}
if ! f.Get() { return }
a = append([]any{f.short}, a...)
origlog.Println(a...)
realPrintln(a...)
}
func Logf(f *LogFlag, s string, a ...any) {
if ! f.Get() { return }
s = f.short + " " + s
origlog.Printf(s, a...)
realPrintf(s, a...)
}

View File

@ -49,31 +49,26 @@ package log
func (l *Logger) Writer() io.Writer
*/
import (
origlog "log"
)
func Println(a ...any) {
if ! PRINTLN.Ok() { return }
if ! PRINTLN.b { return }
origlog.Println(a...)
realPrintln(a...)
}
func Printf(s string, a ...any) {
if ! PRINTLN.Ok() { return }
if ! PRINTLN.b { return }
origlog.Printf(s, a...)
realPrintf(s, a...)
}
func Fatalln(a ...any) {
origlog.Fatalln(a...)
realFatalln(a...)
}
func Fatalf(s string, a ...any) {
origlog.Fatalf(s, a...)
realFatalf(s, a...)
}
func Fatal(s string, a ...any) {
origlog.Fatalf(s, a...)
realFatalf(s, a...)
}

43
reallog.go Normal file
View File

@ -0,0 +1,43 @@
package log
// implements 'daemon' mode so switches to fmt
// instead of log so that timestamps are not printed twice
import (
"fmt"
reallog "log"
)
var daemonMode bool = false
func DaemonMode(b bool) {
daemonMode = b
}
func realPrintln(a ...any) {
if daemonMode {
fmt.Println(a...)
} else {
reallog.Println(a...)
}
}
func realPrintf(s string, a ...any) {
if daemonMode {
fmt.Printf(s, a...)
} else {
reallog.Printf(s, a...)
}
}
func realFatalln(a ...any) {
reallog.Fatalln(a...)
}
func realFatalf(s string, a ...any) {
reallog.Fatalf(s, a...)
}
func realFatal(s string, a ...any) {
reallog.Fatalf(s, a...)
}

11
spew.go
View File

@ -5,7 +5,6 @@
package log
import (
origlog "log"
"go.wit.com/dev/davecgh/spew"
)
@ -18,19 +17,19 @@ func Spew(b any, a ...any) {
if ! b.(bool) {
return
}
origlog.Println("SPEW:", spew.Sdump(a...))
realPrintln("SPEW:", spew.Sdump(a...))
case LogFlag:
var f LogFlag
f = b.(LogFlag)
if ! f.b {
return
}
origlog.Println("SPEW:", spew.Sdump(a...))
realPrintln("SPEW:", spew.Sdump(a...))
default:
origlog.Println("SPEW b:", spew.Sdump(b))
origlog.Println("SPEW a:", spew.Sdump(a...))
realPrintln("SPEW b:", spew.Sdump(b))
realPrintln("SPEW a:", spew.Sdump(a...))
}
// origlog.Println("SPEW:", spew.Sdump(a...))
// realPrintln("SPEW:", spew.Sdump(a...))
/*
scs := spew.ConfigState{Indent: "\t", MaxDepth: 1}
// Output using the ConfigState instance.

View File

@ -4,12 +4,8 @@
package log
import (
origlog "log"
)
func Warn(a ...any) {
if ! WARN.Ok() { return }
if ! WARN.b { return }
origlog.Println(a...)
realPrintln(a...)
}