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
184
185
186
187
188
189
190
191
192
193
|
// SPDX-License-Identifier: GPL-3.0-or-later
package logs
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"github.com/netdata/netdata/go/go.d.plugin/logger"
)
const (
maxEOF = 60
)
var (
ErrNoMatchedFile = errors.New("no matched files")
)
// Reader is a log rotate aware Reader
// TODO: better reopen algorithm
// TODO: handle truncate
type Reader struct {
file *os.File
path string
excludePath string
eofCounter int
continuousEOF int
log *logger.Logger
}
// Open a file and seek to end of the file.
// path: the shell file name pattern
// excludePath: the shell file name pattern
func Open(path string, excludePath string, log *logger.Logger) (*Reader, error) {
var err error
if path, err = filepath.Abs(path); err != nil {
return nil, err
}
if _, err = filepath.Match(path, "/"); err != nil {
return nil, fmt.Errorf("bad path syntax: %q", path)
}
if _, err = filepath.Match(excludePath, "/"); err != nil {
return nil, fmt.Errorf("bad exclude_path syntax: %q", path)
}
r := &Reader{
path: path,
excludePath: excludePath,
log: log,
}
if err = r.open(); err != nil {
return nil, err
}
return r, nil
}
// CurrentFilename get current opened file name
func (r *Reader) CurrentFilename() string {
return r.file.Name()
}
func (r *Reader) open() error {
path := r.findFile()
if path == "" {
r.log.Debugf("couldn't find log file, used path: '%s', exclude_path: '%s'", r.path, r.excludePath)
return ErrNoMatchedFile
}
r.log.Debug("open log file: ", path)
file, err := os.Open(path)
if err != nil {
return err
}
stat, err := file.Stat()
if err != nil {
return err
}
if _, err = file.Seek(stat.Size(), io.SeekStart); err != nil {
return err
}
r.file = file
return nil
}
func (r *Reader) Read(p []byte) (n int, err error) {
n, err = r.file.Read(p)
if err != nil {
switch {
case err == io.EOF:
err = r.handleEOFErr()
case errors.Is(err, os.ErrInvalid): // r.file is nil after Close
err = r.handleInvalidArgErr()
}
return
}
r.continuousEOF = 0
return
}
func (r *Reader) handleEOFErr() (err error) {
err = io.EOF
r.eofCounter++
r.continuousEOF++
if r.eofCounter < maxEOF || r.continuousEOF < 2 {
return err
}
if err2 := r.reopen(); err2 != nil {
err = err2
}
return err
}
func (r *Reader) handleInvalidArgErr() (err error) {
err = io.EOF
if err2 := r.reopen(); err2 != nil {
err = err2
}
return err
}
func (r *Reader) Close() (err error) {
if r == nil || r.file == nil {
return
}
r.log.Debug("close log file: ", r.file.Name())
err = r.file.Close()
r.file = nil
r.eofCounter = 0
return
}
func (r *Reader) reopen() error {
r.log.Debugf("reopen, look for: %s", r.path)
_ = r.Close()
return r.open()
}
func (r *Reader) findFile() string {
return find(r.path, r.excludePath)
}
func find(path, exclude string) string {
return finder{}.find(path, exclude)
}
// TODO: tests
type finder struct{}
func (f finder) find(path, exclude string) string {
files, _ := filepath.Glob(path)
if len(files) == 0 {
return ""
}
files = f.filter(files, exclude)
if len(files) == 0 {
return ""
}
return f.findLastFile(files)
}
func (f finder) filter(files []string, exclude string) []string {
if exclude == "" {
return files
}
fs := make([]string, 0, len(files))
for _, file := range files {
if ok, _ := filepath.Match(exclude, file); ok {
continue
}
fs = append(fs, file)
}
return fs
}
// TODO: the logic is probably wrong
func (f finder) findLastFile(files []string) string {
sort.Strings(files)
for i := len(files) - 1; i >= 0; i-- {
stat, err := os.Stat(files[i])
if err != nil || !stat.Mode().IsRegular() {
continue
}
return files[i]
}
return ""
}
|