2023-12-31 10:45:04 -06:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var INFO bool = true
|
|
|
|
var WARN bool = true
|
|
|
|
var ERROR bool = true
|
|
|
|
|
|
|
|
var VERBOSE bool = false
|
2023-12-31 19:44:55 -06:00
|
|
|
// var SPEW bool = false
|
|
|
|
|
2024-01-01 01:49:53 -06:00
|
|
|
var SPEW LogFlag
|
2023-12-31 19:44:55 -06:00
|
|
|
|
2024-01-01 01:49:53 -06:00
|
|
|
type LogFlag struct {
|
|
|
|
B bool
|
|
|
|
Name string
|
|
|
|
Subsystem string
|
|
|
|
Desc string
|
2023-12-31 19:44:55 -06:00
|
|
|
}
|
2023-12-31 10:45:04 -06:00
|
|
|
|
2023-12-31 16:08:01 -06:00
|
|
|
var registered map[string][]string
|
2024-01-01 01:49:53 -06:00
|
|
|
var flags []*LogFlag
|
2023-12-31 16:08:01 -06:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
registered = make(map[string][]string)
|
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()"
|
2023-12-31 19:44:55 -06:00
|
|
|
|
|
|
|
// register the default flags used by this log package
|
|
|
|
registered["log"] = []string{"SPEW","INFO", "WARN", "ERROR", "VERBOSE"}
|
2023-12-31 16:08:01 -06:00
|
|
|
}
|
|
|
|
|
2023-12-31 10:45:04 -06:00
|
|
|
func All(b bool) {
|
|
|
|
Set("SPEW", b)
|
|
|
|
Set("INFO", b)
|
|
|
|
Set("WARN", b)
|
|
|
|
Set("ERROR", b)
|
|
|
|
Set("VERBOSE", b)
|
2024-01-01 06:45:49 -06:00
|
|
|
for _, f := range flags {
|
2024-01-03 14:22:47 -06:00
|
|
|
Warn("All() ", "(" + f.Subsystem + ")", f.Name, "=", f.B, ":", f.Desc)
|
2024-01-01 06:45:49 -06:00
|
|
|
f.B = b
|
|
|
|
Warn("All() f.B is now", f.B)
|
|
|
|
}
|
2023-12-31 10:45: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....
|
2023-12-31 19:44:55 -06:00
|
|
|
func ListFlags() map[string][]string {
|
2024-01-03 14:22:47 -06:00
|
|
|
Log(true, "ListFlags() registered =", registered)
|
2024-01-01 01:49:53 -06:00
|
|
|
for _, f := range flags {
|
2024-01-03 14:22:47 -06:00
|
|
|
Log(true, "ListFlags() ", "(" + f.Subsystem + ")", f.Name, "=", f.B, ":", f.Desc)
|
2024-01-01 01:49:53 -06:00
|
|
|
}
|
|
|
|
|
2023-12-31 16:08:01 -06:00
|
|
|
return registered
|
2023-12-31 10:45:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func Set(flag string, b bool) {
|
|
|
|
switch flag {
|
|
|
|
case "INFO":
|
|
|
|
INFO = b
|
|
|
|
case "WARN":
|
|
|
|
WARN = b
|
|
|
|
case "SPEW":
|
2024-01-01 01:49:53 -06:00
|
|
|
SPEW.B = b
|
2023-12-31 10:45:04 -06:00
|
|
|
case "ERROR":
|
|
|
|
ERROR = b
|
|
|
|
case "VERBOSE":
|
|
|
|
VERBOSE = 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":
|
2024-01-01 01:49:53 -06:00
|
|
|
return SPEW.B
|
2023-12-31 10:45:04 -06:00
|
|
|
case "ERROR":
|
|
|
|
return ERROR
|
|
|
|
case "VERBOSE":
|
|
|
|
return VERBOSE
|
|
|
|
default:
|
|
|
|
Error(errors.New("unknown flag"), "Flag name sent:", flag)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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-01 01:49:53 -06:00
|
|
|
func (f *LogFlag) Register() {
|
|
|
|
Info("log.Register() ", f)
|
|
|
|
flags = append(flags,f)
|
|
|
|
}
|
|
|
|
|
2023-12-31 10:45:04 -06:00
|
|
|
// register a variable name from a subsystem
|
|
|
|
// this is used for custom log flags
|
2023-12-31 16:08:01 -06:00
|
|
|
func Register(subsystem string, name string, b *bool) {
|
|
|
|
Info("log.Register() got subsystem", subsystem, "with name =", name, "bool value =", b)
|
|
|
|
registered[subsystem] = append(registered[subsystem], name)
|
2023-12-31 10:45:04 -06:00
|
|
|
}
|