From 9884eca784e853e48ff82ecd904890d28b2fe0fa Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Mon, 18 Aug 2025 15:58:37 +0200 Subject: allow game log to be configured using command line flags --- go/log/log.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'go/log/log.go') diff --git a/go/log/log.go b/go/log/log.go index ff090425..dbe3af66 100644 --- a/go/log/log.go +++ b/go/log/log.go @@ -8,18 +8,32 @@ import ( "os" ) -const GameLogLevel = 99 +var GameLogLevel slog.Level = 99 + +var Logger *slog.Logger var handleGameRecord func(any) +// RegisterGameRecordHandler registers a callback for game records. +// The registered callback can be used by the game's UI to present game log entries to the user. +func RegisterGameRecordHandler(handler func(any)) { + handleGameRecord = handler +} + func init() { - // TODO: allow to specify the value and the file - l := slog.New(slog.NewTextHandler( + Logger = slog.New(slog.NewTextHandler( os.Stdout, &slog.HandlerOptions{AddSource: true})) - slog.SetDefault(l) + slog.SetDefault(Logger) +} + +// Configure controls the logging behavior of the default logger. +func Configure(files []string, opts *slog.HandlerOptions) { + Logger = slog.New(NewGameLogHandler(files, opts)) + slog.SetDefault(Logger) } +// Game calls the registered handleGameRecord callback and [Logger.Log] on the default logger. func Game(obj any) { if handleGameRecord != nil { handleGameRecord(obj) @@ -28,34 +42,42 @@ func Game(obj any) { slog.Log(context.Background(), GameLogLevel, msg) } +// Debug calls [Logger.Debug] on the default logger. func Debug(msg string, args ...any) { slog.Debug(msg, args...) } +// Info calls [Logger.Info] on the default logger. func Info(msg string, args ...any) { slog.Info(msg, args...) } +// Warn calls [Logger.Warn] on the default logger. func Warn(msg string, args ...any) { slog.Warn(msg, args...) } +// Error calls [Logger.Error] on the default logger. func Error(msg string, args ...any) { slog.Error(msg, args...) } +// Fatal calls [log.Fatal]. func Fatal(args ...any) { log.Fatal(args...) } +// Fatalf calls [log.Fatalf]. func Fatalf(msg string, args ...any) { log.Fatalf(msg, args...) } +// Panic calls [log.Panic]. func Panic(args ...any) { log.Panic(args...) } +// Panicf calls [log.Panicf]. func Panicf(msg string, args ...any) { log.Panicf(msg, args...) } -- cgit v1.2.3