-
Notifications
You must be signed in to change notification settings - Fork 34
/
log_agent.go
416 lines (393 loc) · 11.1 KB
/
log_agent.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package main
import (
"time"
"fmt"
"os"
"bufio"
"log"
"path/filepath"
"strconv"
"strings"
"io"
)
type LogAgent struct {
currentTime time.Time
currentIndex int
currentFile *os.File
currentLogPath string
fileWriter *bufio.Writer
logRoot string
logContent []string //from new to old
contentModified bool
}
const (
EntryPrefixFormat = "20060102150405"
Day = 24 * time.Hour
MonthFormat = "200601"
DateFormat = "20060102"
ValidEntryIDLength = len(EntryPrefixFormat) + 3
InitialEntryIndex = 1
OpenFilePerm = 0640
)
func CreateLogAgent(dataPath string) (agent *LogAgent, err error) {
const (
logPathName = "log"
DefaultPathPerm = 0740
)
agent = &LogAgent{}
agent.logContent = make([]string, 0)
agent.contentModified = false
agent.logRoot = filepath.Join(dataPath, logPathName)
if _, err = os.Stat(agent.logRoot); os.IsNotExist(err){
if err = os.MkdirAll(agent.logRoot, DefaultPathPerm); err != nil{
return
}
log.Printf("<log> log root path '%s' created", agent.logRoot)
}
var today = time.Now().Truncate(Day)
var monthPath = filepath.Join(agent.logRoot, today.Format(MonthFormat))
agent.currentLogPath = filepath.Join(monthPath, fmt.Sprintf("%s.log", today.Format(DateFormat)))
if _, err = os.Stat(agent.currentLogPath); os.IsNotExist(err){
if _, err = os.Stat(monthPath); os.IsNotExist(err){
if err = os.Mkdir(monthPath, DefaultPathPerm); err != nil{
return
}
log.Printf("<log> new log path '%s' created", monthPath)
}
//today is a new day
agent.currentTime = time.Now().Truncate(time.Second)
agent.currentIndex = InitialEntryIndex
err = agent.openCurrentLog()
return
}
//load current
var lastEntry LogEntry
var entryAvailable = false
{
var scanSource *os.File
scanSource, err = os.Open(agent.currentLogPath)
if err != nil{
return
}
defer scanSource.Close()
var scanner = bufio.NewScanner(scanSource)
for scanner.Scan(){
var line = scanner.Text()
agent.logContent = append(agent.logContent, line)
if !entryAvailable{
lastEntry, err = parseLog(line)
if err != nil{
return
}
entryAvailable = true
}
}
}
if entryAvailable{
agent.currentTime = lastEntry.Time.Truncate(time.Second)
agent.currentIndex, err = strconv.Atoi(lastEntry.ID[len(EntryPrefixFormat):])
if err != nil{
log.Printf("<log> parse index from entry '%s' fail: %s", lastEntry.ID, err.Error())
return
}
log.Printf("<log> last entry '%s' (%d available) loaded from '%s'", lastEntry.ID, len(agent.logContent), agent.currentLogPath)
}else{
agent.currentTime = time.Now().Truncate(time.Second)
agent.currentIndex = InitialEntryIndex
}
err = agent.openCurrentLog()
return
}
func (agent *LogAgent) Write(content string) (err error) {
var now = time.Now()
var entryTimeStamp = now.Truncate(time.Second)
var entry LogEntry
if entryTimeStamp.Equal(agent.currentTime) {
entry.ID = fmt.Sprintf("%s%03d", entryTimeStamp.Format(EntryPrefixFormat), agent.currentIndex)
agent.currentIndex++
}else{
if !now.Truncate(24*time.Hour).Equal(agent.currentTime.Truncate(24*time.Hour)){
//open new file for a new day
if err = agent.Close(); err != nil{
return
}
agent.currentLogPath = filepath.Join(agent.logRoot, now.Format(MonthFormat), fmt.Sprintf("%s.log", now.Format(DateFormat)))
if err = agent.openCurrentLog(); err != nil{
return
}
agent.logContent = make([]string, 0)
agent.contentModified = false
}
//reset mark to second.1
agent.currentTime = entryTimeStamp
agent.currentIndex = InitialEntryIndex
entry.ID = fmt.Sprintf("%s%03d", entryTimeStamp.Format(EntryPrefixFormat), agent.currentIndex)
}
entry.Time = now
entry.Content = content
//insert
agent.logContent = append([]string{logToLine(entry)}, agent.logContent...)
if !agent.contentModified{
agent.contentModified = true
}
return nil
}
func (agent *LogAgent) Remove(idList []string) (err error) {
const (
IndexLength = 3
MonthPrefixLength = 6
)
var currentLocation = time.Now().Location()
var targetMap = map[string]map[string]bool{}
for _, entryID := range idList{
if len(entryID) != (len(EntryPrefixFormat) + IndexLength){
err = fmt.Errorf("invalid entry id '%s' with length %d", entryID, len(entryID))
return
}
timeStamp, err := time.ParseInLocation(EntryPrefixFormat, entryID[:len(EntryPrefixFormat)], currentLocation)
if err != nil{
err = fmt.Errorf("invalid entry id prefix '%s'", entryID[:len(EntryPrefixFormat)])
return err
}
var dayString = timeStamp.Format(DateFormat)
if _, exists := targetMap[dayString]; !exists{
targetMap[dayString] = map[string]bool{entryID: true}
}else{
targetMap[dayString][entryID] = true
}
}
for dayString, targets := range targetMap{
var month = dayString[:MonthPrefixLength]
var logFilePath = filepath.Join(agent.logRoot, month, fmt.Sprintf("%s.log", dayString))
if _, err = os.Stat(logFilePath); os.IsNotExist(err){
err = fmt.Errorf("can not find log file '%s'", logFilePath)
return
}
var isCurrentLog = logFilePath == agent.currentLogPath
var logLines []string
if isCurrentLog{
logLines = agent.logContent
}else{
//load all entries
var logFile *os.File
logFile, err = os.Open(logFilePath)
if err != nil{
err = fmt.Errorf("open log file fail: %s", err.Error())
return err
}
var logScanner = bufio.NewScanner(logFile)
for logScanner.Scan() {
logLines = append(logLines, logScanner.Text())
}
logFile.Close()
}
var removeResult = make([]string, 0)
for _, line := range logLines{
entry, err := parseLog(line)
if err != nil{
return err
}
if _, exists := targets[entry.ID]; exists{
delete(targets, entry.ID)
log.Printf("<log> entry '%s' removed from '%s'", entry.ID, logFilePath)
}else{
removeResult = append(removeResult, line)
}
}
if isCurrentLog{
agent.logContent = removeResult
agent.contentModified = true
}else{
//rewrite all lines
var logFile *os.File
logFile, err = os.OpenFile(logFilePath, os.O_WRONLY|os.O_APPEND|os.O_TRUNC, OpenFilePerm)
if err != nil{
err = fmt.Errorf("rewrite log file fail: %s", err.Error())
return err
}
var writer = bufio.NewWriter(logFile)
for _, line := range removeResult{
_, err = writer.WriteString(line)
if err != nil{
return err
}
if err = writer.WriteByte('\n'); err != nil{
return err
}
}
writer.Flush()
logFile.Close()
log.Printf("<log> %d lines rewrite to '%s'", len(removeResult), logFilePath)
}
}
log.Printf("<log> %d entries removed", len(idList))
return nil
}
func (agent *LogAgent) Query(condition LogQueryCondition) (logs []LogEntry, total uint, err error) {
const (
MaxEntry = 1000
)
total = 0
if condition.EndTime.Before(condition.BeginTime){
err = fmt.Errorf("invalid time range (%s ~ %s)", condition.BeginTime.Format(TimeFormatLayout), condition.EndTime.Format(TimeFormatLayout))
return
}
var queried, offset = 0, 0
var inTimeRange = false
for date := condition.EndTime; date.After(condition.BeginTime); date = date.Add(-Day){
var logFilePath = filepath.Join(agent.logRoot, date.Format(MonthFormat), fmt.Sprintf("%s.log", date.Format(DateFormat)))
var isCurrentLog = logFilePath == agent.currentLogPath
var logLines []string
if isCurrentLog{
logLines = agent.logContent
//log.Printf("<log> debug: %d lines loaded from current log", len(logLines))
}else{
if _, err = os.Stat(logFilePath); os.IsNotExist(err){
//log.Printf("<log> warning: query ignores absent log '%s'", logFilePath)
continue
}
var logFile *os.File
logFile, err = os.Open(logFilePath)
if err != nil{
return
}
var scanner = bufio.NewScanner(logFile)
for scanner.Scan(){
logLines = append(logLines, scanner.Text())
}
logFile.Close()
}
for _, line := range logLines {
var entry LogEntry
entry, err = parseLog(line)
if err != nil{
log.Printf("<log> parse line %d of log '%s' fail: %s", offset, logFilePath, err.Error())
return
}
if !inTimeRange {
if entry.Time.Before(condition.EndTime){
//range start
inTimeRange = true
}
}
if inTimeRange{
if offset < condition.Start{
total++
offset++
continue
}
if entry.Time.Before(condition.BeginTime){
break
}
if total >= MaxEntry{
break
}
total++
if queried < condition.Limit{
//store log
logs = append(logs, entry)
queried++
}
}
}//end scanner
if total >= MaxEntry{
break
}
}
//log.Printf("<log> %d / %d entries queried between (%s ~ %s), start from %d, with limit %d",
// len(logs), total,
// condition.BeginTime.Format(TimeFormatLayout),
// condition.EndTime.Format(TimeFormatLayout),
// condition.Start, condition.Limit)
return logs, total,nil
}
func (agent *LogAgent) Flush() (err error){
if !agent.contentModified{
return nil
}
if _, err = agent.currentFile.Seek(0, io.SeekStart); err != nil{
return
}
agent.fileWriter.Reset(agent.currentFile)
for _, log := range agent.logContent{
if _, err = agent.fileWriter.WriteString(log); err != nil{
return
}
if _, err = agent.fileWriter.WriteString("\n"); err != nil{
return
}
}
if err = agent.fileWriter.Flush(); err != nil{
return
}
agent.contentModified = false
return nil
}
func (agent *LogAgent) Close() (err error){
if err = agent.Flush(); err != nil{
return
}
return agent.closeCurrentLog()
}
func (agent *LogAgent) openCurrentLog() (err error){
if _, err = os.Stat(agent.currentLogPath); os.IsNotExist(err){
agent.currentFile, err = os.Create(agent.currentLogPath)
if err != nil{
return
}
log.Printf("<log> current log '%s' created", agent.currentLogPath)
}else{
//open
agent.currentFile, err = os.OpenFile(agent.currentLogPath, os.O_RDWR|os.O_CREATE, OpenFilePerm)
if err != nil{
return
}
log.Printf("<log> current log '%s' opened", agent.currentLogPath)
}
agent.fileWriter = bufio.NewWriter(agent.currentFile)
return nil
}
func (agent *LogAgent) closeCurrentLog() (err error){
if err = agent.fileWriter.Flush(); err != nil{
return
}
if err = agent.currentFile.Close(); err != nil{
return
}
agent.currentFile = nil
log.Printf("<log> current log '%s' closed", agent.currentLogPath)
return nil
}
func parseLog(line string) (entry LogEntry, err error) {
const (
separator = ","
)
var idTail = strings.Index(line, separator)
if idTail <= 0 {
err = fmt.Errorf("entry ID missed in line %s", line)
return
}
if idTail != ValidEntryIDLength{
err = fmt.Errorf("invalid entry ID '%s'", line[:idTail])
return
}
entry.ID = line[:idTail]
var timeTail = strings.Index(line[(idTail + 1):], separator)
if timeTail <= 0{
err = fmt.Errorf("entry time missed in line %s", line)
return
}
var timeInString = line[(idTail + 1): (idTail + 1 + timeTail)]
var unixnano int64
unixnano, err = strconv.ParseInt(timeInString, 10, 64)
if err != nil{
err = fmt.Errorf("invalid time stamp '%s'", timeInString)
return
}
entry.Time = time.Unix(0, unixnano)
entry.Content = line[(idTail + timeTail + 2):]
return entry, nil
}
func logToLine(entry LogEntry) (line string){
return fmt.Sprintf("%s,%d,%s", entry.ID, entry.Time.UnixNano(), entry.Content)
}