-
Notifications
You must be signed in to change notification settings - Fork 388
/
file.go
176 lines (153 loc) · 3.39 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
package logutil
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
"go.uber.org/multierr"
"moul.io/u"
"berty.tech/berty/v2/go/pkg/errcode"
)
func newFileWriteCloser(target, kind string) (io.WriteCloser, error) {
var filename string
switch {
case strings.HasSuffix(target, ".log"): // use the indicated 'path' as filename
filename = target
default: // automatically create a new file in the 'path' directory following a pattern
startTime := time.Now().Format(filePatternDateLayout)
filename = filepath.Join(
target,
fmt.Sprintf("%s-%s.log", kind, startTime),
)
// run gc
{
err := LogfileGC(target, 20)
if err != nil {
return nil, err
}
}
}
if dir := filepath.Dir(filename); !u.DirExists(dir) {
err := os.MkdirAll(dir, 0o711)
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
}
var writer io.WriteCloser
if u.FileExists(filename) {
var err error
writer, err = os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
} else {
var err error
writer, err = os.Create(filename)
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
}
return writer, nil
}
type Logfile struct {
Dir string
Name string
Size int64
Kind string
Time time.Time
Latest bool
Errs error `json:"Errs,omitempty"`
}
func (l Logfile) Path() string {
return filepath.Join(l.Dir, l.Name)
}
const filePatternDateLayout = "2006-01-02T15-04-05.000"
var filePatternRegex = regexp.MustCompile(`(?m)^(.*)-(\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}.\d{3}).log$`)
func LogfileList(logDir string) ([]*Logfile, error) {
files, err := ioutil.ReadDir(logDir)
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
logfiles := []*Logfile{}
for _, file := range files {
sub := filePatternRegex.FindStringSubmatch(file.Name())
if sub == nil {
continue
}
t, err := time.Parse(filePatternDateLayout, sub[2])
var errs error
if err != nil {
errs = multierr.Append(errs, err)
}
logfiles = append(logfiles, &Logfile{
Dir: logDir,
Name: file.Name(),
Size: file.Size(),
Kind: sub[1],
Time: t,
Errs: errs,
})
}
// compute latest
if len(logfiles) > 0 {
var maxTime time.Time
for _, file := range logfiles {
if file.Time.After(maxTime) {
maxTime = file.Time
}
}
for _, file := range logfiles {
if file.Time == maxTime {
file.Latest = true
}
}
}
return logfiles, nil
}
func CurrentLogfilePath(target string) (string, error) {
filename := ""
switch {
case strings.HasSuffix(target, ".log"): // use the indicated 'path' as filename
filename = target
default: // find the latest log file in the 'path' directory following a pattern
logfileList, err := LogfileList(target)
if err != nil {
return "", err
}
for _, logfile := range logfileList {
if logfile.Latest {
filename = logfile.Path()
break
}
}
}
return filename, nil
}
func LogfileGC(logDir string, max int) error {
if !u.DirExists(logDir) {
return nil
}
files, err := LogfileList(logDir)
if err != nil {
return errcode.TODO.Wrap(err)
}
if len(files) < max {
return nil
}
sort.Slice(files, func(i, j int) bool {
return files[i].Time.Before(files[j].Time)
})
var errs error
for i := 0; i < len(files)-max; i++ {
err := os.Remove(files[i].Path())
if err != nil {
errs = multierr.Append(errs, err)
}
}
return errs
}