amuz.es/src/infra/goutils@v0.1.3/logger/rotater/wrapped.go (about) 1 package rotater 2 3 import ( 4 "sync" 5 "amuz.es/src/infra/goutils/logger" 6 ) 7 8 type LockedWriteSyncer struct { 9 sync.Mutex 10 ws logger.WriteSyncer 11 } 12 13 // Lock wraps a WriteSyncer in a mutex to make it safe for concurrent use. In 14 // particular, *os.Files must be locked before use. 15 func NewLocked(ws logger.WriteSyncer) logger.RotateSyncer { 16 if lws, ok := ws.(*LockedWriteSyncer); ok { 17 // no need to layer on another lock 18 return lws 19 } 20 return &LockedWriteSyncer{ws: ws} 21 } 22 23 func (s *LockedWriteSyncer) Write(bs []byte) (int, error) { 24 s.Lock() 25 n, err := s.ws.Write(bs) 26 s.Unlock() 27 return n, err 28 } 29 30 func (s *LockedWriteSyncer) Sync() error { 31 s.Lock() 32 err := s.ws.Sync() 33 s.Unlock() 34 return err 35 } 36 37 func (r *LockedWriteSyncer) SetOnClose(closeFunc func()) {} 38 func (r *LockedWriteSyncer) Rotate() (err error) { return } 39 func (r *LockedWriteSyncer) Close() (err error) { return }