a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
at main 1.7 kB view raw
1//go:build !go1.21 2// +build !go1.21 3 4package log 5 6import ( 7 "context" 8 "runtime" 9 "sync/atomic" 10 11 "golang.org/x/exp/slog" 12) 13 14// type alises for slog. 15type ( 16 slogAttr = slog.Attr 17 slogValue = slog.Value 18 slogLogValuer = slog.LogValuer 19) 20 21const slogKindGroup = slog.KindGroup 22 23// Enabled reports whether the logger is enabled for the given level. 24// 25// Implements slog.Handler. 26func (l *Logger) Enabled(_ context.Context, level slog.Level) bool { 27 return atomic.LoadInt64(&l.level) <= int64(level) 28} 29 30// Handle handles the Record. It will only be called if Enabled returns true. 31// 32// Implements slog.Handler. 33func (l *Logger) Handle(_ context.Context, record slog.Record) error { 34 fields := make([]interface{}, 0, record.NumAttrs()*2) 35 record.Attrs(func(a slog.Attr) bool { 36 fields = append(fields, a.Key, a.Value) 37 return true 38 }) 39 // Get the caller frame using the record's PC. 40 frames := runtime.CallersFrames([]uintptr{record.PC}) 41 frame, _ := frames.Next() 42 l.handle(Level(record.Level), l.timeFunc(record.Time), []runtime.Frame{frame}, record.Message, fields...) 43 return nil 44} 45 46// WithAttrs returns a new Handler with the given attributes added. 47// 48// Implements slog.Handler. 49func (l *Logger) WithAttrs(attrs []slog.Attr) slog.Handler { 50 fields := make([]interface{}, 0, len(attrs)*2) 51 for _, attr := range attrs { 52 fields = append(fields, attr.Key, attr.Value) 53 } 54 return l.With(fields...) 55} 56 57// WithGroup returns a new Handler with the given group name prepended to the 58// current group name or prefix. 59// 60// Implements slog.Handler. 61func (l *Logger) WithGroup(name string) slog.Handler { 62 if l.prefix != "" { 63 name = l.prefix + "." + name 64 } 65 return l.WithPrefix(name) 66} 67 68var _ slog.Handler = (*Logger)(nil)