more work on registration of flags

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-01-01 01:49:53 -06:00
parent bf41970f77
commit b780c1a929
3 changed files with 63 additions and 22 deletions

View File

@ -11,24 +11,25 @@ var ERROR bool = true
var VERBOSE bool = false
// var SPEW bool = false
var SPEW logFlag
var SPEW LogFlag
type logFlag struct {
b bool
name string
pkg string
desc string
type LogFlag struct {
B bool
Name string
Subsystem string
Desc string
}
var registered map[string][]string
var flags []*LogFlag
func init() {
registered = make(map[string][]string)
SPEW.b = false
SPEW.name = "SPEW"
SPEW.pkg = "log"
SPEW.desc = "Enable log.Spew()"
SPEW.B = false
SPEW.Name = "SPEW"
SPEW.Subsystem = "log"
SPEW.Desc = "Enable log.Spew()"
// register the default flags used by this log package
registered["log"] = []string{"SPEW","INFO", "WARN", "ERROR", "VERBOSE"}
@ -43,6 +44,11 @@ func All(b bool) {
}
func ListFlags() map[string][]string {
Info("ListFlags() registered =", registered)
for _, f := range flags {
Info("ListFlags() flag B =", f.B, "Name =", f.Name, "Subsystem =", f.Subsystem, "Description:", f.Desc)
}
return registered
}
@ -53,7 +59,7 @@ func Set(flag string, b bool) {
case "WARN":
WARN = b
case "SPEW":
SPEW.b = b
SPEW.B = b
case "ERROR":
ERROR = b
case "VERBOSE":
@ -70,7 +76,7 @@ func Get(flag string) bool {
case "WARN":
return WARN
case "SPEW":
return SPEW.b
return SPEW.B
case "ERROR":
return ERROR
case "VERBOSE":
@ -81,6 +87,12 @@ func Get(flag string) bool {
return false
}
// register a variable name from a subsystem
func (f *LogFlag) Register() {
Info("log.Register() ", f)
flags = append(flags,f)
}
// register a variable name from a subsystem
// this is used for custom log flags
func Register(subsystem string, name string, b *bool) {

39
log.go
View File

@ -20,12 +20,41 @@ In your package, register NETWARN:
log.Register("myNetPkg", "NETWARN", &NETWARN)
*/
func Log(b bool, a ...any) {
if ! b { return }
origlog.Println(a...)
func Log(x any, a ...any) {
if x == nil { return }
switch x.(type) {
case bool:
if ! x.(bool) {
return
}
origlog.Println(a...)
case LogFlag:
var f LogFlag
f = x.(LogFlag)
if ! f.B {
return
}
a = append([]any{f.Subsystem}, a...)
origlog.Println(a...)
default:
a = append([]any{x}, a...)
origlog.Println(a...)
}
}
func Logf(b bool, s string, a ...any) {
if ! b { return }
func Logf(x any, s string, a ...any) {
if x == nil { return }
switch x.(type) {
case bool:
if ! x.(bool) {
return
}
case LogFlag:
var f LogFlag
f = x.(LogFlag)
if ! f.B {
return
}
}
origlog.Printf(s, a...)
}

10
spew.go
View File

@ -6,7 +6,7 @@ import (
)
func Spew(b any, a ...any) {
if ! SPEW.b { return }
if ! SPEW.B { return }
switch b.(type) {
case bool:
@ -14,10 +14,10 @@ func Spew(b any, a ...any) {
return
}
origlog.Println("SPEW:", spew.Sdump(a...))
case logFlag:
var f logFlag
f = b.(logFlag)
if ! f.b {
case LogFlag:
var f LogFlag
f = b.(LogFlag)
if ! f.B {
return
}
origlog.Println("SPEW:", spew.Sdump(a...))