-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
tailer.go
72 lines (59 loc) · 1.1 KB
/
tailer.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
package tail
import (
"os"
"github.com/hpcloud/tail"
)
type followerImpl struct {
filename string
t *tail.Tail
line chan string
}
// NewFollower creates a new Follower instance for a given file (given by name)
func NewFileFollower(filename string) (Follower, error) {
f := &followerImpl{
filename: filename,
line: make(chan string),
}
if err := f.start(); err != nil {
return nil, err
}
return f, nil
}
func (f *followerImpl) start() error {
var seekInfo *tail.SeekInfo
_, err := os.Stat(f.filename)
if err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
seekInfo = &tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}
}
t, err := tail.TailFile(f.filename, tail.Config{
Follow: true,
ReOpen: true,
Poll: true,
Location: seekInfo,
})
if err != nil {
return err
}
f.t = t
return nil
}
func (f *followerImpl) OnError(cb func(error)) {
go func() {
err := f.t.Wait()
if err != nil {
cb(err)
}
}()
}
func (f *followerImpl) Lines() chan string {
go func() {
for n := range f.t.Lines {
f.line <- n.Text
}
}()
return f.line
}