From 4f8752d9f4e210c48006217f059658049f6d682e Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 9 Jan 2024 15:50:53 -0600 Subject: [PATCH] go.wit.com/log flag changes Signed-off-by: Jeff Carr --- .gitignore | 1 + args.go | 33 --------- error.go | 2 +- flags.go | 189 +++++++++++++++++++++++++++++++++------------------- go.mod | 5 -- go.sum | 2 - info.go | 6 +- log.go | 48 ++++--------- original.go | 36 +++++++++- spew.go | 5 +- verbose.go | 3 +- warn.go | 3 +- 12 files changed, 180 insertions(+), 153 deletions(-) create mode 100644 .gitignore delete mode 100644 args.go delete mode 100644 go.mod delete mode 100644 go.sum diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/args.go b/args.go deleted file mode 100644 index 4a452d3..0000000 --- a/args.go +++ /dev/null @@ -1,33 +0,0 @@ -package log - -/* -// disable this until it hopefully is adopted by the go lang developers -// TODO: clean alex's dependancies - -import ( - arg "github.com/alexflint/go-arg" -) - -// -// Attempt to switch logging to syslog on linux -// - -var argLog ArgLog - -// This struct can be used with the go-arg package -type ArgLog struct { - LogDebug bool `arg:"--log-debug" help:"show the log gui"` - LogTmp bool `arg:"--log-tmp" help:"send all output /tmp"` - LogStdout bool `arg:"--log-stdout" help:"send all output to STDOUT"` - LogQuiet bool `arg:"--log-quiet" help:"suppress all output"` -} - -func init() { - arg.Register(&argLog) -} - -// returns true if --log-debug was passed from the command line -func ArgDebug() bool { - return argLog.LogDebug -} -*/ diff --git a/error.go b/error.go index 6db5849..4e9d7ea 100644 --- a/error.go +++ b/error.go @@ -5,7 +5,7 @@ import ( ) func Error(err error, a ...any) { - if ! ERROR.B { return } + if ! ERROR.Get() { return } origlog.Println("Error:", err) origlog.Println(a...) } diff --git a/flags.go b/flags.go index 9a54a81..49cdc28 100644 --- a/flags.go +++ b/flags.go @@ -4,77 +4,79 @@ package log Handles the on/off flags for things like log.Info() and log.Warn() */ +/* + The original log flags: + + log.Ldate: The date in the local time zone: YYYY/MM/DD. + log.Ltime: The time in the local time zone: HH:MM:SS. + log.Lmicroseconds: Microsecond resolution: HH:MM:SS.microseconds. + log.Llongfile: Full file name and line number: /a/b/c/d.go:23. + log.Lshortfile: Final file name element and line number: d.go:23. + log.LUTC: If Ldate or Ltime is set, use UTC rather than the local time zone. + log.Lmsgprefix: Move the "prefix" from the beginning of the line to before the message. + log.LstdFlags: Initial values for the standard logger (Ldate | Ltime). + + can be set this way: + myLogger.SetFlags(log.Ldate | log.Ltime) + +*/ + + + import ( "sync" ) -var INFO LogFlag -var VERBOSE LogFlag -var SPEW LogFlag -var WARN LogFlag -var ERROR LogFlag -var PRINTLN LogFlag +var INFO *LogFlag // toggles log.Info() +var VERBOSE *LogFlag // toggles log.Verbose() +var SPEW *LogFlag // toggles log.Spew() + +var WARN *LogFlag // toggles log.Warn() (true by default) +var ERROR *LogFlag // toggles log.Warn() (true by default) +var PRINTLN *LogFlag // toggles log.Println() (true by default) + +var always *LogFlag // writeMutex protects locks the write process var flagsMutex sync.Mutex type LogFlag struct { - B bool - Default bool // set at the time of Registration() - Name string + b bool + orig bool // used as the Default value. set at the time of Registration() + name string // TODO: figure out what package is sending the Registration - Subsystem string // probably should just be forced to be the package name - Short string // string actually printed on each line - Desc string + subsystem string // probably should just be forced to be the package name + short string // string actually printed on each line + desc string } var flags []*LogFlag func init() { - INFO.B = false - INFO.Name = "INFO" - INFO.Subsystem = "log" - INFO.Desc = "Enable log.Info()" - INFO.Register() + full := "go.wit.com/log" + short := "log" - SPEW.B = false - SPEW.Name = "SPEW" - SPEW.Subsystem = "log" - SPEW.Desc = "Enable log.Spew()" - SPEW.Register() + INFO = NewFlag("INFO", false, full, short, "Enable log.Info()") + SPEW = NewFlag("SPEW", false, full, short, "Enable log.Spew()") + WARN = NewFlag("WARN", true, full, short, "Enable log.Warn()") - VERBOSE.B = false - VERBOSE.Name = "VERBOSE" - VERBOSE.Subsystem = "log" - VERBOSE.Desc = "Enable log.Verbose()" - VERBOSE.Register() + ERROR = NewFlag("ERROR", true, full, short, "Enable log.Error()") + PRINTLN = NewFlag("PRINTLN", true, full, short, "Enable log.Println()") + VERBOSE = NewFlag("VERBOSE", false, full, short, "Enable log.Verbose()") - WARN.B = true - WARN.Name = "WARN" - WARN.Subsystem = "log" - WARN.Desc = "Enable log.Warn()" - WARN.Register() - - ERROR.B = true - ERROR.Name = "ERROR" - ERROR.Subsystem = "log" - ERROR.Desc = "Enable log.Error()" - ERROR.Register() - - PRINTLN.B = true - PRINTLN.Name = "PRINTLN" - PRINTLN.Subsystem = "log" - PRINTLN.Desc = "Enable log.Println()" - PRINTLN.Register() + // internally used to bypass the possibility that all the flags are off + always = new(LogFlag) + always.b = true + always.orig = true + always.subsystem = full + always.short = short + always.desc = "internal only" } -// set all the flags -func SetAll(b bool) { - flagsMutex.Lock() - defer flagsMutex.Unlock() - for _, f := range flags { - f.B = b - } +// restores flag to it's default value +func (f *LogFlag) SetDefault() { + if ! f.Ok() {return} + f.b = f.orig } // set all the flags @@ -82,7 +84,22 @@ func SetDefaults() { flagsMutex.Lock() defer flagsMutex.Unlock() for _, f := range flags { - f.B = f.Default + f.SetDefault() + } +} + +// protects against panic() by making sure it exists. +func (f *LogFlag) Ok() bool { + if f == nil {return false} + return true +} + +// set all the flags +func SetAll(b bool) { + flagsMutex.Lock() + defer flagsMutex.Unlock() + for _, f := range flags { + f.b = b } } @@ -94,55 +111,86 @@ func ShowFlags() []*LogFlag { flagsMutex.Lock() defer flagsMutex.Unlock() for _, f := range flags { - Log(true, "ShowFlags() ", "(" + f.Subsystem + ")", f.Name, "=", f.B, ":", f.Desc) + Log(always, "ShowFlags() ", "(" + f.subsystem + ")", f.name, "=", f.b, ":", f.desc) } return flags } -// TODO, switch to this +// TODO, switch to this. maybe. func ProcessFlags(callback func(*LogFlag)) { flagsMutex.Lock() defer flagsMutex.Unlock() for _, f := range flags { - Log(true, "ProcessFlags() run callback(f) here on", f) + Log(always, "ProcessFlags() run callback(f) here on", f) callback(f) } } +// returns the value of the flag +func (f *LogFlag) Get() bool { + if ! f.Ok() {return false} + return f.b +} + +// returns the name of the flag +func (f *LogFlag) GetName() string { + if ! f.Ok() {return ""} + return f.name +} + +// returns the subsystem of the flag +func (f *LogFlag) GetSubsystem() string { + if ! f.Ok() {return ""} + return f.subsystem +} + +// returns the description of the flag +func (f *LogFlag) GetDesc() string { + if ! f.Ok() {return ""} + return f.desc +} // register a variable name from a subsystem // inspired by Alex Flint // set the Default value at the time of registration -func (f *LogFlag) Register() { + +// this is what the current log.SetFlag() function should become +func NewFlag(name string, b bool, full, short, desc string) *LogFlag { flagsMutex.Lock() defer flagsMutex.Unlock() - Info("log.Register() ", f) - f.Default = f.B - if f.Short == "" { - f.Short = f.Subsystem - } + f := new(LogFlag) + Log(always, "log.SetFlag() ", full, short, name, true) + f.b = b + f.orig = b + f.short = short + f.subsystem = full + f.name = name + f.desc = desc flags = append(flags,f) + return f } func (f *LogFlag) Set(b bool) { + if ! f.Ok() {return} flagsMutex.Lock() defer flagsMutex.Unlock() - Info("Set() ", "(" + f.Subsystem + ")", f.Name, "=", f.B, ":", f.Desc) - f.B = b - Info("Set() f.B is now", f.B) + Info("Set() ", "(" + f.subsystem + ")", f.name, "=", f.b, ":", f.desc) + f.b = b + Info("Set() f.b is now", f.b) } +/* func Set(subsystem string, name string, b bool) { flagsMutex.Lock() defer flagsMutex.Unlock() Verbose("log.Set() TODO find var:", "(" + subsystem + ")", name, "=", b) for _, f := range flags { - Verbose("log.Set() ", "(" + f.Subsystem + ")", f.Name, "=", f.B, ":", f.Desc) - if (subsystem == f.Subsystem) && (name == f.Name) { + Verbose("log.Set() ", "(" + f.subsystem + ")", f.name, "=", f.b, ":", f.desc) + if (subsystem == f.subsystem) && (name == f.name) { Verbose("log.Set() FOUND ", f) - f.B = b + f.b = b return } } @@ -154,11 +202,12 @@ func Get(subsystem string, name string) bool { defer flagsMutex.Unlock() Verbose("log.Get() TODO find var:", "(" + subsystem + ")", name) for _, f := range flags { - Verbose("log.Get() ", "(" + f.Subsystem + ")", f.Name, "=", f.B, ":", f.Desc) - if (subsystem == f.Subsystem) && (name == f.Name) { + Verbose("log.Get() ", "(" + f.subsystem + ")", f.name, "=", f.b, ":", f.desc) + if (subsystem == f.subsystem) && (name == f.name) { Verbose("log.Get() FOUND ", f) - return f.B + return f.b } } return false } +*/ diff --git a/go.mod b/go.mod deleted file mode 100644 index 243bc19..0000000 --- a/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module go.wit.com/log - -go 1.21.4 - -require go.wit.com/spew v0.0.0-20240101141411-c7b8e91573c9 diff --git a/go.sum b/go.sum deleted file mode 100644 index 6a0c10c..0000000 --- a/go.sum +++ /dev/null @@ -1,2 +0,0 @@ -go.wit.com/spew v0.0.0-20240101141411-c7b8e91573c9 h1:UEX2EzLQPzLTfy/kUFQD7OXtvKn8wk/+jpDOkbl4ff4= -go.wit.com/spew v0.0.0-20240101141411-c7b8e91573c9/go.mod h1:qBpgJXThMMT15vym7/E4Ur9y8oOo2nP7t2RP52QHUNw= diff --git a/info.go b/info.go index 0ffd53a..0d2e81c 100644 --- a/info.go +++ b/info.go @@ -5,11 +5,13 @@ import ( ) func Info(a ...any) { - if ! INFO.B { return } + if ! INFO.Ok() { return } + if ! INFO.b { return } golanglog.Println(a...) } func Infof(s string, a ...any) { - if ! INFO.B { return } + if ! INFO.Ok() { return } + if ! INFO.b { return } golanglog.Printf(s, a...) } diff --git a/log.go b/log.go index 1382816..50ac9bc 100644 --- a/log.go +++ b/log.go @@ -16,45 +16,25 @@ Example: In your package, register NETWARN: - var NETWARN bool - log.Register("myNetPkg", "NETWARN", &NETWARN) + var NETWARN *log.LogFlag + NETWARN = log.NewFlag("NETWARN", true, "go.wit.com/log", "log", "network warnings!") + */ -func Log(x any, a ...any) { - if x == nil { return } - switch x.(type) { - case bool: - if ! x.(bool) { - return - } - origlog.Println(a...) - case LogFlag: - var f LogFlag - f = x.(LogFlag) - if ! f.B { - return - } - a = append([]any{f.Short}, a...) - origlog.Println(a...) - default: - a = append([]any{x}, a...) +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...) + return } + if ! f.Get() { return } + a = append([]any{f.short}, a...) + origlog.Println(a...) } -func Logf(x any, s string, a ...any) { - if x == nil { return } - switch x.(type) { - case bool: - if ! x.(bool) { - return - } - case LogFlag: - var f LogFlag - f = x.(LogFlag) - if ! f.B { - return - } - } +func Logf(f *LogFlag, s string, a ...any) { + if ! f.Get() { return } + s = f.short + " " + s origlog.Printf(s, a...) } diff --git a/original.go b/original.go index b76dcf6..02fb701 100644 --- a/original.go +++ b/original.go @@ -19,18 +19,50 @@ package log */ // TODO: fill in the other functions from "log". Is there a way to automagically do that? +// the full list is: +/* + type Logger + + // NEED THESE + func (l *Logger) Fatal(v ...any) + func (l *Logger) Fatalf(format string, v ...any) + func (l *Logger) Fatalln(v ...any) + func (l *Logger) Panic(v ...any) + func (l *Logger) Panicf(format string, v ...any) + func (l *Logger) Panicln(v ...any) + func (l *Logger) Print(v ...any) + func (l *Logger) Printf(format string, v ...any) + func (l *Logger) Println(v ...any) + + func Default() *Logger + func New(out io.Writer, prefix string, flag int) *Logger + + // what are these? + func (l *Logger) Flags() int + func (l *Logger) SetFlags(flag int) + func (l *Logger) Prefix() string + func (l *Logger) SetPrefix(prefix string) + + // probably not this stuff + func (l *Logger) SetOutput(w io.Writer) + func (l *Logger) Output(calldepth int, s string) error + func (l *Logger) Writer() io.Writer +*/ + import ( origlog "log" ) func Println(a ...any) { - if ! PRINTLN.B { return } + if ! PRINTLN.Ok() { return } + if ! PRINTLN.b { return } origlog.Println(a...) } func Printf(s string, a ...any) { - if ! PRINTLN.B { return } + if ! PRINTLN.Ok() { return } + if ! PRINTLN.b { return } origlog.Printf(s, a...) } diff --git a/spew.go b/spew.go index 2ea40f0..7f433a7 100644 --- a/spew.go +++ b/spew.go @@ -6,7 +6,8 @@ import ( ) func Spew(b any, a ...any) { - if ! SPEW.B { return } + if ! SPEW.Ok() { return } + if ! SPEW.b { return } switch b.(type) { case bool: @@ -17,7 +18,7 @@ func Spew(b any, a ...any) { case LogFlag: var f LogFlag f = b.(LogFlag) - if ! f.B { + if ! f.b { return } origlog.Println("SPEW:", spew.Sdump(a...)) diff --git a/verbose.go b/verbose.go index 31c3772..ed776b8 100644 --- a/verbose.go +++ b/verbose.go @@ -5,6 +5,7 @@ import ( ) func Verbose(a ...any) { - if ! VERBOSE.B { return } + if ! VERBOSE.Ok() { return } + if ! VERBOSE.b { return } golanglog.Println(a...) } diff --git a/warn.go b/warn.go index 0ad3118..e8518bd 100644 --- a/warn.go +++ b/warn.go @@ -5,6 +5,7 @@ import ( ) func Warn(a ...any) { - if ! WARN.B { return } + if ! WARN.Ok() { return } + if ! WARN.b { return } origlog.Println(a...) }