diff options
Diffstat (limited to 'vendor/github.com/go-kit/kit/log/sync.go')
| -rw-r--r-- | vendor/github.com/go-kit/kit/log/sync.go | 89 |
1 files changed, 5 insertions, 84 deletions
diff --git a/vendor/github.com/go-kit/kit/log/sync.go b/vendor/github.com/go-kit/kit/log/sync.go index c07cdfa..bcfee2b 100644 --- a/vendor/github.com/go-kit/kit/log/sync.go +++ b/vendor/github.com/go-kit/kit/log/sync.go @@ -2,8 +2,8 @@ package log import ( "io" - "sync" - "sync/atomic" + + "github.com/go-kit/log" ) // SwapLogger wraps another logger that may be safely replaced while other @@ -12,29 +12,7 @@ import ( // // SwapLogger serves well as a package global logger that can be changed by // importers. -type SwapLogger struct { - logger atomic.Value -} - -type loggerStruct struct { - Logger -} - -// Log implements the Logger interface by forwarding keyvals to the currently -// wrapped logger. It does not log anything if the wrapped logger is nil. -func (l *SwapLogger) Log(keyvals ...interface{}) error { - s, ok := l.logger.Load().(loggerStruct) - if !ok || s.Logger == nil { - return nil - } - return s.Log(keyvals...) -} - -// Swap replaces the currently wrapped logger with logger. Swap may be called -// concurrently with calls to Log from other goroutines. -func (l *SwapLogger) Swap(logger Logger) { - l.logger.Store(loggerStruct{logger}) -} +type SwapLogger = log.SwapLogger // NewSyncWriter returns a new writer that is safe for concurrent use by // multiple goroutines. Writes to the returned writer are passed on to w. If @@ -47,55 +25,7 @@ func (l *SwapLogger) Swap(logger Logger) { // Fd() uintptr // } func NewSyncWriter(w io.Writer) io.Writer { - switch w := w.(type) { - case fdWriter: - return &fdSyncWriter{fdWriter: w} - default: - return &syncWriter{Writer: w} - } -} - -// syncWriter synchronizes concurrent writes to an io.Writer. -type syncWriter struct { - sync.Mutex - io.Writer -} - -// Write writes p to the underlying io.Writer. If another write is already in -// progress, the calling goroutine blocks until the syncWriter is available. -func (w *syncWriter) Write(p []byte) (n int, err error) { - w.Lock() - n, err = w.Writer.Write(p) - w.Unlock() - return n, err -} - -// fdWriter is an io.Writer that also has an Fd method. The most common -// example of an fdWriter is an *os.File. -type fdWriter interface { - io.Writer - Fd() uintptr -} - -// fdSyncWriter synchronizes concurrent writes to an fdWriter. -type fdSyncWriter struct { - sync.Mutex - fdWriter -} - -// Write writes p to the underlying io.Writer. If another write is already in -// progress, the calling goroutine blocks until the fdSyncWriter is available. -func (w *fdSyncWriter) Write(p []byte) (n int, err error) { - w.Lock() - n, err = w.fdWriter.Write(p) - w.Unlock() - return n, err -} - -// syncLogger provides concurrent safe logging for another Logger. -type syncLogger struct { - mu sync.Mutex - logger Logger + return log.NewSyncWriter(w) } // NewSyncLogger returns a logger that synchronizes concurrent use of the @@ -103,14 +33,5 @@ type syncLogger struct { // only one goroutine will be allowed to log to the wrapped logger at a time. // The other goroutines will block until the logger is available. func NewSyncLogger(logger Logger) Logger { - return &syncLogger{logger: logger} -} - -// Log logs keyvals to the underlying Logger. If another log is already in -// progress, the calling goroutine blocks until the syncLogger is available. -func (l *syncLogger) Log(keyvals ...interface{}) error { - l.mu.Lock() - err := l.logger.Log(keyvals...) - l.mu.Unlock() - return err + return log.NewSyncLogger(logger) } |
