This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtail.go
183 lines (164 loc) · 3.57 KB
/
tail.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package dendrite
import (
"bufio"
"fmt"
"github.com/ActiveState/tail/watch"
"github.com/fizx/logs"
"io"
"launchpad.net/tomb"
"os"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"time"
)
type TimeProvider interface {
Now() time.Time
}
type SystemTimeProvider struct{}
func (*SystemTimeProvider) Now() time.Time {
return time.Now()
}
var StandardTimeProvider TimeProvider = new(SystemTimeProvider)
type Tail struct {
Path string
OffsetPath string
Watcher watch.FileWatcher
Parser Parser
maxBackfill int64
offset int64
handle *os.File
}
func NewTail(parser Parser, maxBackfill int64, path string, offsetPath string, offset int64) *Tail {
tail := new(Tail)
tail.Path = path
tail.offset = offset
tail.OffsetPath = offsetPath
tail.Parser = parser
tail.Watcher = watch.NewInotifyFileWatcher(path)
tail.LoadOffset()
tail.maxBackfill = maxBackfill
handle, err := os.Open(path)
if err != nil {
logs.Debug("Can't open file: ", path)
return nil
} else {
tail.handle = handle
}
tail.seek()
return tail
}
func (tail *Tail) Stat() (fi os.FileInfo, err error) {
return tail.handle.Stat()
}
func (tail *Tail) seek() {
fi, err := tail.Stat()
if err != nil {
logs.Error("Can't stat file: %s", err)
return
}
off := tail.Offset()
if tail.maxBackfill >= 0 {
if off < fi.Size()-tail.maxBackfill {
off = fi.Size() - tail.maxBackfill
}
}
_, err = tail.handle.Seek(off, 0)
if err != nil {
logs.Error("Can't seek file: %s", err)
return
}
}
func (tail *Tail) Offset() int64 {
return atomic.LoadInt64(&tail.offset)
}
func (tail *Tail) SetOffset(o int64) {
atomic.StoreInt64(&tail.offset, o)
}
func (tail *Tail) WriteOffset() {
path := filepath.Join(os.TempDir(), filepath.Base(tail.OffsetPath))
temp, err := os.Create(path)
if err != nil {
logs.Debug("Can't create tempfile:", err)
} else {
_, err := fmt.Fprintf(temp, "%d\n", tail.Offset())
if err != nil {
logs.Debug("Can't write to tempfile:", err)
temp.Close()
} else {
temp.Close()
err := os.Rename(path, tail.OffsetPath)
if err != nil {
logs.Debug("Rename failed:", err)
}
}
}
}
func (tail *Tail) LoadOffset() {
file, err := os.Open(tail.OffsetPath)
if err != nil {
tail.WriteOffset()
} else {
reader := bufio.NewReader(file)
str, err := reader.ReadString('\n')
if err != nil {
logs.Debug("Malformed offset file: ", err)
} else {
out, err := strconv.ParseInt(strings.TrimSpace(str), 10, 64)
if err != nil {
logs.Debug("Malformed offset file: ", err)
} else {
logs.Debug("Found offset: %d", out)
tail.SetOffset(out)
}
}
file.Close()
}
}
func (tail *Tail) StartWatching() {
go func() {
fi, err := tail.Stat()
if err != nil {
logs.Error("Can't stat file: %s", err)
return
}
c := tail.Watcher.ChangeEvents(&tomb.Tomb{}, fi)
go tail.pollChannel(c.Modified)
go tail.pollChannel(c.Truncated)
go tail.pollChannel(c.Deleted)
}()
}
func (tail *Tail) pollChannel(c chan bool) {
for _, ok := <-c; ok; {
tail.Poll()
}
}
func (tail *Tail) Poll() {
size := 16384
buffer := make([]byte, size)
for {
len, err := tail.handle.Read(buffer)
if err == io.EOF {
fi, err := tail.Stat()
if err != nil {
logs.Warn("Can't stat %s", err)
} else if fi.Size() < tail.Offset() {
logs.Warn("File truncated, resetting...")
tail.SetOffset(0)
tail.WriteOffset()
tail.seek()
}
return
} else if err != nil {
logs.Debug("read error: ", err)
return
} else {
tail.Parser.Consume(buffer[0:len], &tail.offset)
tail.WriteOffset()
}
}
}
func (tail *Tail) Close() {
tail.handle.Close()
}