-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilenotifier.go
147 lines (128 loc) · 2.89 KB
/
filenotifier.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
package ueloghandler
import (
"bufio"
"context"
"errors"
"io"
"io/fs"
"os"
"strings"
"time"
)
var ErrFileRemoved = errors.New("ueLogHandler:File removed")
type FileNotifier struct {
logs chan string
readBytes int64
sb strings.Builder
basicFormatSection bool
watchInterval time.Duration
filePath string
}
func NewFileNotifier(filePath string, watchInterval time.Duration) *FileNotifier {
wacher := &FileNotifier{logs: make(chan string), watchInterval: watchInterval, filePath: filePath}
return wacher
}
func (f *FileNotifier) Flush() error {
err := f.read(f.filePath)
f.sendUnsentLog()
return err
}
// sendUnsentLog
//
// The log is sent when the next log is started.
// Therefore, there may be an unsent log when the Watch method ends.
func (f *FileNotifier) sendUnsentLog() {
if f.sb.Len() > 0 {
f.logs <- f.sb.String()
f.sb.Reset()
}
f.readBytes = 0
f.sb.Reset()
f.basicFormatSection = false
}
func (f *FileNotifier) Logs() chan string {
return f.logs
}
// Subscribe Start watching log file and send logs to Logs channel
func (f *FileNotifier) Subscribe(ctx context.Context) error {
f.sendUnsentLog()
fstat, err := os.Stat(f.filePath)
if err != nil {
return err
}
latestModTime := fstat.ModTime()
ticker := time.NewTicker(f.watchInterval)
for {
select {
case <-ticker.C:
fstat, err := os.Stat(f.filePath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return ErrFileRemoved
}
return err
}
if !fstat.ModTime().Equal(latestModTime) {
latestModTime = fstat.ModTime()
err = f.read(f.filePath)
if err != nil {
return err
}
}
case <-ctx.Done():
return nil
}
}
}
func (f *FileNotifier) read(filePath string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
fs, err := file.Stat()
if err != nil {
return err
}
if fileRecreated := fs.Size() < f.readBytes; fileRecreated {
f.sendUnsentLog()
}
_, err = file.Seek(f.readBytes, io.SeekStart)
if err != nil {
return err
}
bufreader := bufio.NewReader(file)
for {
lineData, err := bufreader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
return nil
}
return err
}
f.readBytes += int64(len(lineData))
lineStr := string(lineData)
lineStr = ToUTF8_LF(lineStr)
logInfo := NewLog(lineStr)
startBasicFormatLog := logInfo.Category != "" || logInfo.Time != ""
if f.basicFormatSection {
// The log may output multiple lines.
// Therefore, when the start of the next log output is detected, the string up to that point is as a log.
if startBasicFormatLog {
f.logs <- f.sb.String()
f.sb.Reset()
f.sb.WriteString(lineStr)
} else {
f.sb.WriteString(lineStr)
}
} else {
if startBasicFormatLog {
f.sb.Reset()
f.sb.WriteString(lineStr)
f.basicFormatSection = true
} else {
f.logs <- lineStr
}
}
}
}