-
Notifications
You must be signed in to change notification settings - Fork 0
/
logfile.go
165 lines (151 loc) · 3.19 KB
/
logfile.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
// Copyright (c) 2016 CHEN Xianren. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package log
import (
"io"
"os"
"path/filepath"
"sort"
"sync"
"time"
)
type Logfile struct {
mu sync.Mutex
file *os.File
layout, baseName string
size, seconds, maxSize, maxCount int64
}
func Open(baseName string, seconds, maxSize, maxCount int64) (io.WriteCloser, error) {
return open(".20060102.150405", baseName, seconds, maxSize, maxCount)
}
func OpenDaily(baseName string) (io.WriteCloser, error) {
return open(".20060102", baseName, 86400, 0, 0)
}
func open(layout, baseName string, seconds, maxSize, maxCount int64) (io.WriteCloser, error) {
if seconds <= 0 && maxSize <= 0 && maxCount <= 0 {
return openFile(baseName)
}
lf := &Logfile{
layout: layout,
baseName: baseName,
seconds: seconds,
maxSize: maxSize,
maxCount: maxCount,
}
if err := lf.rotate(); err != nil {
ErrWarning(lf.Close())
return nil, err
}
go lf.cycle()
return lf, nil
}
func (lf *Logfile) Close() error {
lf.mu.Lock()
defer lf.mu.Unlock()
return lf.replace(nil)
}
func (lf *Logfile) Write(p []byte) (int, error) {
lf.mu.Lock()
defer lf.mu.Unlock()
if lf.file == nil {
return 0, os.ErrInvalid
}
n, err := lf.file.Write(p)
lf.size += int64(n)
if err != nil {
return n, err
}
if lf.maxSize > 0 && lf.size > lf.maxSize {
err = lf.rotate()
}
return n, err
}
func (lf *Logfile) replace(newFile *os.File) error {
if lf.file != nil {
if err := lf.file.Close(); err != nil {
return err
}
}
if newFile != nil {
if stat, err := newFile.Stat(); err != nil {
return err
} else {
lf.size = stat.Size()
}
} else {
lf.size = 0
}
lf.file = newFile
return nil
}
func (lf *Logfile) rotate() error {
name := lf.baseName + time.Now().Format(lf.layout)
if lf.file != nil && lf.file.Name() == name {
return nil
}
file, err := openFile(name)
if err == nil {
err = lf.replace(file)
}
if err != nil {
return err
}
existWarning(os.Remove(lf.baseName))
_, f := filepath.Split(name)
ErrWarning(os.Symlink(f, lf.baseName))
go lf.purge()
return nil
}
func (lf *Logfile) cycle() {
if lf.seconds <= 0 {
return
}
ns := lf.seconds * 1e9
for {
now := time.Now().UnixNano()
next := (now/ns)*ns + ns
<-time.After(time.Duration(next - now))
lf.mu.Lock()
if lf.file == nil {
lf.mu.Unlock()
return
} else {
ErrWarning(lf.rotate())
lf.mu.Unlock()
}
}
}
func (lf *Logfile) purge() {
if lf.maxCount <= 0 {
return
}
names, _ := filepath.Glob(lf.baseName + "*")
i, n := 0, len(names)
for i < n {
_, err := time.Parse(lf.layout, names[i][len(lf.baseName):])
if err == nil {
i++
} else {
names = append(names[:i], names[i+1:]...)
n--
}
}
n -= int(lf.maxCount)
if n > 0 {
sort.Strings(names)
for i = 0; i < n; i++ {
if lf.file == nil || lf.file.Name() != names[i] {
existWarning(os.Remove(names[i]))
}
}
}
}
func openFile(name string) (*os.File, error) {
return os.OpenFile(name, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
}
func existWarning(err error) {
if err != nil && !os.IsNotExist(err) {
Warningln(err)
}
}