-
Notifications
You must be signed in to change notification settings - Fork 3
/
reopening_writer.go
57 lines (49 loc) · 1.13 KB
/
reopening_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"os"
"sync"
)
type ReOpeningWriter struct {
fp *os.File
lock sync.Mutex
filename string // should be set to the actual filename
}
// Make a new ReOpeningWriter. Return nil if error occurs during setup.
func NewReOpeningWriter(filename string) *ReOpeningWriter {
w := ReOpeningWriter{filename: filename}
err := w.ReOpen()
if err != nil {
fmt.Println(err)
return nil
}
return &w
}
// Write satisfies the io.Writer interface.
func (w *ReOpeningWriter) Write(output []byte) (int, error) {
w.lock.Lock()
defer w.lock.Unlock()
w.CheckReOpen()
return w.fp.Write(output)
}
// Perform the actual act of rotating and reopening file.
func (w *ReOpeningWriter) ReOpen() (err error) {
// Close existing file if open
if w.fp != nil {
err = w.fp.Close()
w.fp = nil
if err != nil {
return
}
}
// Open file for appending
w.fp, err = os.OpenFile(w.filename, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
return
}
func (w *ReOpeningWriter) CheckReOpen() (err error) {
// Reopen dest file if it has been rotated
if _, err := os.Stat(w.filename); os.IsNotExist(err) {
err = w.ReOpen()
}
return err
}