a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
1package log 2 3import ( 4 "bytes" 5 "fmt" 6 "io" 7 "log" 8 "os" 9 "sync" 10 "sync/atomic" 11 "time" 12 13 "github.com/muesli/termenv" 14) 15 16var ( 17 // registry is a map of all registered lipgloss renderers. 18 registry = sync.Map{} 19 20 // defaultLogger is the default global logger instance. 21 defaultLogger atomic.Pointer[Logger] 22 defaultLoggerOnce sync.Once 23) 24 25// Default returns the default logger. The default logger comes with timestamp enabled. 26func Default() *Logger { 27 dl := defaultLogger.Load() 28 if dl == nil { 29 defaultLoggerOnce.Do(func() { 30 defaultLogger.CompareAndSwap( 31 nil, NewWithOptions(os.Stderr, Options{ReportTimestamp: true}), 32 ) 33 }) 34 dl = defaultLogger.Load() 35 } 36 return dl 37} 38 39// SetDefault sets the default global logger. 40func SetDefault(logger *Logger) { 41 defaultLogger.Store(logger) 42} 43 44// New returns a new logger with the default options. 45func New(w io.Writer) *Logger { 46 return NewWithOptions(w, Options{}) 47} 48 49// NewWithOptions returns a new logger using the provided options. 50func NewWithOptions(w io.Writer, o Options) *Logger { 51 l := &Logger{ 52 b: bytes.Buffer{}, 53 mu: &sync.RWMutex{}, 54 helpers: &sync.Map{}, 55 level: int64(o.Level), 56 reportTimestamp: o.ReportTimestamp, 57 reportCaller: o.ReportCaller, 58 prefix: o.Prefix, 59 timeFunc: o.TimeFunction, 60 timeFormat: o.TimeFormat, 61 formatter: o.Formatter, 62 fields: o.Fields, 63 callerFormatter: o.CallerFormatter, 64 callerOffset: o.CallerOffset, 65 } 66 67 l.SetOutput(w) 68 l.SetLevel(Level(l.level)) 69 l.SetStyles(DefaultStyles()) 70 71 if l.callerFormatter == nil { 72 l.callerFormatter = ShortCallerFormatter 73 } 74 75 if l.timeFunc == nil { 76 l.timeFunc = func(t time.Time) time.Time { return t } 77 } 78 79 if l.timeFormat == "" { 80 l.timeFormat = DefaultTimeFormat 81 } 82 83 return l 84} 85 86// SetReportTimestamp sets whether to report timestamp for the default logger. 87func SetReportTimestamp(report bool) { 88 Default().SetReportTimestamp(report) 89} 90 91// SetReportCaller sets whether to report caller location for the default logger. 92func SetReportCaller(report bool) { 93 Default().SetReportCaller(report) 94} 95 96// SetLevel sets the level for the default logger. 97func SetLevel(level Level) { 98 Default().SetLevel(level) 99} 100 101// GetLevel returns the level for the default logger. 102func GetLevel() Level { 103 return Default().GetLevel() 104} 105 106// SetTimeFormat sets the time format for the default logger. 107func SetTimeFormat(format string) { 108 Default().SetTimeFormat(format) 109} 110 111// SetTimeFunction sets the time function for the default logger. 112func SetTimeFunction(f TimeFunction) { 113 Default().SetTimeFunction(f) 114} 115 116// SetOutput sets the output for the default logger. 117func SetOutput(w io.Writer) { 118 Default().SetOutput(w) 119} 120 121// SetFormatter sets the formatter for the default logger. 122func SetFormatter(f Formatter) { 123 Default().SetFormatter(f) 124} 125 126// SetCallerFormatter sets the caller formatter for the default logger. 127func SetCallerFormatter(f CallerFormatter) { 128 Default().SetCallerFormatter(f) 129} 130 131// SetCallerOffset sets the caller offset for the default logger. 132func SetCallerOffset(offset int) { 133 Default().SetCallerOffset(offset) 134} 135 136// SetPrefix sets the prefix for the default logger. 137func SetPrefix(prefix string) { 138 Default().SetPrefix(prefix) 139} 140 141// SetColorProfile force sets the underlying Lip Gloss renderer color profile 142// for the TextFormatter. 143func SetColorProfile(profile termenv.Profile) { 144 Default().SetColorProfile(profile) 145} 146 147// SetStyles sets the logger styles for the TextFormatter. 148func SetStyles(s *Styles) { 149 Default().SetStyles(s) 150} 151 152// GetPrefix returns the prefix for the default logger. 153func GetPrefix() string { 154 return Default().GetPrefix() 155} 156 157// With returns a new logger with the given keyvals. 158func With(keyvals ...interface{}) *Logger { 159 return Default().With(keyvals...) 160} 161 162// WithPrefix returns a new logger with the given prefix. 163func WithPrefix(prefix string) *Logger { 164 return Default().WithPrefix(prefix) 165} 166 167// Helper marks the calling function as a helper 168// and skips it for source location information. 169// It's the equivalent of testing.TB.Helper(). 170func Helper() { 171 Default().helper(1) 172} 173 174// Log logs a message with the given level. 175func Log(level Level, msg interface{}, keyvals ...interface{}) { 176 Default().Log(level, msg, keyvals...) 177} 178 179// Debug logs a debug message. 180func Debug(msg interface{}, keyvals ...interface{}) { 181 Default().Log(DebugLevel, msg, keyvals...) 182} 183 184// Info logs an info message. 185func Info(msg interface{}, keyvals ...interface{}) { 186 Default().Log(InfoLevel, msg, keyvals...) 187} 188 189// Warn logs a warning message. 190func Warn(msg interface{}, keyvals ...interface{}) { 191 Default().Log(WarnLevel, msg, keyvals...) 192} 193 194// Error logs an error message. 195func Error(msg interface{}, keyvals ...interface{}) { 196 Default().Log(ErrorLevel, msg, keyvals...) 197} 198 199// Fatal logs a fatal message and exit. 200func Fatal(msg interface{}, keyvals ...interface{}) { 201 Default().Log(FatalLevel, msg, keyvals...) 202 os.Exit(1) 203} 204 205// Print logs a message with no level. 206func Print(msg interface{}, keyvals ...interface{}) { 207 Default().Log(noLevel, msg, keyvals...) 208} 209 210// Logf logs a message with formatting and level. 211func Logf(level Level, format string, args ...interface{}) { 212 Default().Logf(level, format, args...) 213} 214 215// Debugf logs a debug message with formatting. 216func Debugf(format string, args ...interface{}) { 217 Default().Log(DebugLevel, fmt.Sprintf(format, args...)) 218} 219 220// Infof logs an info message with formatting. 221func Infof(format string, args ...interface{}) { 222 Default().Log(InfoLevel, fmt.Sprintf(format, args...)) 223} 224 225// Warnf logs a warning message with formatting. 226func Warnf(format string, args ...interface{}) { 227 Default().Log(WarnLevel, fmt.Sprintf(format, args...)) 228} 229 230// Errorf logs an error message with formatting. 231func Errorf(format string, args ...interface{}) { 232 Default().Log(ErrorLevel, fmt.Sprintf(format, args...)) 233} 234 235// Fatalf logs a fatal message with formatting and exit. 236func Fatalf(format string, args ...interface{}) { 237 Default().Log(FatalLevel, fmt.Sprintf(format, args...)) 238 os.Exit(1) 239} 240 241// Printf logs a message with formatting and no level. 242func Printf(format string, args ...interface{}) { 243 Default().Log(noLevel, fmt.Sprintf(format, args...)) 244} 245 246// StandardLog returns a standard logger from the default logger. 247func StandardLog(opts ...StandardLogOptions) *log.Logger { 248 return Default().StandardLog(opts...) 249}