From ef61eb494b42a7bc226de1edc114579ce530abdd Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 7 Nov 2024 01:23:43 -0600 Subject: [PATCH] try allowing switching to writing to http --- Makefile | 3 ++ bool.go | 4 ++- doc.go | 1 + error.go | 4 ++- flags.go | 83 +++++++++++++++++++++++++++++++---------------------- info.go | 16 ++++++++--- log.go | 10 +++++-- original.go | 28 +++++++++--------- output.go | 6 ++-- reallog.go | 31 ++++++++++++++------ sleep.go | 20 ++++++------- spew.go | 26 ++++++++++------- verbose.go | 8 ++++-- warn.go | 8 ++++-- 14 files changed, 155 insertions(+), 93 deletions(-) diff --git a/Makefile b/Makefile index 0e76e55..d562c95 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,9 @@ redomod: GO111MODULE= go mod init GO111MODULE= go mod tidy +goimport: + goimports -w *.go + github: git push origin master git push origin devel diff --git a/bool.go b/bool.go index 03a056b..7bcefa2 100644 --- a/bool.go +++ b/bool.go @@ -5,6 +5,8 @@ package log func Bool(b bool, a ...any) { - if ! b {return} + if !b { + return + } realPrintln(a...) } diff --git a/doc.go b/doc.go index b56ce1e..c9eb4ac 100644 --- a/doc.go +++ b/doc.go @@ -3,6 +3,7 @@ // license that can be found in the LICENSE file. package log + // // version v1.5 // diff --git a/error.go b/error.go index 2235ad5..983b522 100644 --- a/error.go +++ b/error.go @@ -5,7 +5,9 @@ package log func Error(err error, a ...any) { - if ! ERROR.Get() { return } + if !ERROR.Get() { + return + } realPrintln("Error:", err) realPrintln(a...) } diff --git a/flags.go b/flags.go index 8488d01..927ccb5 100644 --- a/flags.go +++ b/flags.go @@ -25,19 +25,18 @@ package log */ - - -import ( +import ( + "net/http" "sync" ) -var INFO *LogFlag // toggles log.Info() -var VERBOSE *LogFlag // toggles log.Verbose() -var SPEW *LogFlag // toggles log.Spew() +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 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 var allon bool = false @@ -46,18 +45,18 @@ var allon bool = false var flagsMutex sync.Mutex type LogFlag struct { - b bool - orig bool // used as the Default value. 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 var daemonMode bool - +var httpMode http.ResponseWriter func init() { full := "go.wit.com/log" @@ -75,16 +74,18 @@ func init() { INFO = NewFlag("INFO", true, full, short, "Enable log.Info()") SPEW = NewFlag("SPEW", false, full, short, "Enable log.Spew()") - WARN = NewFlag("WARN", true, full, short, "Enable log.Warn()") + WARN = NewFlag("WARN", true, full, short, "Enable log.Warn()") - 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()") + 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()") } // restores flag to it's default value func (f *LogFlag) SetDefault() { - if ! f.Ok() {return} + if !f.Ok() { + return + } f.b = f.orig } @@ -99,7 +100,9 @@ func SetDefaults() { // simply protects against panic() by making sure it exists. func (f *LogFlag) Ok() bool { - if f == nil {return false} + if f == nil { + return false + } return true } @@ -121,7 +124,7 @@ func ShowFlags() []*LogFlag { flagsMutex.Lock() defer flagsMutex.Unlock() for _, f := range flags { - Log(always, "ShowFlags() ", "(" + f.subsystem + ")", f.name, "=", f.b, ":", f.desc) + Log(always, "ShowFlags() ", "("+f.subsystem+")", f.name, "=", f.b, ":", f.desc) } return flags @@ -141,13 +144,17 @@ func ProcessFlags(callback func(*LogFlag)) { // probably a better name than Get() // switch to this func (f *LogFlag) Bool() bool { - if ! f.Ok() {return false} + if !f.Ok() { + return false + } return f.b } // returns the value of the flag func (f *LogFlag) Get() bool { - if ! f.Ok() {return false} + if !f.Ok() { + return false + } return f.b } @@ -160,25 +167,33 @@ func (f *LogFlag) Set(b bool) { // returns the name of the flag func (f *LogFlag) GetName() string { - if ! f.Ok() {return ""} + if !f.Ok() { + return "" + } return f.name } // returns the subsystem of the flag func (f *LogFlag) GetSubsystem() string { - if ! f.Ok() {return ""} + if !f.Ok() { + return "" + } return f.subsystem } // returns the description of the flag func (f *LogFlag) GetDesc() string { - if ! f.Ok() {return ""} + if !f.Ok() { + return "" + } return f.desc } // returns the description of the flag func (f *LogFlag) GetDefault() bool { - if ! f.Ok() {return false} + if !f.Ok() { + return false + } return f.orig } @@ -202,17 +217,17 @@ func NewFlag(name string, b bool, full, short, desc string) *LogFlag { f.subsystem = full f.name = name f.desc = desc - flags = append(flags,f) + flags = append(flags, f) return f } func (f *LogFlag) SetBool(b bool) { /* - if ! f.Ok() {return} - flagsMutex.Lock() - defer flagsMutex.Unlock() + if ! f.Ok() {return} + flagsMutex.Lock() + defer flagsMutex.Unlock() */ - Info("Set() ", "(" + f.subsystem + ")", f.name, "=", f.b, ":", f.desc) + Info("Set() ", "("+f.subsystem+")", f.name, "=", f.b, ":", f.desc) f.b = b Info("Set() f.b is now", f.b) } diff --git a/info.go b/info.go index f9b8194..0219fdc 100644 --- a/info.go +++ b/info.go @@ -5,13 +5,21 @@ package log func Info(a ...any) { - if ! INFO.Ok() { return } - if ! INFO.b { return } + if !INFO.Ok() { + return + } + if !INFO.b { + return + } realPrintln(a...) } func Infof(s string, a ...any) { - if ! INFO.Ok() { return } - if ! INFO.b { return } + if !INFO.Ok() { + return + } + if !INFO.b { + return + } realPrintf(s, a...) } diff --git a/log.go b/log.go index 5b436b6..deb15a0 100644 --- a/log.go +++ b/log.go @@ -22,19 +22,23 @@ In your package, register NETWARN: */ 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 a = append([]any{"FLAG = NULL"}, a...) realPrintln(a...) return } - if ! f.Get() { return } + if !f.Get() { + return + } a = append([]any{f.short}, a...) realPrintln(a...) } func Logf(f *LogFlag, s string, a ...any) { - if ! f.Get() { return } + if !f.Get() { + return + } s = f.short + " " + s realPrintf(s, a...) } diff --git a/original.go b/original.go index 7bd287d..7663e5e 100644 --- a/original.go +++ b/original.go @@ -5,7 +5,7 @@ package log "log" } - and + and import ( "go.wit.com/log" @@ -50,33 +50,35 @@ package log */ func Println(a ...any) { - if ! PRINTLN.Ok() { return } - if ! PRINTLN.b { return } + if !PRINTLN.Ok() { + return + } + if !PRINTLN.b { + return + } realPrintln(a...) } func Printf(s string, a ...any) { - if ! PRINTLN.Ok() { return } - if ! PRINTLN.b { return } + if !PRINTLN.Ok() { + return + } + if !PRINTLN.b { + return + } realPrintf(s, a...) } func Sprint(a ...any) string { - if ! PRINTLN.Ok() { return ""} - if ! PRINTLN.b { return ""} return realSprint(a...) } func Sprintf(s string, a ...any) string { - if ! PRINTLN.Ok() { return ""} - if ! PRINTLN.b { return ""} return realSprintf(s, a...) } -func Sprintln(s string, a ...any) string { - if ! PRINTLN.Ok() { return ""} - if ! PRINTLN.b { return ""} - return realSprintln(s, a...) +func Sprintln(a ...any) string { + return realSprintln(a...) } func Fatalln(a ...any) { diff --git a/output.go b/output.go index 4982f0d..4d8eeff 100644 --- a/output.go +++ b/output.go @@ -4,14 +4,14 @@ package log -import ( - "os" +import ( golanglog "log" + "os" ) // start writing all the logging to a tmp file func SetTmpOLD() { - f, err := os.OpenFile("/tmp/guilogfile", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) + f, err := os.OpenFile("/tmp/guilogfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { realFatalf("error opening file: %v", err) } diff --git a/reallog.go b/reallog.go index 9191fa8..a2c9a76 100644 --- a/reallog.go +++ b/reallog.go @@ -3,15 +3,20 @@ package log // implements 'daemon' mode so switches to fmt // instead of log so that timestamps are not printed twice -import ( +import ( "fmt" reallog "log" + "net/http" ) func DaemonMode(b bool) { daemonMode = b } +func HttpMode(w http.ResponseWriter) { + httpMode = w +} + func DaemonShow() { if daemonMode { fmt.Println("daemonMode=true") @@ -21,18 +26,26 @@ func DaemonShow() { } func realPrintln(a ...any) { - if daemonMode { - fmt.Println(a...) + if httpMode == nil { + if daemonMode { + fmt.Println(a...) + } else { + reallog.Println(a...) + } } else { - reallog.Println(a...) + fmt.Fprintln(httpMode, a...) } } func realPrintf(s string, a ...any) { - if daemonMode { - fmt.Printf(s, a...) + if httpMode == nil { + if daemonMode { + fmt.Printf(s, a...) + } else { + reallog.Printf(s, a...) + } } else { - reallog.Printf(s, a...) + fmt.Fprintln(httpMode, fmt.Sprintf(s, a...)) } } @@ -44,8 +57,8 @@ func realSprintf(s string, a ...any) string { return fmt.Sprintf(s, a...) } -func realSprintln(s string, a ...any) string { - return fmt.Sprintf(s, a...) + "\n" +func realSprintln(a ...any) string { + return fmt.Sprintln(a...) } func realFatalln(a ...any) { diff --git a/sleep.go b/sleep.go index 40484be..28b49ce 100644 --- a/sleep.go +++ b/sleep.go @@ -6,19 +6,19 @@ package log // a shortcut for sleep so you don't have to always change the import lines when debugging -import ( - "os" - "time" +import ( "errors" + "os" "reflect" + "time" ) /* - sleep() # you know what this does? sleeps for 1 second. yep. dump. easy. - sleep(.1) # you know what this does? yes, it sleeps for 1/10th of a second +sleep() # you know what this does? sleeps for 1 second. yep. dump. easy. +sleep(.1) # you know what this does? yes, it sleeps for 1/10th of a second */ func Sleep(a ...any) { - if (a == nil) { + if a == nil { time.Sleep(time.Second) return } @@ -29,16 +29,16 @@ func Sleep(a ...any) { case int: time.Sleep(time.Duration(a[0].(int)) * time.Second) case float64: - time.Sleep(time.Duration(a[0].(float64) * 1000) * time.Millisecond) + time.Sleep(time.Duration(a[0].(float64)*1000) * time.Millisecond) default: Info("sleep a[0], type = ", a[0], reflect.TypeOf(a[0])) } } /* - exit() # yep. exits. I guess everything must be fine - exit(3) # I guess 3 it is then - exit("dont like apples") # ok. I'll make a note of that +exit() # yep. exits. I guess everything must be fine +exit(3) # I guess 3 it is then +exit("dont like apples") # ok. I'll make a note of that */ func Exit(a ...any) { Error(errors.New("log.Exit()"), a...) diff --git a/spew.go b/spew.go index 15b28bf..f088cb7 100644 --- a/spew.go +++ b/spew.go @@ -4,24 +4,28 @@ package log -import ( +import ( "go.wit.com/dev/davecgh/spew" ) func Spew(b any, a ...any) { - if ! SPEW.Ok() { return } - if ! SPEW.b { return } + if !SPEW.Ok() { + return + } + if !SPEW.b { + return + } switch b.(type) { case bool: - if ! b.(bool) { + if !b.(bool) { return } realPrintln("SPEW:", spew.Sdump(a...)) case LogFlag: var f LogFlag f = b.(LogFlag) - if ! f.b { + if !f.b { return } realPrintln("SPEW:", spew.Sdump(a...)) @@ -31,11 +35,11 @@ func Spew(b any, a ...any) { } // realPrintln("SPEW:", spew.Sdump(a...)) /* - scs := spew.ConfigState{Indent: "\t", MaxDepth: 1} - // Output using the ConfigState instance. - v := map[string]int{"one": 1} - scs.Printf("v: %v\n", v) - scs.Dump(v) - scs.Dump(a) + scs := spew.ConfigState{Indent: "\t", MaxDepth: 1} + // Output using the ConfigState instance. + v := map[string]int{"one": 1} + scs.Printf("v: %v\n", v) + scs.Dump(v) + scs.Dump(a) */ } diff --git a/verbose.go b/verbose.go index b0ae7bf..15ac95d 100644 --- a/verbose.go +++ b/verbose.go @@ -5,7 +5,11 @@ package log func Verbose(a ...any) { - if ! VERBOSE.Ok() { return } - if ! VERBOSE.b { return } + if !VERBOSE.Ok() { + return + } + if !VERBOSE.b { + return + } realPrintln(a...) } diff --git a/warn.go b/warn.go index d02d301..f94e255 100644 --- a/warn.go +++ b/warn.go @@ -5,7 +5,11 @@ package log func Warn(a ...any) { - if ! WARN.Ok() { return } - if ! WARN.b { return } + if !WARN.Ok() { + return + } + if !WARN.b { + return + } realPrintln(a...) }