3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 11:59:40 +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"
"sync"
colorable "github.com/mattn/go-colorable"
"github.com/mgutz/ansi"
)
@ -49,6 +52,7 @@ var (
// Logger is the main interface used to log debug/info/error messages.
type Logger struct {
loggers []SingleLogger
stderrWriteLock sync.Mutex
DumpingRawInOut bool
}
@ -66,6 +70,7 @@ func NewLogger(config []LoggingConfig) (*Logger, error) {
Level: logConfig.Level,
Types: logConfig.Types,
ExcludedTypes: logConfig.ExcludedTypes,
stderrWriteLock: &logger.stderrWriteLock,
}
if logConfig.Types["userinput"] || logConfig.Types["useroutput"] || (logConfig.Types["*"] && !(logConfig.ExcludedTypes["userinput"] && logConfig.ExcludedTypes["useroutput"])) {
logger.DumpingRawInOut = true
@ -101,6 +106,7 @@ type fileMethod struct {
// SingleLogger represents a single logger instance.
type SingleLogger struct {
stderrWriteLock *sync.Mutex
MethodSTDERR bool
MethodFile fileMethod
Level LogLevel
@ -161,7 +167,9 @@ func (logger *SingleLogger) Log(level LogLevel, logType string, messageParts ...
// output
if logger.MethodSTDERR {
fmt.Fprintln(os.Stderr, fullStringFormatted)
logger.stderrWriteLock.Lock()
fmt.Fprintln(colorable.NewColorableStderr(), fullStringFormatted)
logger.stderrWriteLock.Unlock()
}
if logger.MethodFile.Enabled {
logger.MethodFile.Writer.WriteString(fullStringRaw + "\n")