Source File
sync.go
Belonging Package
github.com/go-kit/log
package logimport ()// SwapLogger wraps another logger that may be safely replaced while other// goroutines use the SwapLogger concurrently. The zero value for a SwapLogger// will discard all log events without error.//// 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 ( *SwapLogger) ( ...interface{}) error {, := .logger.Load().(loggerStruct)if ! || .Logger == nil {return nil}return .Log(...)}// Swap replaces the currently wrapped logger with logger. Swap may be called// concurrently with calls to Log from other goroutines.func ( *SwapLogger) ( Logger) {.logger.Store(loggerStruct{})}// 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// another write is already in progress, the calling goroutine blocks until// the writer is available.//// If w implements the following interface, so does the returned writer.//// interface {// Fd() uintptr// }func ( io.Writer) io.Writer {switch w := .(type) {case fdWriter:return &fdSyncWriter{fdWriter: }default:return &syncWriter{Writer: }}}// syncWriter synchronizes concurrent writes to an io.Writer.type syncWriter struct {sync.Mutexio.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 ( *syncWriter) ( []byte) ( int, error) {.Lock()defer .Unlock()return .Writer.Write()}// 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.WriterFd() uintptr}// fdSyncWriter synchronizes concurrent writes to an fdWriter.type fdSyncWriter struct {sync.MutexfdWriter}// 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 ( *fdSyncWriter) ( []byte) ( int, error) {.Lock()defer .Unlock()return .fdWriter.Write()}// syncLogger provides concurrent safe logging for another Logger.type syncLogger struct {mu sync.Mutexlogger Logger}// NewSyncLogger returns a logger that synchronizes concurrent use of the// wrapped logger. When multiple goroutines use the SyncLogger concurrently// 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 ( Logger) Logger {return &syncLogger{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 ( *syncLogger) ( ...interface{}) error {.mu.Lock()defer .mu.Unlock()return .logger.Log(...)}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |