mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-08-30 15:52:03 +02:00
improve debug logging, fix stringer interface, debuglog events sent, fix debuglogging affecting the actual values.
This commit is contained in:
36
log_debug.go
36
log_debug.go
@@ -8,17 +8,33 @@ import (
|
||||
)
|
||||
|
||||
func debugLog(str string, args ...any) {
|
||||
// this is such that we don't modify the actual args that may be used outside of this function
|
||||
printableArgs := make([]any, len(args))
|
||||
|
||||
for i, v := range args {
|
||||
switch v.(type) {
|
||||
case []json.RawMessage:
|
||||
j, _ := json.Marshal(v)
|
||||
args[i] = string(j)
|
||||
case []byte:
|
||||
args[i] = string(v.([]byte))
|
||||
case fmt.Stringer:
|
||||
args[i] = v.(fmt.Stringer).String()
|
||||
}
|
||||
printableArgs[i] = stringify(v)
|
||||
}
|
||||
|
||||
DebugLogger.Printf(str, args...)
|
||||
DebugLogger.Printf(str, printableArgs...)
|
||||
}
|
||||
|
||||
func stringify(anything any) any {
|
||||
switch v := anything.(type) {
|
||||
case []any:
|
||||
// this is such that we don't modify the actual values that may be used outside of this function
|
||||
printableValues := make([]any, len(v))
|
||||
for i, subv := range v {
|
||||
printableValues[i] = stringify(subv)
|
||||
}
|
||||
return printableValues
|
||||
case []json.RawMessage:
|
||||
j, _ := json.Marshal(v)
|
||||
return string(j)
|
||||
case []byte:
|
||||
return string(v)
|
||||
case fmt.Stringer:
|
||||
return v.String()
|
||||
default:
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user