2023-12-31 10:45:04 -06:00
|
|
|
package log
|
|
|
|
|
2024-01-04 12:34:04 -06:00
|
|
|
/*
|
|
|
|
Handles the on/off flags for things like log.Info() and log.Warn()
|
|
|
|
*/
|
|
|
|
|
2023-12-31 10:45:04 -06:00
|
|
|
import (
|
2024-01-04 12:34:04 -06:00
|
|
|
"sync"
|
2023-12-31 10:45:04 -06:00
|
|
|
)
|
|
|
|
|
2024-01-04 12:34:04 -06:00
|
|
|
var INFO LogFlag
|
|
|
|
var VERBOSE LogFlag
|
2024-01-01 01:49:53 -06:00
|
|
|
var SPEW LogFlag
|
2024-01-04 12:34:04 -06:00
|
|
|
var WARN LogFlag
|
|
|
|
var ERROR LogFlag
|
|
|
|
var PRINTLN LogFlag
|
|
|
|
|
|
|
|
// writeMutex protects locks the write process
|
|
|
|
var flagsMutex sync.Mutex
|
2023-12-31 19:44:55 -06:00
|
|
|
|
2024-01-01 01:49:53 -06:00
|
|
|
type LogFlag struct {
|
|
|
|
B bool
|
2024-01-04 15:22:08 -06:00
|
|
|
Default bool // set at the time of Registration()
|
2024-01-01 01:49:53 -06:00
|
|
|
Name string
|
2024-01-05 20:39:56 -06:00
|
|
|
// 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
|
2024-01-01 01:49:53 -06:00
|
|
|
Desc string
|
2023-12-31 19:44:55 -06:00
|
|
|
}
|
2023-12-31 10:45:04 -06:00
|
|
|
|
2024-01-01 01:49:53 -06:00
|
|
|
var flags []*LogFlag
|
2023-12-31 16:08:01 -06:00
|
|
|
|
|
|
|
func init() {
|
2024-01-04 12:34:04 -06:00
|
|
|
INFO.B = false
|
|
|
|
INFO.Name = "INFO"
|
|
|
|
INFO.Subsystem = "log"
|
|
|
|
INFO.Desc = "Enable log.Info()"
|
|
|
|
INFO.Register()
|
2023-12-31 19:44:55 -06:00
|
|
|
|
2024-01-01 01:49:53 -06:00
|
|
|
SPEW.B = false
|
|
|
|
SPEW.Name = "SPEW"
|
|
|
|
SPEW.Subsystem = "log"
|
|
|
|
SPEW.Desc = "Enable log.Spew()"
|
2024-01-04 12:34:04 -06:00
|
|
|
SPEW.Register()
|
|
|
|
|
|
|
|
VERBOSE.B = false
|
|
|
|
VERBOSE.Name = "VERBOSE"
|
|
|
|
VERBOSE.Subsystem = "log"
|
|
|
|
VERBOSE.Desc = "Enable log.Verbose()"
|
|
|
|
VERBOSE.Register()
|
|
|
|
|
|
|
|
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()
|
2023-12-31 16:08:01 -06:00
|
|
|
}
|
|
|
|
|
2024-01-04 12:34:04 -06:00
|
|
|
// set all the flags
|
|
|
|
func SetAll(b bool) {
|
|
|
|
flagsMutex.Lock()
|
|
|
|
defer flagsMutex.Unlock()
|
2024-01-01 06:45:49 -06:00
|
|
|
for _, f := range flags {
|
|
|
|
f.B = b
|
|
|
|
}
|
2023-12-31 10:45:04 -06:00
|
|
|
}
|
|
|
|
|
2024-01-04 15:22:08 -06:00
|
|
|
// set all the flags
|
|
|
|
func SetDefaults() {
|
|
|
|
flagsMutex.Lock()
|
|
|
|
defer flagsMutex.Unlock()
|
|
|
|
for _, f := range flags {
|
|
|
|
f.B = f.Default
|
|
|
|
}
|
|
|
|
}
|
2024-01-04 12:34:04 -06:00
|
|
|
|
2024-01-03 14:22:47 -06:00
|
|
|
// this bypasses all checks and _always_ logs the info to STDOUT
|
|
|
|
// is this a bad idea? Probably not....
|
2024-01-04 12:34:04 -06:00
|
|
|
// TODO: returning []*LogFlag is not safe and access must be locked
|
|
|
|
// but this is only used by the log debugging window at this time
|
|
|
|
func ShowFlags() []*LogFlag {
|
|
|
|
flagsMutex.Lock()
|
|
|
|
defer flagsMutex.Unlock()
|
2024-01-01 01:49:53 -06:00
|
|
|
for _, f := range flags {
|
2024-01-04 12:34:04 -06:00
|
|
|
Log(true, "ShowFlags() ", "(" + f.Subsystem + ")", f.Name, "=", f.B, ":", f.Desc)
|
2024-01-01 01:49:53 -06:00
|
|
|
}
|
|
|
|
|
2024-01-04 12:34:04 -06:00
|
|
|
return flags
|
2023-12-31 10:45:04 -06:00
|
|
|
}
|
|
|
|
|
2024-01-04 12:34:04 -06:00
|
|
|
// TODO, switch to this
|
|
|
|
func ProcessFlags(callback func(*LogFlag)) {
|
|
|
|
flagsMutex.Lock()
|
|
|
|
defer flagsMutex.Unlock()
|
|
|
|
for _, f := range flags {
|
|
|
|
Log(true, "ProcessFlags() run callback(f) here on", f)
|
|
|
|
callback(f)
|
2023-12-31 10:45:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-04 12:34:04 -06:00
|
|
|
|
2024-01-01 01:49:53 -06:00
|
|
|
// register a variable name from a subsystem
|
2024-01-01 06:45:49 -06:00
|
|
|
// inspired by Alex Flint
|
2024-01-04 15:22:08 -06:00
|
|
|
// set the Default value at the time of registration
|
2024-01-01 01:49:53 -06:00
|
|
|
func (f *LogFlag) Register() {
|
2024-01-04 12:34:04 -06:00
|
|
|
flagsMutex.Lock()
|
|
|
|
defer flagsMutex.Unlock()
|
2024-01-01 01:49:53 -06:00
|
|
|
Info("log.Register() ", f)
|
2024-01-04 15:22:08 -06:00
|
|
|
f.Default = f.B
|
2024-01-06 17:10:59 -06:00
|
|
|
if f.Short == "" {
|
|
|
|
f.Short = f.Subsystem
|
|
|
|
}
|
2024-01-01 01:49:53 -06:00
|
|
|
flags = append(flags,f)
|
|
|
|
}
|
|
|
|
|
2024-01-04 12:34:04 -06:00
|
|
|
func (f *LogFlag) Set(b bool) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Set(subsystem string, name string, b bool) {
|
|
|
|
flagsMutex.Lock()
|
|
|
|
defer flagsMutex.Unlock()
|
2024-01-05 20:39:56 -06:00
|
|
|
Verbose("log.Set() TODO find var:", "(" + subsystem + ")", name, "=", b)
|
2024-01-04 12:34:04 -06:00
|
|
|
for _, f := range flags {
|
2024-01-05 20:39:56 -06:00
|
|
|
Verbose("log.Set() ", "(" + f.Subsystem + ")", f.Name, "=", f.B, ":", f.Desc)
|
2024-01-04 12:34:04 -06:00
|
|
|
if (subsystem == f.Subsystem) && (name == f.Name) {
|
2024-01-05 20:39:56 -06:00
|
|
|
Verbose("log.Set() FOUND ", f)
|
2024-01-04 12:34:04 -06:00
|
|
|
f.B = b
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func Get(subsystem string, name string) bool {
|
|
|
|
flagsMutex.Lock()
|
|
|
|
defer flagsMutex.Unlock()
|
2024-01-05 20:39:56 -06:00
|
|
|
Verbose("log.Get() TODO find var:", "(" + subsystem + ")", name)
|
2024-01-04 12:34:04 -06:00
|
|
|
for _, f := range flags {
|
2024-01-05 20:39:56 -06:00
|
|
|
Verbose("log.Get() ", "(" + f.Subsystem + ")", f.Name, "=", f.B, ":", f.Desc)
|
2024-01-04 12:34:04 -06:00
|
|
|
if (subsystem == f.Subsystem) && (name == f.Name) {
|
2024-01-05 20:39:56 -06:00
|
|
|
Verbose("log.Get() FOUND ", f)
|
2024-01-04 12:34:04 -06:00
|
|
|
return f.B
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2023-12-31 10:45:04 -06:00
|
|
|
}
|