-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.go
86 lines (69 loc) · 1.46 KB
/
point.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
// Point represents data accumulated during last log check.
type Point struct {
prevSize int64
size int64
diff int64
lines []string
linesQty int
reader *bufio.Reader
file *os.File
}
// NewPoint returns a new Point object.
func NewPoint(f *os.File, r *bufio.Reader, prevSize int64) *Point {
return &Point{
file: f,
prevSize: prevSize,
reader: r,
}
}
// GetChange checks log file for size changes and reads added data into log entry strings.
func (p *Point) GetChange() error {
stat, err := p.file.Stat()
if err != nil {
return err
}
p.size = stat.Size()
p.diff = p.size - p.prevSize
// If truncated, adjust for a new size and continue from beginning.
if p.diff > 0 {
p.lines, err = readIncrement(p.reader)
if err != nil {
return fmt.Errorf(" Error reading log chunk: %s ", err.Error())
}
p.linesQty = len(p.lines)
}
p.prevSize = p.size
return nil
}
// readIncrement reads a log file from a start position to EOF
// returning result as a slice of (string) log entries.
func readIncrement(r *bufio.Reader) ([]string, error) {
var data []byte
var err error
var out []string
for {
data, err = r.ReadBytes('\n')
if err == nil || err == io.EOF {
line := strings.TrimSpace(string(data))
if line != "" {
out = append(out, line)
}
}
if err != nil {
if err != io.EOF {
return out, err
}
// EOF
break
}
}
return out, nil
}