forked from morita-kuma/golog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appender_buffer.go
62 lines (47 loc) · 1.01 KB
/
appender_buffer.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
package golog
import (
"bytes"
"sync"
)
// ByteBufferAppender
type ByteBufferAppender struct {
bufferPool *sync.Pool
}
// Write implements Appender interface
func (appender *ByteBufferAppender) Write(data []byte) (n int, err error) {
if appender != nil {
// get buffer from bufferPool
buffer := appender.bufferPool.Get().(*bytes.Buffer)
// release
defer func() {
appender.bufferPool.Put(buffer)
}()
// write
buffer.Write(data)
buffer.WriteString("\n")
}
return 0, nil
}
// Close implements io.Closer
func (appender *ByteBufferAppender) Close() error {
return nil
}
// String implements stringer
func (appender *ByteBufferAppender) String() string {
if appender != nil {
buffer := appender.bufferPool.Get().(*bytes.Buffer)
return buffer.String()
}
return ""
}
// NewByteBufferAppender
func NewByteBufferAppender() *ByteBufferAppender {
bufferPool := &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
return &ByteBufferAppender{
bufferPool: bufferPool,
}
}