This repository has been archived on 2022-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
2018-06-06 15:30:23 +09:00
|
|
|
package rotater
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"amuz.es/src/infra/goutils/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LockedWriteSyncer struct {
|
|
|
|
sync.Mutex
|
|
|
|
ws logger.WriteSyncer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock wraps a WriteSyncer in a mutex to make it safe for concurrent use. In
|
|
|
|
// particular, *os.Files must be locked before use.
|
2018-06-07 00:48:38 +09:00
|
|
|
func NewLocked(ws logger.WriteSyncer) logger.RotateSyncer {
|
2018-06-06 15:30:23 +09:00
|
|
|
if lws, ok := ws.(*LockedWriteSyncer); ok {
|
|
|
|
// no need to layer on another lock
|
|
|
|
return lws
|
|
|
|
}
|
|
|
|
return &LockedWriteSyncer{ws: ws}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LockedWriteSyncer) Write(bs []byte) (int, error) {
|
|
|
|
s.Lock()
|
|
|
|
n, err := s.ws.Write(bs)
|
|
|
|
s.Unlock()
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LockedWriteSyncer) Sync() error {
|
|
|
|
s.Lock()
|
|
|
|
err := s.ws.Sync()
|
|
|
|
s.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-12 08:45:20 +09:00
|
|
|
func (r *LockedWriteSyncer) SetOnClose(closeFunc func()) {}
|
|
|
|
func (r *LockedWriteSyncer) Rotate() (err error) { return }
|
|
|
|
func (r *LockedWriteSyncer) Close() (err error) { return }
|