forked from AliyunContainerService/pouch
/
jsonfile_read.go
58 lines (47 loc) · 1.27 KB
/
jsonfile_read.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
package jsonfile
import (
"os"
"github.com/alibaba/pouch/daemon/logger"
)
// ReadLogMessages will create goroutine to read the log message and send it to
// LogWatcher.
func (lf *JSONLogFile) ReadLogMessages(cfg *logger.ReadConfig) *logger.LogWatcher {
watcher := logger.NewLogWatcher()
go func() {
// NOTE: We cannot close the channel in the JSONLogFile.read
// function because we cannot guarantee that watcher will be
// close the channel. Since the watcher is created in the
// JSONLogFile.ReadLogMessages, we make sure that watcher.Msgs
// can be closed after the JSONLogFile.read.
defer close(watcher.Msgs)
lf.read(cfg, watcher)
}()
return watcher
}
func (lf *JSONLogFile) read(cfg *logger.ReadConfig, watcher *logger.LogWatcher) {
lf.mu.Lock()
f, err := os.Open(lf.f.Name())
lf.mu.Unlock()
if err != nil {
watcher.Err <- err
return
}
defer f.Close()
// find the offset if the config contains the valid tail lines
if cfg.Tail > 0 {
offset, err := seekOffsetByTailLines(f, cfg.Tail)
if err != nil {
watcher.Err <- err
return
}
if _, err := f.Seek(offset, os.SEEK_SET); err != nil {
watcher.Err <- err
return
}
}
tailFile(f, cfg, newUnmarshal, watcher)
if !cfg.Follow {
return
}
followFile(f, cfg, newUnmarshal, watcher)
}