-
Notifications
You must be signed in to change notification settings - Fork 11
/
buffered_writer.go
105 lines (94 loc) · 1.89 KB
/
buffered_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package timber
import (
"bufio"
"fmt"
"io"
"time"
)
type flusher interface {
Flush() error
}
// Use this of you need some buffering, or not
type BufferedWriter struct {
buf *bufio.Writer
writer io.WriteCloser
mc chan string
fc chan int
autoFlush *time.Ticker
closeChan chan bool
closedChan chan bool
}
func NewBufferedWriter(writer io.WriteCloser) (*BufferedWriter, error) {
bw := new(BufferedWriter)
bw.writer = writer
bw.buf = bufio.NewWriter(writer)
bw.mc = make(chan string)
bw.fc = make(chan int)
bw.autoFlush = time.NewTicker(time.Second)
bw.closeChan = make(chan bool)
bw.closedChan = make(chan bool)
go bw.writeLoop()
return bw, nil
}
func (bw *BufferedWriter) writeLoop() {
for {
select {
case msg := <-bw.mc:
bw.writeMessage(msg)
case <-bw.fc:
bw.flush()
case <-bw.autoFlush.C:
bw.flush()
case <-bw.closeChan:
// close requested. drain message queue and exit
for {
select {
case msg := <-bw.mc:
bw.writeMessage(msg)
default:
bw.flush()
bw.writer.Close()
close(bw.closedChan)
return
}
}
}
}
}
func (bw *BufferedWriter) writeMessage(msg string) {
_, err := bw.buf.Write([]byte(msg))
if err != nil {
// uh-oh... what do i do if logging fails; punt!
fmt.Printf("TIMBER! epic fail: %v", err)
}
}
// perform actual flush. only on writeLoop goroutine
func (bw *BufferedWriter) flush() {
// flush buffer
bw.buf.Flush()
// flush underlying buffer if supported
if f, ok := bw.writer.(flusher); ok {
f.Flush()
}
}
func (bw *BufferedWriter) LogWrite(msg string) {
select {
case <-bw.closedChan:
// writer is closed. messages are discarded
case bw.mc <- msg:
}
}
// Force flush the buffer
func (bw *BufferedWriter) Flush() error {
bw.fc <- 1
return nil
}
func (bw *BufferedWriter) Close() {
for {
select {
case bw.closeChan <- true:
case <-bw.closedChan:
return
}
}
}