-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathbuffer.go
52 lines (40 loc) · 1 KB
/
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
package kail
import "bytes"
const bufferMaxRetainSize = logBufsiz
type buffer interface {
process([]byte) []Event
}
type _buffer struct {
source EventSource
prev *bytes.Buffer
}
func newBuffer(source EventSource) buffer {
return &_buffer{source, new(bytes.Buffer)}
}
func (b *_buffer) process(log []byte) []Event {
var events []Event
for end := bytes.IndexRune(log, '\n'); end >= 0 && len(log) > 0; end = bytes.IndexRune(log, '\n') {
var ebuf []byte
if plen := b.prev.Len(); plen > 0 {
ebuf = make([]byte, plen+end)
copy(ebuf, b.prev.Bytes())
copy(ebuf[plen:], log[:end])
b.prev.Reset()
} else {
ebuf = make([]byte, end)
copy(ebuf, log[:end])
}
events = append(events, newEvent(b.source, ebuf))
log = log[end+1:]
}
if sz := len(log); sz > 0 {
b.prev.Write(log)
if plen := b.prev.Len(); plen >= bufferMaxRetainSize {
ebuf := make([]byte, plen)
copy(ebuf, b.prev.Bytes())
events = append(events, newEvent(b.source, ebuf))
b.prev.Reset()
}
}
return events
}