aboutsummaryrefslogtreecommitdiff
path: root/go/log/log.go
blob: 040eaef8afb7918a5047f5adfc9ae77038c176c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package log

import (
	"context"
	"fmt"
	"log"
	"log/slog"
)

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
}

// 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)
	}
	msg := fmt.Sprintf("%s", obj)
	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...)
}