-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
186 lines (164 loc) · 3.71 KB
/
file.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
//
// Copyright (c) 2018 Cavium
//
// SPDX-License-Identifier: Apache-2.0
//
package logging
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/Circutor/edgex/pkg/models"
)
const (
rmFileSuffix string = ".tmp"
)
type fileLog struct {
filename string
maxBytes int64
logsCount int
out *os.File //io.WriteCloser
}
func (fl *fileLog) closeSession() {
if fl.out != nil {
fl.out.Close()
}
}
func (fl *fileLog) add(le models.LogEntry) error {
if fl.out == nil {
var err error
//First check to see if the specified directory exists
//File won't be written without directory.
path := filepath.Dir(fl.filename)
if _, err = os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, 0755)
}
fl.out, err = os.OpenFile(fl.filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
//fmt.Println("Error opening log file: ", fl.filename, err)
fl.out = nil
return err
}
}
stat, err := fl.out.Stat()
if err != nil {
//fmt.Println("Error reading log file size: ", fl.filename, err)
return err
}
if stat.Size() > fl.maxBytes {
fl.out.Close()
for i := fl.logsCount - 1; i > 0; i-- {
oldFileName := fmt.Sprintf("%s.%d", fl.filename, i)
newFileName := fmt.Sprintf("%s.%d", fl.filename, i+1)
os.Rename(oldFileName, newFileName)
}
newFileName := fmt.Sprintf("%s.1", fl.filename)
os.Rename(fl.filename, newFileName)
fl.out, err = os.OpenFile(fl.filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
//fmt.Println("Error opening log file: ", fl.filename, err)
fl.out = nil
return err
}
}
res, err := json.Marshal(le)
if err != nil {
return err
}
fl.out.Write(res)
fl.out.Write([]byte("\n"))
return nil
}
func (fl *fileLog) remove(criteria matchCriteria) (int, error) {
var allCount int
for i := fl.logsCount; i >= 0; i-- {
var logPartialFile string
if i > 0 {
logPartialFile = fmt.Sprintf("%s.%d", fl.filename, i)
} else {
logPartialFile = fl.filename
}
tmpFilename := logPartialFile + rmFileSuffix
tmpFile, err := os.OpenFile(tmpFilename, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
continue
}
defer os.Remove(tmpFilename)
count := 0
f, err := os.Open(logPartialFile)
if err != nil {
continue
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
var le models.LogEntry
line := scanner.Bytes()
err := json.Unmarshal(line, &le)
if err == nil {
if !criteria.match(le) {
tmpFile.Write(line)
tmpFile.Write([]byte("\n"))
} else {
count++
}
}
}
tmpFile.Close()
if count != 0 {
err = os.Rename(tmpFilename, logPartialFile)
if err != nil {
continue
}
// Close old file to open the new one when writing next log
if fl.out != nil {
fl.out.Close()
fl.out = nil
}
}
allCount += count
}
return allCount, nil
}
func (fl *fileLog) find(criteria matchCriteria) ([]models.LogEntry, error) {
var logs []models.LogEntry
var err error
// Here we should make a for to include all files in logs
for i := fl.logsCount; i >= 0; i-- {
var logPartialFile string
if i > 0 {
logPartialFile = fmt.Sprintf("%s.%d", fl.filename, i)
} else {
logPartialFile = fl.filename
}
f, err := os.Open(logPartialFile)
if err != nil {
continue
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
var le models.LogEntry
line := scanner.Bytes()
err := json.Unmarshal(line, &le)
if err == nil {
if criteria.match(le) {
logs = append(logs, le)
if criteria.Limit != 0 && len(logs) >= criteria.Limit {
break
}
}
}
}
}
return logs, err
}
func (fl *fileLog) reset() {
if fl.out != nil {
fl.out.Close()
fl.out = nil
}
os.Remove(fl.filename)
}