mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-11-02 17:27:23 -04:00
28a40983ad
* moved logger into d2util * resolved upstream conflict
102 lines
1.7 KiB
Go
102 lines
1.7 KiB
Go
package d2util
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
)
|
|
|
|
// LogLevel determines how verbose the logging is (higher is more verbose)
|
|
type LogLevel int
|
|
|
|
// Log levels
|
|
const (
|
|
LogLevelNone LogLevel = iota
|
|
LogLevelError
|
|
LogLevelWarning
|
|
LogLevelInfo
|
|
LogLevelDebug
|
|
)
|
|
|
|
// Log format strings for log levels
|
|
const (
|
|
LogFmtDebug = "[DEBUG] %s\n\r"
|
|
LogFmtInfo = "[INFO] %s\n\r"
|
|
LogFmtWarning = "[WARNING] %s\n\r"
|
|
LogFmtError = "[ERROR] %s\n\r"
|
|
)
|
|
|
|
// Logger is used to write log messages, and can have a log level to determine verbosity
|
|
type Logger struct {
|
|
io.Writer
|
|
level LogLevel
|
|
}
|
|
|
|
// SetLevel sets the log level
|
|
func (l *Logger) SetLevel(level LogLevel) {
|
|
l.level = level
|
|
}
|
|
|
|
// Debug logs a debug message
|
|
func (l *Logger) Debug(msg string) {
|
|
if l == nil {
|
|
return
|
|
}
|
|
|
|
l.print(LogLevelDebug, msg)
|
|
}
|
|
|
|
// Info logs an info message
|
|
func (l *Logger) Info(msg string) {
|
|
if l == nil {
|
|
return
|
|
}
|
|
|
|
l.print(LogLevelInfo, msg)
|
|
}
|
|
|
|
// Warning logs a warning message
|
|
func (l *Logger) Warning(msg string) {
|
|
if l == nil {
|
|
return
|
|
}
|
|
|
|
l.print(LogLevelWarning, msg)
|
|
}
|
|
|
|
// Error logs an error message
|
|
func (l *Logger) Error(msg string) {
|
|
if l == nil {
|
|
return
|
|
}
|
|
|
|
l.print(LogLevelError, msg)
|
|
}
|
|
|
|
func (l *Logger) print(level LogLevel, msg string) {
|
|
if l == nil || l.level < level {
|
|
return
|
|
}
|
|
|
|
fmtString := ""
|
|
|
|
switch level {
|
|
case LogLevelDebug:
|
|
fmtString = LogFmtDebug
|
|
case LogLevelInfo:
|
|
fmtString = LogFmtInfo
|
|
case LogLevelWarning:
|
|
fmtString = LogFmtWarning
|
|
case LogLevelError:
|
|
fmtString = LogFmtError
|
|
case LogLevelNone:
|
|
default:
|
|
return
|
|
}
|
|
|
|
_, _ = l.Write(format(fmtString, []byte(msg)))
|
|
}
|
|
|
|
func format(fmtStr string, fmtInput []byte) []byte {
|
|
return []byte(fmt.Sprintf(fmtStr, string(fmtInput)))
|
|
}
|