forked from cloudfoundry/gorouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_sink.go
67 lines (49 loc) · 921 Bytes
/
io_sink.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
package gosteno
import (
"bufio"
"os"
"sync"
)
type IOSink struct {
writer *bufio.Writer
codec Codec
file *os.File
sync.Mutex
}
func NewIOSink(file *os.File) *IOSink {
writer := bufio.NewWriter(file)
x := new(IOSink)
x.writer = writer
x.file = file
return x
}
func NewFileSink(path string) *IOSink {
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
panic(err)
}
return NewIOSink(file)
}
func (x *IOSink) AddRecord(record *Record) {
bytes, _ := x.codec.EncodeRecord(record)
x.Lock()
defer x.Unlock()
x.writer.Write(bytes)
// Need to append a newline for IO sink
x.writer.WriteString("\n")
}
func (x *IOSink) Flush() {
x.Lock()
defer x.Unlock()
x.writer.Flush()
}
func (x *IOSink) SetCodec(codec Codec) {
x.Lock()
defer x.Unlock()
x.codec = codec
}
func (x *IOSink) GetCodec() Codec {
x.Lock()
defer x.Unlock()
return x.codec
}