From d7d3fbc3a31dbba4de1737244f39945608e5a0ac Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 30 Dec 2023 20:48:24 -0600 Subject: [PATCH] more or less functional draft --- doc.go | 32 +++++++++++++++++++++++ error.go | 3 ++- info.go | 15 +---------- log.go | 44 +++++++++++++++++++++++++++++++ output.go | 19 ++++++++++++++ sleep.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ spew.go | 20 +++++++++++++++ warn.go | 1 + 8 files changed, 196 insertions(+), 15 deletions(-) create mode 100644 doc.go create mode 100644 output.go create mode 100644 sleep.go create mode 100644 spew.go diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..902ca38 --- /dev/null +++ b/doc.go @@ -0,0 +1,32 @@ +package log +// +// version v1.5 +// +// I like things to be easy. +// +// this means all the log settings are in one place. it should allow +// things to be over-ridden externally to the library +// but still allow command line --args to pass debugging settings +// +// I also have a generic sleep() and exit() in here because it's simple +// +// Usage: +// +// log.Info("something", foo, bar) +// log.Bool(DEBUG, "something else", someOtherVariable) # if DEBUG == false, return doing nothing +// + +/* + I've spent, am spending, too much time thinking about 'logging'. 'log', 'logrus', 'zap', whatever. + I'm not twitter. i don't give a fuck about how many nanoseconds it takes to log. Anyway, this + implementation is probably faster than all of those because you just set one bool to FALSE + and it all stops. + Sometimes I need to capture to stdout, sometimes stdout can't + work because it doesn't exist for the user. This whole thing is a PITA. Then it's spread + over 8 million references in every .go file. I'm tapping out and putting + it in one place. here it is. Also, this makes having debug levels really fucking easy. + You can define whatever level of logging you want from anywhere (command line) etc. + + log() # doesn't do anything + log(stuff) # sends it to whatever log you define in a single place. here is the place +*/ diff --git a/error.go b/error.go index 95c6cc3..b01068e 100644 --- a/error.go +++ b/error.go @@ -4,6 +4,7 @@ import ( origlog "log" ) -func Error(a ...any) { +func Error(err error, a ...any) { + origlog.Println("Error:", err) origlog.Println(a...) } diff --git a/info.go b/info.go index b390062..7afbc15 100644 --- a/info.go +++ b/info.go @@ -1,23 +1,10 @@ package log import ( - "os" golanglog "log" ) func Info(a ...any) { + if ! INFO { return } golanglog.Println(a...) } - -// start writing all the logging to a tmp file -func SetTmp() { - f, err := os.OpenFile("/tmp/guilogfile", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) - if err != nil { - golanglog.Fatalf("error opening file: %v", err) - } - // hmm. is there a trick here or must this be in main() - // defer f.Close() - - golanglog.SetOutput(f) - golanglog.Println("This is a test log entry") -} diff --git a/log.go b/log.go index 675bfe3..d63b6a1 100644 --- a/log.go +++ b/log.go @@ -1,9 +1,53 @@ package log import ( + "errors" origlog "log" ) +var SPEW bool = true +var INFO bool = true +var WARN bool = true +var ERROR bool = true + +func All(b bool) { + Set("SPEW", b) + Set("INFO", b) + Set("WARN", b) + Set("ERROR", b) +} + +func Set(flag string, b bool) { + switch flag { + case "INFO": + INFO = b + case "WARN": + WARN = b + case "SPEW": + SPEW = b + case "ERROR": + ERROR = b + default: + Error(errors.New("unknown flag"), "Flag name sent:", flag) + } +} + +func Get(flag string) bool { + switch flag { + case "INFO": + return INFO + case "WARN": + return WARN + case "SPEW": + return SPEW + case "ERROR": + return ERROR + default: + Error(errors.New("unknown flag"), "Flag name sent:", flag) + } + return false +} + func Println(a ...any) { origlog.Println(a...) } diff --git a/output.go b/output.go new file mode 100644 index 0000000..8077332 --- /dev/null +++ b/output.go @@ -0,0 +1,19 @@ +package log + +import ( + "os" + golanglog "log" +) + +// start writing all the logging to a tmp file +func SetTmp() { + f, err := os.OpenFile("/tmp/guilogfile", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) + if err != nil { + golanglog.Fatalf("error opening file: %v", err) + } + // hmm. is there a trick here or must this be in main() + // defer f.Close() + + golanglog.SetOutput(f) + golanglog.Println("This is a test log entry") +} diff --git a/sleep.go b/sleep.go new file mode 100644 index 0000000..baacca0 --- /dev/null +++ b/sleep.go @@ -0,0 +1,77 @@ +package log +// +// version v1.2 +// +// I like things to be easy. +// +// this means all the log settings are in one place. it should allow +// things to be over-ridden externally to the library +// but still allow command line --args to pass debugging settings +// +// I also have a generic sleep() and exit() in here because it's simple +// +// Usage: +// +// log("something", foo, bar) +// var DEBUG bool = true +// log(DEBUG, "something else", someOtherVariable) # if DEBUG == false, return doing nothing +// + +/* + I've spent, am spending, too much time thinking about 'logging'. 'log', 'logrus', 'zap', whatever. + I'm not twitter. i don't give a fuck about how many nanoseconds it takes to log. Anyway, this + implementation is probably faster than all of those because you just set one bool to FALSE + and it all stops. + Sometimes I need to capture to stdout, sometimes stdout can't + work because it doesn't exist for the user. This whole thing is a PITA. Then it's spread + over 8 million references in every .go file. I'm tapping out and putting + it in one place. here it is. Also, this makes having debug levels really fucking easy. + You can define whatever level of logging you want from anywhere (command line) etc. + + log() # doesn't do anything + log(stuff) # sends it to whatever log you define in a single place. here is the place +*/ + +import ( + "os" + "time" + "errors" + "reflect" + + origlog "log" +) + +/* + 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) { + time.Sleep(time.Second) + return + } + + origlog.Println("sleep", a[0]) + + switch a[0].(type) { + case int: + time.Sleep(time.Duration(a[0].(int)) * time.Second) + case float64: + time.Sleep(time.Duration(a[0].(float64) * 1000) * time.Millisecond) + default: + origlog.Println("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 +*/ +func Exit(a ...any) { + Error(errors.New("Exit"), a) + //if (a) { + // os.Exit(a) + //} + os.Exit(0) +} diff --git a/spew.go b/spew.go new file mode 100644 index 0000000..fa0e8b1 --- /dev/null +++ b/spew.go @@ -0,0 +1,20 @@ +package log + +import ( + origlog "log" + "github.com/davecgh/go-spew/spew" +) + +func Spew(a ...any) { + if ! SPEW { return } + origlog.Println("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) + */ +} diff --git a/warn.go b/warn.go index 75d466c..b15311d 100644 --- a/warn.go +++ b/warn.go @@ -5,5 +5,6 @@ import ( ) func Warn(a ...any) { + if ! WARN { return } origlog.Println(a...) }