1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-02 17:27:23 -04:00
OpenDiablo2/d2common/d2util/logger.go
gravestench d6c9748fef
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset

* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file

* fixed log level tests
2020-11-02 21:23:07 -05:00

197 lines
3.8 KiB
Go

package d2util
import (
"fmt"
"io"
"log"
"runtime"
)
// LogLevel determines how verbose the logging is (higher is more verbose)
type LogLevel = int
// Log levels
const (
LogLevelNone LogLevel = iota
LogLevelError
LogLevelWarning
LogLevelInfo
LogLevelDebug
LogLevelUnspecified
)
// LogLevelDefault is the default log level
const LogLevelDefault = LogLevelInfo
const (
red = 1
green = 2
yellow = 3
magenta = 5
cyan = 6
)
const fmtColorEscape = "\033[3%dm"
const colorEscapeReset = "\033[0m"
// Log format strings for log levels
const (
fmtPrefix = "[%s]"
LogFmtDebug = "[DEBUG]" + colorEscapeReset + " %s"
LogFmtInfo = "[INFO]" + colorEscapeReset + " %s"
LogFmtWarning = "[WARNING]" + colorEscapeReset + " %s"
LogFmtError = "[ERROR]" + colorEscapeReset + " %s"
)
// NewLogger creates a new logger with a default
func NewLogger() *Logger {
l := &Logger{
level: LogLevelDefault,
colorEnabled: true,
}
l.Writer = log.Writer()
return l
}
// Logger is used to write log messages, and can have a log level to determine verbosity
type Logger struct {
prefix string
io.Writer
level LogLevel
colorEnabled bool
}
// SetPrefix sets a prefix for the message.
// example:
// logger.SetPrefix("XYZ")
// logger.Debug("ABC") will print "[XYZ] [DEBUG] ABC"
func (l *Logger) SetPrefix(s string) {
l.prefix = s
}
// SetLevel sets the log level
func (l *Logger) SetLevel(level LogLevel) {
l.level = level
}
// SetColorEnabled adds color escape-sequences to the logging output
func (l *Logger) SetColorEnabled(b bool) {
if runtime.GOOS == "windows" {
b = false
}
l.colorEnabled = b
}
// Info logs an info message
func (l *Logger) Info(msg string) {
if l == nil || l.level < LogLevelInfo {
return
}
l.print(LogLevelInfo, msg)
}
// Infof formats and then logs an info message
func (l *Logger) Infof(fmtMsg string, args ...interface{}) {
l.Info(fmt.Sprintf(fmtMsg, args...))
}
// Warning logs a warning message
func (l *Logger) Warning(msg string) {
if l == nil || l.level < LogLevelWarning {
return
}
l.print(LogLevelWarning, msg)
}
// Warningf formats and then logs a warning message
func (l *Logger) Warningf(fmtMsg string, args ...interface{}) {
l.Warning(fmt.Sprintf(fmtMsg, args...))
}
// Error logs an error message
func (l *Logger) Error(msg string) {
if l == nil || l.level < LogLevelError {
return
}
l.print(LogLevelError, msg)
}
// Errorf formats and then logs a error message
func (l *Logger) Errorf(fmtMsg string, args ...interface{}) {
l.Error(fmt.Sprintf(fmtMsg, args...))
}
// Debug logs a debug message
func (l *Logger) Debug(msg string) {
if l == nil || l.level < LogLevelDebug {
return
}
l.print(LogLevelDebug, msg)
}
// Debugf formats and then logs a debug message
func (l *Logger) Debugf(fmtMsg string, args ...interface{}) {
l.Debug(fmt.Sprintf(fmtMsg, args...))
}
func (l *Logger) print(level LogLevel, msg string) {
if l == nil || l.level < level {
return
}
colors := map[LogLevel]int{
LogLevelDebug: cyan,
LogLevelInfo: green,
LogLevelWarning: yellow,
LogLevelError: red,
}
fmtString := ""
if l.prefix != "" {
if l.colorEnabled {
fmtString = fmt.Sprintf(fmtColorEscape, magenta)
}
fmtString += fmt.Sprintf(fmtPrefix, l.prefix)
}
if l.colorEnabled {
fmtString += fmt.Sprintf(fmtColorEscape, colors[level])
}
switch level {
case LogLevelDebug:
fmtString += LogFmtDebug
case LogLevelInfo:
fmtString += LogFmtInfo
case LogLevelWarning:
fmtString += LogFmtWarning
case LogLevelError:
fmtString += LogFmtError
case LogLevelNone:
default:
return
}
_, err := l.Write(format(fmtString, []byte(msg)))
if err != nil {
log.Print(err)
}
}
func format(fmtStr string, fmtInput []byte) []byte {
return []byte(fmt.Sprintf(fmtStr, string(fmtInput)))
}
func (l *Logger) Write(p []byte) (n int, err error) {
return l.Writer.Write(p)
}