optimize IsLoggingRawIO

This commit is contained in:
Shivaram Lingamneni 2018-05-01 06:00:09 -04:00
parent 3150f4e23b
commit be588076e9
1 changed files with 5 additions and 6 deletions

View File

@ -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.