-
Notifications
You must be signed in to change notification settings - Fork 1
/
tail.go
163 lines (142 loc) · 3.32 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
package tail
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
"time"
)
type Tail struct {
fileName string
file *os.File
stat os.FileInfo
reader *bufio.Reader
config *Config
}
type Config struct {
// How frequently polling a file for a changes
PollInterval time.Duration
// How long to wait for a deleted file before stop polling.
// StopPollingTimeout should be greater than PollInterval,
// for cases when file was deleted and than created again
StopPollingTimeout time.Duration
}
func (c *Config) Validate() error {
switch {
case c.PollInterval <= 0:
return fmt.Errorf("PollInterval must be > 0")
case c.StopPollingTimeout <= 0:
return fmt.Errorf("StopPollingTimeout must be > 0")
}
return nil
}
// NewConfig return config with default values
func NewConfig() *Config {
c := Config{
PollInterval: time.Second * 1,
StopPollingTimeout: time.Second * 5,
}
return &c
}
func NewTail(fileName string, offset int64, config *Config) (tail Tail, err error) {
if config == nil {
config = NewConfig()
}
err = config.Validate()
if err != nil {
return tail, err
}
tail.config = config
tail.fileName = fileName
tail.file, err = os.Open(fileName)
if err != nil {
return tail, fmt.Errorf("failed to open file %s: %s", fileName, err)
}
tail.stat, err = tail.file.Stat()
if err != nil {
return tail, fmt.Errorf("failed to stat file %s: %s", fileName, err)
}
if offset != 0 {
_, err = tail.file.Seek(offset, io.SeekStart)
if err != nil {
return tail, fmt.Errorf("failed to seek file %s: %s", fileName, err)
}
}
tail.reader = bufio.NewReader(tail.file)
return tail, nil
}
func (tail *Tail) ReadLine() (string, error) {
var linePart string
for {
line, err := tail.reader.ReadString('\n')
if err == nil {
if linePart != "" {
line = linePart + line
linePart = ""
}
return strings.TrimRight(line, "\n"), nil
}
linePart = line
changesErr := tail.waitForChanges()
if changesErr != nil {
return "", changesErr
}
}
}
func (tail *Tail) waitForChanges() error {
var stat os.FileInfo
var err error
lastSuccessfulRead := time.Now()
for {
time.Sleep(tail.config.PollInterval)
stat, err = os.Stat(tail.fileName)
if err != nil {
if time.Since(lastSuccessfulRead) > tail.config.StopPollingTimeout {
log.Printf("failed to stat file %s: %s, stop tailing", tail.fileName, err)
tail.file.Close()
return err
}
continue
}
lastSuccessfulRead = time.Now()
if !os.SameFile(tail.stat, stat) {
log.Printf("file was moved %s", tail.fileName)
tail.file.Close()
tail.file, err = os.Open(tail.fileName)
if err != nil {
log.Printf("failed to open file %s: %s", tail.fileName, err)
continue
}
tail.reader = bufio.NewReader(tail.file)
break
}
if stat.Size() < tail.stat.Size() {
log.Printf("file was truncated %s", tail.fileName)
_, err = tail.file.Seek(0, io.SeekStart)
if err != nil {
log.Printf("failed to seek file %s: %s", tail.fileName, err)
continue
}
break
}
if stat.Size() > tail.stat.Size() {
break
}
}
tail.stat = stat
return nil
}
func (tail *Tail) Close() {
if tail.file != nil {
tail.file.Close()
}
}
func (tail *Tail) Offset() (int64, error) {
offset, err := tail.file.Seek(0, io.SeekCurrent)
if err != nil {
return 0, err
}
return offset - int64(tail.reader.Buffered()), nil
}