3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

logger: Enable Windows support, properly lock writing to stderr

This commit is contained in:
Daniel Oaks 2017-03-10 12:27:05 +10:00
parent 62c3159ce1
commit 7bb53af44e

View File

@ -11,6 +11,9 @@ import (
"strings" "strings"
"sync"
colorable "github.com/mattn/go-colorable"
"github.com/mgutz/ansi" "github.com/mgutz/ansi"
) )
@ -49,6 +52,7 @@ var (
// Logger is the main interface used to log debug/info/error messages. // Logger is the main interface used to log debug/info/error messages.
type Logger struct { type Logger struct {
loggers []SingleLogger loggers []SingleLogger
stderrWriteLock sync.Mutex
DumpingRawInOut bool DumpingRawInOut bool
} }
@ -63,9 +67,10 @@ func NewLogger(config []LoggingConfig) (*Logger, error) {
Enabled: logConfig.Methods["file"], Enabled: logConfig.Methods["file"],
Filename: logConfig.Filename, Filename: logConfig.Filename,
}, },
Level: logConfig.Level, Level: logConfig.Level,
Types: logConfig.Types, Types: logConfig.Types,
ExcludedTypes: logConfig.ExcludedTypes, ExcludedTypes: logConfig.ExcludedTypes,
stderrWriteLock: &logger.stderrWriteLock,
} }
if logConfig.Types["userinput"] || logConfig.Types["useroutput"] || (logConfig.Types["*"] && !(logConfig.ExcludedTypes["userinput"] && logConfig.ExcludedTypes["useroutput"])) { if logConfig.Types["userinput"] || logConfig.Types["useroutput"] || (logConfig.Types["*"] && !(logConfig.ExcludedTypes["userinput"] && logConfig.ExcludedTypes["useroutput"])) {
logger.DumpingRawInOut = true logger.DumpingRawInOut = true
@ -101,11 +106,12 @@ type fileMethod struct {
// SingleLogger represents a single logger instance. // SingleLogger represents a single logger instance.
type SingleLogger struct { type SingleLogger struct {
MethodSTDERR bool stderrWriteLock *sync.Mutex
MethodFile fileMethod MethodSTDERR bool
Level LogLevel MethodFile fileMethod
Types map[string]bool Level LogLevel
ExcludedTypes map[string]bool Types map[string]bool
ExcludedTypes map[string]bool
} }
// Log logs the given message with the given details. // Log logs the given message with the given details.
@ -161,7 +167,9 @@ func (logger *SingleLogger) Log(level LogLevel, logType string, messageParts ...
// output // output
if logger.MethodSTDERR { if logger.MethodSTDERR {
fmt.Fprintln(os.Stderr, fullStringFormatted) logger.stderrWriteLock.Lock()
fmt.Fprintln(colorable.NewColorableStderr(), fullStringFormatted)
logger.stderrWriteLock.Unlock()
} }
if logger.MethodFile.Enabled { if logger.MethodFile.Enabled {
logger.MethodFile.Writer.WriteString(fullStringRaw + "\n") logger.MethodFile.Writer.WriteString(fullStringRaw + "\n")