try allowing switching to writing to http

This commit is contained in:
Jeff Carr 2024-11-07 01:23:43 -06:00
parent 2b11a4e334
commit ef61eb494b
14 changed files with 155 additions and 93 deletions

View File

@ -9,6 +9,9 @@ redomod:
GO111MODULE= go mod init GO111MODULE= go mod init
GO111MODULE= go mod tidy GO111MODULE= go mod tidy
goimport:
goimports -w *.go
github: github:
git push origin master git push origin master
git push origin devel git push origin devel

View File

@ -5,6 +5,8 @@
package log package log
func Bool(b bool, a ...any) { func Bool(b bool, a ...any) {
if ! b {return} if !b {
return
}
realPrintln(a...) realPrintln(a...)
} }

1
doc.go
View File

@ -3,6 +3,7 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package log package log
// //
// version v1.5 // version v1.5
// //

View File

@ -5,7 +5,9 @@
package log package log
func Error(err error, a ...any) { func Error(err error, a ...any) {
if ! ERROR.Get() { return } if !ERROR.Get() {
return
}
realPrintln("Error:", err) realPrintln("Error:", err)
realPrintln(a...) realPrintln(a...)
} }

View File

@ -25,19 +25,18 @@ package log
*/ */
import (
"net/http"
import (
"sync" "sync"
) )
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()
var WARN *LogFlag // toggles log.Warn() (true by default) var WARN *LogFlag // toggles log.Warn() (true by default)
var ERROR *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 PRINTLN *LogFlag // toggles log.Println() (true by default)
var always *LogFlag var always *LogFlag
var allon bool = false var allon bool = false
@ -46,18 +45,18 @@ var allon bool = false
var flagsMutex sync.Mutex var flagsMutex sync.Mutex
type LogFlag struct { type LogFlag struct {
b bool b bool
orig bool // used as the Default value. set at the time of Registration() orig bool // used as the Default value. set at the time of Registration()
name string name string
// TODO: figure out what package is sending the Registration // TODO: figure out what package is sending the Registration
subsystem string // probably should just be forced to be the package name subsystem string // probably should just be forced to be the package name
short string // string actually printed on each line short string // string actually printed on each line
desc string desc string
} }
var flags []*LogFlag var flags []*LogFlag
var daemonMode bool var daemonMode bool
var httpMode http.ResponseWriter
func init() { func init() {
full := "go.wit.com/log" full := "go.wit.com/log"
@ -75,16 +74,18 @@ func init() {
INFO = NewFlag("INFO", true, full, short, "Enable log.Info()") INFO = NewFlag("INFO", true, full, short, "Enable log.Info()")
SPEW = NewFlag("SPEW", false, full, short, "Enable log.Spew()") 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()") ERROR = NewFlag("ERROR", true, full, short, "Enable log.Error()")
PRINTLN = NewFlag("PRINTLN", true, full, short, "Enable log.Println()") PRINTLN = NewFlag("PRINTLN", true, full, short, "Enable log.Println()")
VERBOSE = NewFlag("VERBOSE", false, full, short, "Enable log.Verbose()") VERBOSE = NewFlag("VERBOSE", false, full, short, "Enable log.Verbose()")
} }
// restores flag to it's default value // restores flag to it's default value
func (f *LogFlag) SetDefault() { func (f *LogFlag) SetDefault() {
if ! f.Ok() {return} if !f.Ok() {
return
}
f.b = f.orig f.b = f.orig
} }
@ -99,7 +100,9 @@ func SetDefaults() {
// simply protects against panic() by making sure it exists. // simply protects against panic() by making sure it exists.
func (f *LogFlag) Ok() bool { func (f *LogFlag) Ok() bool {
if f == nil {return false} if f == nil {
return false
}
return true return true
} }
@ -121,7 +124,7 @@ func ShowFlags() []*LogFlag {
flagsMutex.Lock() flagsMutex.Lock()
defer flagsMutex.Unlock() defer flagsMutex.Unlock()
for _, f := range flags { 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 return flags
@ -141,13 +144,17 @@ func ProcessFlags(callback func(*LogFlag)) {
// probably a better name than Get() // probably a better name than Get()
// switch to this // switch to this
func (f *LogFlag) Bool() bool { func (f *LogFlag) Bool() bool {
if ! f.Ok() {return false} if !f.Ok() {
return false
}
return f.b return f.b
} }
// returns the value of the flag // returns the value of the flag
func (f *LogFlag) Get() bool { func (f *LogFlag) Get() bool {
if ! f.Ok() {return false} if !f.Ok() {
return false
}
return f.b return f.b
} }
@ -160,25 +167,33 @@ func (f *LogFlag) Set(b bool) {
// returns the name of the flag // returns the name of the flag
func (f *LogFlag) GetName() string { func (f *LogFlag) GetName() string {
if ! f.Ok() {return ""} if !f.Ok() {
return ""
}
return f.name return f.name
} }
// returns the subsystem of the flag // returns the subsystem of the flag
func (f *LogFlag) GetSubsystem() string { func (f *LogFlag) GetSubsystem() string {
if ! f.Ok() {return ""} if !f.Ok() {
return ""
}
return f.subsystem return f.subsystem
} }
// returns the description of the flag // returns the description of the flag
func (f *LogFlag) GetDesc() string { func (f *LogFlag) GetDesc() string {
if ! f.Ok() {return ""} if !f.Ok() {
return ""
}
return f.desc return f.desc
} }
// returns the description of the flag // returns the description of the flag
func (f *LogFlag) GetDefault() bool { func (f *LogFlag) GetDefault() bool {
if ! f.Ok() {return false} if !f.Ok() {
return false
}
return f.orig return f.orig
} }
@ -202,17 +217,17 @@ func NewFlag(name string, b bool, full, short, desc string) *LogFlag {
f.subsystem = full f.subsystem = full
f.name = name f.name = name
f.desc = desc f.desc = desc
flags = append(flags,f) flags = append(flags, f)
return f return f
} }
func (f *LogFlag) SetBool(b bool) { func (f *LogFlag) SetBool(b bool) {
/* /*
if ! f.Ok() {return} if ! f.Ok() {return}
flagsMutex.Lock() flagsMutex.Lock()
defer flagsMutex.Unlock() 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 f.b = b
Info("Set() f.b is now", f.b) Info("Set() f.b is now", f.b)
} }

16
info.go
View File

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

10
log.go
View File

@ -22,19 +22,23 @@ 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"}, a...)
realPrintln(a...) realPrintln(a...)
return return
} }
if ! f.Get() { return } if !f.Get() {
return
}
a = append([]any{f.short}, a...) a = append([]any{f.short}, a...)
realPrintln(a...) realPrintln(a...)
} }
func Logf(f *LogFlag, s string, a ...any) { func Logf(f *LogFlag, s string, a ...any) {
if ! f.Get() { return } if !f.Get() {
return
}
s = f.short + " " + s s = f.short + " " + s
realPrintf(s, a...) realPrintf(s, a...)
} }

View File

@ -50,33 +50,35 @@ package log
*/ */
func Println(a ...any) { func Println(a ...any) {
if ! PRINTLN.Ok() { return } if !PRINTLN.Ok() {
if ! PRINTLN.b { return } return
}
if !PRINTLN.b {
return
}
realPrintln(a...) realPrintln(a...)
} }
func Printf(s string, a ...any) { func Printf(s string, a ...any) {
if ! PRINTLN.Ok() { return } if !PRINTLN.Ok() {
if ! PRINTLN.b { return } return
}
if !PRINTLN.b {
return
}
realPrintf(s, a...) realPrintf(s, a...)
} }
func Sprint(a ...any) string { func Sprint(a ...any) string {
if ! PRINTLN.Ok() { return ""}
if ! PRINTLN.b { return ""}
return realSprint(a...) return realSprint(a...)
} }
func Sprintf(s string, a ...any) string { func Sprintf(s string, a ...any) string {
if ! PRINTLN.Ok() { return ""}
if ! PRINTLN.b { return ""}
return realSprintf(s, a...) return realSprintf(s, a...)
} }
func Sprintln(s string, a ...any) string { func Sprintln(a ...any) string {
if ! PRINTLN.Ok() { return ""} return realSprintln(a...)
if ! PRINTLN.b { return ""}
return realSprintln(s, a...)
} }
func Fatalln(a ...any) { func Fatalln(a ...any) {

View File

@ -4,14 +4,14 @@
package log package log
import ( import (
"os"
golanglog "log" golanglog "log"
"os"
) )
// start writing all the logging to a tmp file // start writing all the logging to a tmp file
func SetTmpOLD() { 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 { if err != nil {
realFatalf("error opening file: %v", err) realFatalf("error opening file: %v", err)
} }

View File

@ -3,15 +3,20 @@ package log
// implements 'daemon' mode so switches to fmt // implements 'daemon' mode so switches to fmt
// instead of log so that timestamps are not printed twice // instead of log so that timestamps are not printed twice
import ( import (
"fmt" "fmt"
reallog "log" reallog "log"
"net/http"
) )
func DaemonMode(b bool) { func DaemonMode(b bool) {
daemonMode = b daemonMode = b
} }
func HttpMode(w http.ResponseWriter) {
httpMode = w
}
func DaemonShow() { func DaemonShow() {
if daemonMode { if daemonMode {
fmt.Println("daemonMode=true") fmt.Println("daemonMode=true")
@ -21,18 +26,26 @@ func DaemonShow() {
} }
func realPrintln(a ...any) { func realPrintln(a ...any) {
if daemonMode { if httpMode == nil {
fmt.Println(a...) if daemonMode {
fmt.Println(a...)
} else {
reallog.Println(a...)
}
} else { } else {
reallog.Println(a...) fmt.Fprintln(httpMode, a...)
} }
} }
func realPrintf(s string, a ...any) { func realPrintf(s string, a ...any) {
if daemonMode { if httpMode == nil {
fmt.Printf(s, a...) if daemonMode {
fmt.Printf(s, a...)
} else {
reallog.Printf(s, a...)
}
} else { } 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...) return fmt.Sprintf(s, a...)
} }
func realSprintln(s string, a ...any) string { func realSprintln(a ...any) string {
return fmt.Sprintf(s, a...) + "\n" return fmt.Sprintln(a...)
} }
func realFatalln(a ...any) { func realFatalln(a ...any) {

View File

@ -6,19 +6,19 @@ package log
// a shortcut for sleep so you don't have to always change the import lines when debugging // a shortcut for sleep so you don't have to always change the import lines when debugging
import ( import (
"os"
"time"
"errors" "errors"
"os"
"reflect" "reflect"
"time"
) )
/* /*
sleep() # you know what this does? sleeps for 1 second. yep. dump. easy. 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(.1) # you know what this does? yes, it sleeps for 1/10th of a second
*/ */
func Sleep(a ...any) { func Sleep(a ...any) {
if (a == nil) { if a == nil {
time.Sleep(time.Second) time.Sleep(time.Second)
return return
} }
@ -29,16 +29,16 @@ func Sleep(a ...any) {
case int: case int:
time.Sleep(time.Duration(a[0].(int)) * time.Second) time.Sleep(time.Duration(a[0].(int)) * time.Second)
case float64: case float64:
time.Sleep(time.Duration(a[0].(float64) * 1000) * time.Millisecond) time.Sleep(time.Duration(a[0].(float64)*1000) * time.Millisecond)
default: default:
Info("sleep a[0], type = ", a[0], reflect.TypeOf(a[0])) Info("sleep a[0], type = ", a[0], reflect.TypeOf(a[0]))
} }
} }
/* /*
exit() # yep. exits. I guess everything must be fine exit() # yep. exits. I guess everything must be fine
exit(3) # I guess 3 it is then exit(3) # I guess 3 it is then
exit("dont like apples") # ok. I'll make a note of that exit("dont like apples") # ok. I'll make a note of that
*/ */
func Exit(a ...any) { func Exit(a ...any) {
Error(errors.New("log.Exit()"), a...) Error(errors.New("log.Exit()"), a...)

26
spew.go
View File

@ -4,24 +4,28 @@
package log package log
import ( import (
"go.wit.com/dev/davecgh/spew" "go.wit.com/dev/davecgh/spew"
) )
func Spew(b any, a ...any) { func Spew(b any, a ...any) {
if ! SPEW.Ok() { return } if !SPEW.Ok() {
if ! SPEW.b { return } return
}
if !SPEW.b {
return
}
switch b.(type) { switch b.(type) {
case bool: case bool:
if ! b.(bool) { if !b.(bool) {
return return
} }
realPrintln("SPEW:", spew.Sdump(a...)) realPrintln("SPEW:", spew.Sdump(a...))
case LogFlag: case LogFlag:
var f LogFlag var f LogFlag
f = b.(LogFlag) f = b.(LogFlag)
if ! f.b { if !f.b {
return return
} }
realPrintln("SPEW:", spew.Sdump(a...)) realPrintln("SPEW:", spew.Sdump(a...))
@ -31,11 +35,11 @@ func Spew(b any, a ...any) {
} }
// realPrintln("SPEW:", spew.Sdump(a...)) // realPrintln("SPEW:", spew.Sdump(a...))
/* /*
scs := spew.ConfigState{Indent: "\t", MaxDepth: 1} scs := spew.ConfigState{Indent: "\t", MaxDepth: 1}
// Output using the ConfigState instance. // Output using the ConfigState instance.
v := map[string]int{"one": 1} v := map[string]int{"one": 1}
scs.Printf("v: %v\n", v) scs.Printf("v: %v\n", v)
scs.Dump(v) scs.Dump(v)
scs.Dump(a) scs.Dump(a)
*/ */
} }

View File

@ -5,7 +5,11 @@
package log package log
func Verbose(a ...any) { func Verbose(a ...any) {
if ! VERBOSE.Ok() { return } if !VERBOSE.Ok() {
if ! VERBOSE.b { return } return
}
if !VERBOSE.b {
return
}
realPrintln(a...) realPrintln(a...)
} }

View File

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