3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-23 03:02:48 +01:00

optimize IsLoggingRawIO

This commit is contained in:
Shivaram Lingamneni 2018-05-01 06:00:09 -04:00
parent 3150f4e23b
commit be588076e9

View File

@ -12,6 +12,7 @@ import (
"strings" "strings"
"sync" "sync"
"sync/atomic"
colorable "github.com/mattn/go-colorable" colorable "github.com/mattn/go-colorable"
"github.com/mgutz/ansi" "github.com/mgutz/ansi"
@ -57,7 +58,7 @@ type Manager struct {
loggers []singleLogger loggers []singleLogger
stdoutWriteLock sync.Mutex // use one lock for both stdout and stderr stdoutWriteLock sync.Mutex // use one lock for both stdout and stderr
fileWriteLock sync.Mutex fileWriteLock sync.Mutex
loggingRawIO bool loggingRawIO uint32
} }
// LoggingConfig represents the configuration of a single logger. // LoggingConfig represents the configuration of a single logger.
@ -95,7 +96,7 @@ func (logger *Manager) ApplyConfig(config []LoggingConfig) error {
} }
logger.loggers = nil logger.loggers = nil
logger.loggingRawIO = false atomic.StoreUint32(&logger.loggingRawIO, 0)
// for safety, this deep-copies all mutable data in `config` // for safety, this deep-copies all mutable data in `config`
// XXX let's keep it that way // XXX let's keep it that way
@ -124,7 +125,7 @@ func (logger *Manager) ApplyConfig(config []LoggingConfig) error {
fileWriteLock: &logger.fileWriteLock, fileWriteLock: &logger.fileWriteLock,
} }
if typeMap["userinput"] || typeMap["useroutput"] || (typeMap["*"] && !(excludedTypeMap["userinput"] && excludedTypeMap["useroutput"])) { if typeMap["userinput"] || typeMap["useroutput"] || (typeMap["*"] && !(excludedTypeMap["userinput"] && excludedTypeMap["useroutput"])) {
logger.loggingRawIO = true atomic.StoreUint32(&logger.loggingRawIO, 1)
} }
if sLogger.MethodFile.Enabled { if sLogger.MethodFile.Enabled {
file, err := os.OpenFile(sLogger.MethodFile.Filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) 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. // IsLoggingRawIO returns true if raw user input and output is being logged.
func (logger *Manager) IsLoggingRawIO() bool { func (logger *Manager) IsLoggingRawIO() bool {
logger.configMutex.RLock() return atomic.LoadUint32(&logger.loggingRawIO) == 1
defer logger.configMutex.RUnlock()
return logger.loggingRawIO
} }
// Log logs the given message with the given details. // Log logs the given message with the given details.