-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogfile.go
55 lines (45 loc) · 1.21 KB
/
logfile.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
package graphblast
import (
"fmt"
"io"
"strings"
)
type LogFile struct {
Values map[string]string
Layout string // the layout to use (interpreted by JS)
Label string // the label of the display
Window int // the number of lines to retain
Colors string // the colors to use when displaying the graph
FontSize string // the CSS font size to use when displaying the graph
Count int // the number of values encountered so far
Filtered int // the number of values filtered out so far
Errors int // the number of values skipped due to errors so far
}
func NewLogFile() *LogFile {
return &LogFile{
Layout: "logfile",
Window: 100,
Values: make(map[string]string, 1024)}
}
func (lf *LogFile) Changed(indicator int) (bool, int) {
if lf.Count <= indicator {
return false, indicator
}
return true, lf.Count
}
func (lf *LogFile) Add(line string, err error) {
if err != nil {
lf.Errors += 1
return
}
lf.Values[fmt.Sprintf("%v", lf.Count)] = line
lf.Count += 1
if len(lf.Values) > lf.Window {
delete(lf.Values, fmt.Sprintf("%v", lf.Count-lf.Window-1))
}
}
func (lf *LogFile) Read(reader io.Reader) error {
return doRead(reader, func(line string) {
lf.Add(strings.TrimSpace(line), nil)
})
}