-
Notifications
You must be signed in to change notification settings - Fork 3
/
techlog.go
274 lines (194 loc) · 4.56 KB
/
techlog.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package techlog
import (
"context"
"github.com/radovskyb/watcher"
"github.com/xelaj/go-dry"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
reader2 "v8platform/techlog/reader"
)
type Options func()
type Events chan Event
var DefaultChunkSize = 4 * 1024
func MaxEvents(max int) Options {
return func() {
}
}
func FilterType(filters ...string) Options {
return func() {
}
}
type Watcher struct {
dir string
MaxEvents int
Filters []string
Ctx context.Context
e Events
journals map[string]int64
fileWatcher *watcher.Watcher
}
func (w *Watcher) Stop() {
// Завершает конекст выполнения чтения
w.Ctx.Done()
w.fileWatcher.Close()
}
func (w *Watcher) Start() Events {
w.e = make(Events, w.MaxEvents)
w.process()
return w.e
}
func (w *Watcher) process() {
go w.processFileWatcher()
}
func (w *Watcher) processFileWatcher() {
w.journals = make(map[string]int64)
w.fileWatcher = watcher.New()
if err := w.fileWatcher.Add(w.dir); err != nil {
log.Fatalln(err)
}
w.fileWatcher.SetMaxEvents(1)
w.fileWatcher.FilterOps(watcher.Create, watcher.Write, watcher.Remove)
go func() {
_ = w.fileWatcher.Start(time.Microsecond * 300)
}()
for {
select {
case <-w.Ctx.Done():
break
case e := <-w.fileWatcher.Event:
switch e.Op {
case watcher.Write:
w.emmitWriteEvent(e)
case watcher.Create:
w.journals[e.Name()] = 0
case watcher.Remove:
delete(w.journals, e.Name())
}
}
}
}
func (w *Watcher) emmitWriteEvent(event watcher.Event) {
if event.IsDir() || strings.HasSuffix(event.Name(), "~") {
return
}
offset := w.journals[event.Name()]
newOffset, _ := readLogFile(event.Path, offset, w.e)
w.journals[event.Name()] = newOffset
}
func readLogFile(path string, offset int64, inEvents Events) (n int64, err error) {
file, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
return 0, err
}
filestats, _ := file.Stat()
t := getFileDatetime(filestats.Name())
if offset > 0 {
_, err = file.Seek(offset, io.SeekStart)
if err != nil {
return
}
}
n, err = readTechlogData(file, offset, t, inEvents)
if err != nil {
return n, err
}
return offset + n, nil
}
func readTechlogData(reader io.Reader, offset int64, t time.Time, in Events) (int64, error) {
var readBytes int64
//wg := &sync.WaitGroup{}
//eLock := &sync.Mutex{}
//limitReader := make(chan struct{}, 1)
cr := reader2.NewChunkReader(reader, DefaultChunkSize)
for {
//limitReader <- struct{}{}
data, n, err := cr.Read()
if err != nil && err != io.EOF {
readBytes += int64(n)
return readBytes, err
}
//switch err {
//case nil, io.EOF:
// //
//default:
// log.Printf("error reading data <%s>", err)
// //<-limitReader
// break
//}
//if n == 0 {
// <-limitReader
// break
//}
//wg.Add(1)
//go func(d []byte, off int64) {
events := parseChunkData(data, t, offset)
//eLock.Lock()
for _, event := range events {
in <- event
}
//eLock.Unlock()
//wg.Done()
//<-limitReader
//}(data, offset)
offset += int64(n)
readBytes += int64(n)
if err == io.EOF {
return readBytes, err
}
}
//wg.Wait()
//return readBytes, nil
}
func getFileDatetime(name string) time.Time {
year := "20" + name[0:2]
month := name[2:4]
day := name[4:6]
hours := name[6:8]
return time.Date(dry.StringToInt(year), time.Month(dry.StringToInt(month)),
dry.StringToInt(day), dry.StringToInt(hours), 0, 0, 0, time.Local)
}
// TODO Пока сделал заготовку для функции мониторинга файла
func watch(dir string, opts ...Options) (Events, *Watcher) {
w := &Watcher{
dir: dir,
MaxEvents: 10,
Ctx: context.Background(),
}
return w.Start(), w
}
func StreamRead(file string, maxEvents int64) (Events, error) {
events, _, err := StreamReadAt(file, maxEvents, 0)
return events, err
}
func StreamReadAt(file string, maxEvents int64, offset int64) (Events, *int64, error) {
fullPath, err := filepath.Abs(file)
if err != nil {
return nil, &offset, err
}
off := &offset
stream := make(Events, maxEvents)
go func() {
*off, err = readLogFile(fullPath, offset, stream)
close(stream)
}()
return stream, off, nil
}
func ReadAt(file string, offset int64) ([]Event, *int64, error) {
stream, off, err := StreamReadAt(file, 50, offset)
if err != nil {
return nil, &offset, err
}
var events []Event
for event := range stream {
events = append(events, event)
}
return events, off, nil
}
func Read(file string) ([]Event, error) {
events, _, err := ReadAt(file, 0)
return events, err
}