diff options
Diffstat (limited to 'src/gobang.go')
-rw-r--r-- | src/gobang.go | 143 |
1 files changed, 85 insertions, 58 deletions
diff --git a/src/gobang.go b/src/gobang.go index fff0503..b1fe281 100644 --- a/src/gobang.go +++ b/src/gobang.go @@ -15,7 +15,9 @@ import ( "math/bits" "os" "reflect" + "runtime" "runtime/debug" + "slices" "strings" "sync" "syscall" @@ -410,9 +412,6 @@ func Hash(password []byte, salt []byte) []byte{ return hash } -// FIXME: finish rewriting -// FIXME: add tests -// // getV7Time returns the time in milliseconds and nanoseconds / 256. // The returned (milli << (12 + seq)) is guaranteed to be greater than // (milli << (12 + seq)) returned by any previous call to getV7Time. @@ -518,6 +517,45 @@ func ParseUUID(str string) (UUID, error) { return UUID { bytes: [uuidByteCount]byte(data) }, nil } +func sourceInfo(skip int) slog.Attr { + pc := make([]uintptr, 10) + n := runtime.Callers(skip, pc) + if n == 0 { + return slog.Group( + "src", + "file", "UNAVAILABLE", + "function", "UNAVAILABLE", + "line", "UNAVAILABLE", + ) + } + + pc = pc[:n] + frames := runtime.CallersFrames(pc) + frame, _ := frames.Next() + return slog.Group( + "src", + "file", frame.File, + "function", frame.Function, + "line", frame.Line, + ) +} + +func logArgs(type_ string) []string { + return []string { + "id", NewUUID().String(), + "kind", "log", + "type", type_, + } +} + +func anyArr[S ~[]E, E any](arr S) []any { + ret := make([]any, len(arr)) + for i , el := range arr { + ret[i] = el + } + return ret +} + func Debug(message string, type_ string, args ...any) { if level < LevelDebug { return @@ -525,13 +563,10 @@ func Debug(message string, type_ string, args ...any) { slog.Debug( message, - append( - []any { - "id", NewUUID(), - "kind", "log", - "type", type_, - }, - args..., + slices.Concat( + anyArr(logArgs(type_)), + []any { sourceInfo(3) }, + args, )..., ) } @@ -543,13 +578,10 @@ func Info(message string, type_ string, args ...any) { slog.Info( message, - append( - []any { - "id", NewUUID(), - "kind", "log", - "type", type_, - }, - args..., + slices.Concat( + anyArr(logArgs(type_)), + []any { sourceInfo(3) }, + args, )..., ) } @@ -561,13 +593,10 @@ func Warning(message string, type_ string, args ...any) { slog.Warn( message, - append( - []any { - "id", NewUUID(), - "kind", "log", - "type", type_, - }, - args..., + slices.Concat( + anyArr(logArgs(type_)), + []any { sourceInfo(3) }, + args, )..., ) } @@ -579,36 +608,46 @@ func Error(message string, type_ string, args ...any) { slog.Error( message, - append( - []any { - "id", NewUUID(), - "kind", "log", - "type", type_, - }, - args..., + slices.Concat( + anyArr(logArgs(type_)), + []any { sourceInfo(3) }, + args, )..., ) } -func Metric(type_ string, label string, args ...any) { +func metric(type_ string, label string, args ...any) { if !emitMetric { return } slog.Info( "_", - append( + slices.Concat( []any { - "id", NewUUID(), + "id", NewUUID().String(), "kind", "metric", "type", type_, "label", label, }, - args..., + []any { sourceInfo(3) }, + args, )..., ) } +func MakeCounter(label string) func(...any) { + return func(args ...any) { + metric( + "counter", label, + slices.Concat( + []any { "value", 1 }, + args, + )..., + ) + } +} + func MakeGauge(label string, staticArgs ...any) Gauge { var zero = big.NewInt(0) var one = big.NewInt(1) @@ -618,25 +657,20 @@ func MakeGauge(label string, staticArgs ...any) Gauge { Error( "Gauge went negative", "process-metric", - append( + slices.Concat( []any { "value", count }, - append( - staticArgs, - dynamicArgs..., - )..., + staticArgs, + dynamicArgs, )..., ) return // avoid wrong metrics being emitted } - Metric( + metric( "gauge", label, - // TODO: we'll have slices.Concat on Go 1.22 - append( + slices.Concat( []any { "value", count }, - append( - staticArgs, - dynamicArgs..., - )..., + staticArgs, + dynamicArgs, )..., ) } @@ -652,15 +686,6 @@ func MakeGauge(label string, staticArgs ...any) Gauge { } } -func MakeCounter(label string) func(...any) { - return func(args ...any) { - Metric( - "counter", label, - append([]any { "value", 1 }, args...)..., - ) - } -} - func ErrorIf(err error) { if err != nil { fmt.Fprintf(os.Stderr, "Unexpected error: %#v\n", err) @@ -727,15 +752,17 @@ func AssertEqualI(i int, given any, expected any) { } } +var unfilteringLevel = new(slog.LevelVar) func setLoggerOutput(w io.Writer) { + unfilteringLevel.Set(slog.LevelDebug) slog.SetDefault(slog.New(slog.NewJSONHandler(w, &slog.HandlerOptions { - AddSource: true, + Level: unfilteringLevel, })).With( slog.Group( "info", "pid", os.Getpid(), "ppid", os.Getppid(), - "puuid", NewUUID(), + "puuid", NewUUID().String(), ), )) } |