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

logger: Make safer to avoid file write races

This commit is contained in:
Daniel Oaks 2017-03-26 21:41:52 +10:00
parent 861b65eb39
commit a8e13b062b

View File

@ -53,6 +53,7 @@ var (
type Manager struct { type Manager struct {
loggers []singleLogger loggers []singleLogger
stderrWriteLock sync.Mutex stderrWriteLock sync.Mutex
fileWriteLock sync.Mutex
DumpingRawInOut bool DumpingRawInOut bool
} }
@ -93,6 +94,7 @@ func NewManager(config ...Config) (*Manager, error) {
Types: typeMap, Types: typeMap,
ExcludedTypes: excludedTypeMap, ExcludedTypes: excludedTypeMap,
stderrWriteLock: &logger.stderrWriteLock, stderrWriteLock: &logger.stderrWriteLock,
fileWriteLock: &logger.fileWriteLock,
} }
if typeMap["userinput"] || typeMap["useroutput"] || (typeMap["*"] && !(excludedTypeMap["userinput"] && excludedTypeMap["useroutput"])) { if typeMap["userinput"] || typeMap["useroutput"] || (typeMap["*"] && !(excludedTypeMap["userinput"] && excludedTypeMap["useroutput"])) {
logger.DumpingRawInOut = true logger.DumpingRawInOut = true
@ -164,6 +166,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 stderrWriteLock *sync.Mutex
fileWriteLock *sync.Mutex
MethodSTDERR bool MethodSTDERR bool
MethodFile fileMethod MethodFile fileMethod
Level Level Level Level
@ -229,6 +232,8 @@ func (logger *singleLogger) Log(level Level, logType string, messageParts ...str
logger.stderrWriteLock.Unlock() logger.stderrWriteLock.Unlock()
} }
if logger.MethodFile.Enabled { if logger.MethodFile.Enabled {
logger.fileWriteLock.Lock()
logger.MethodFile.Writer.WriteString(fullStringRaw + "\n") logger.MethodFile.Writer.WriteString(fullStringRaw + "\n")
logger.fileWriteLock.Unlock()
} }
} }