-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrotate_io_writer.go
37 lines (32 loc) · 1 KB
/
rotate_io_writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package writer
import (
"io"
"sync"
)
// RotateIoWriter will rotate the io.writer when rotate is called
// it delegates the io.writer creation to the Provider
type RotateIoWriter struct {
mutex sync.Mutex
writerProvider Provider
writer io.Writer
}
// Write will pass-through data to the underlying io.writer
func (w *RotateIoWriter) Write(output []byte) (int, error) {
w.mutex.Lock()
defer w.mutex.Unlock()
return w.writer.Write(output)
}
// Rotate will ask the provider to change the underlying io.writer with a new one
func (w *RotateIoWriter) Rotate() (err error) {
w.mutex.Lock()
defer w.mutex.Unlock()
w.writer, err = w.writerProvider(w.writer)
return err
}
// NewRotateIoWriter will create a RotateIoWriter
// create a new io.Writer that can be changed other the time when Rotate() is called
func NewRotateIoWriter(provider Provider) (*RotateIoWriter, error) {
// create new io.Writer
writer, err := provider(nil)
return &RotateIoWriter{writer: writer, writerProvider: provider}, err
}