From be588076e9f4318cc48f1770e4446d4187fa9fdd Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Tue, 1 May 2018 06:00:09 -0400 Subject: [PATCH] optimize IsLoggingRawIO --- irc/logger/logger.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/irc/logger/logger.go b/irc/logger/logger.go index 2e9f2f05..48cbb097 100644 --- a/irc/logger/logger.go +++ b/irc/logger/logger.go @@ -12,6 +12,7 @@ import ( "strings" "sync" + "sync/atomic" colorable "github.com/mattn/go-colorable" "github.com/mgutz/ansi" @@ -57,7 +58,7 @@ type Manager struct { loggers []singleLogger stdoutWriteLock sync.Mutex // use one lock for both stdout and stderr fileWriteLock sync.Mutex - loggingRawIO bool + loggingRawIO uint32 } // LoggingConfig represents the configuration of a single logger. @@ -95,7 +96,7 @@ func (logger *Manager) ApplyConfig(config []LoggingConfig) error { } logger.loggers = nil - logger.loggingRawIO = false + atomic.StoreUint32(&logger.loggingRawIO, 0) // for safety, this deep-copies all mutable data in `config` // XXX let's keep it that way @@ -124,7 +125,7 @@ func (logger *Manager) ApplyConfig(config []LoggingConfig) error { fileWriteLock: &logger.fileWriteLock, } if typeMap["userinput"] || typeMap["useroutput"] || (typeMap["*"] && !(excludedTypeMap["userinput"] && excludedTypeMap["useroutput"])) { - logger.loggingRawIO = true + atomic.StoreUint32(&logger.loggingRawIO, 1) } if sLogger.MethodFile.Enabled { file, err := os.OpenFile(sLogger.MethodFile.Filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) @@ -143,9 +144,7 @@ func (logger *Manager) ApplyConfig(config []LoggingConfig) error { // IsLoggingRawIO returns true if raw user input and output is being logged. func (logger *Manager) IsLoggingRawIO() bool { - logger.configMutex.RLock() - defer logger.configMutex.RUnlock() - return logger.loggingRawIO + return atomic.LoadUint32(&logger.loggingRawIO) == 1 } // Log logs the given message with the given details.