-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
glc.go
155 lines (136 loc) · 3.18 KB
/
glc.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
package glc
import (
"io/ioutil"
"os"
"strings"
"time"
"github.com/golang/glog"
)
type severity int32 // sync/atomic int32
// identical to glog log severities
const (
infoLog severity = iota
warningLog
errorLog
fatalLog
numSeverity = 4
)
var severityName = []string{
infoLog: "INFO",
warningLog: "WARNING",
errorLog: "ERROR",
fatalLog: "FATAL",
}
var empty = struct{}{}
// GLC define the glog cleaner options:
//
// path - Log files will be clean to this directory
// prefix - Log files name prefix
// interval - Log files clean scanning interval
// reserve - Log files reserve time
//
type GLC struct {
path string
prefix string
interval time.Duration
reserve time.Duration
symlinks map[string]struct{}
}
// InitOption define the glog cleaner init options for glc:
//
// Path - Log files will be clean to this directory
// Prefix - Log files name prefix
// Interval - Log files clean scanning interval
// Reserve - Log files reserve time
//
type InitOption struct {
Path string
Prefix string
Interval time.Duration
Reserve time.Duration
}
// NewGLC create a cleaner in a goroutine and do instantiation GLC by given
// init options.
func NewGLC(option InitOption) *GLC {
c := new(GLC)
c.path = option.Path
c.interval = option.Interval
c.prefix = option.Prefix
c.reserve = option.Reserve
c.symlinks = make(map[string]struct{}, numSeverity)
for i := 0; i < numSeverity; i++ {
symlink := c.prefix + "." + severityName[i]
c.symlinks[symlink] = empty
}
go c.cleaner()
return c
}
// clean provides function to check path exists by given log files path.
func (c *GLC) clean() {
exists, err := c.exists(c.path)
if err != nil {
glog.Errorln(err)
return
}
if !exists {
return
}
files, err := ioutil.ReadDir(c.path)
if err != nil {
glog.Errorln(err)
return
}
c.check(files)
}
// exists returns whether the given file or directory exists or not
func (c *GLC) exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
// check provides function to check log files name whether the deletion
// condition is satisfied.
func (c *GLC) check(files []os.FileInfo) {
excludes := make(map[string]struct{}, numSeverity)
for _, f := range files {
// skip the destination of symlink files
if _, ok := c.symlinks[f.Name()]; ok && f.Mode()&os.ModeSymlink != 0 {
if dst, err := os.Readlink(c.path + f.Name()); err == nil {
excludes[dst] = empty
}
}
}
for _, f := range files {
if _, ok := excludes[f.Name()]; ok {
continue
}
prefix := strings.HasPrefix(f.Name(), c.prefix)
str := strings.Split(f.Name(), `.`)
if prefix && len(str) == 7 && str[3] == `log` {
c.drop(f)
}
}
}
// drop check the log file creation time and delete the file if the conditions
// are met.
func (c *GLC) drop(f os.FileInfo) {
if time.Since(f.ModTime()) > c.reserve {
err := os.Remove(c.path + f.Name())
if err != nil {
glog.Errorln(err)
}
}
}
// cleaner provides regular cleaning function by given log files clean
// scanning interval.
func (c *GLC) cleaner() {
for {
c.clean()
time.Sleep(c.interval)
}
}