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
1 changed files with 17 additions and 9 deletions

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
} }
@ -66,6 +70,7 @@ func NewLogger(config []LoggingConfig) (*Logger, error) {
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,6 +106,7 @@ type fileMethod struct {
// SingleLogger represents a single logger instance. // SingleLogger represents a single logger instance.
type SingleLogger struct { type SingleLogger struct {
stderrWriteLock *sync.Mutex
MethodSTDERR bool MethodSTDERR bool
MethodFile fileMethod MethodFile fileMethod
Level LogLevel Level LogLevel
@ -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")