/
log_rotate.go
109 lines (89 loc) · 2.24 KB
/
log_rotate.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
package utils
import (
"fmt"
"io"
"os"
"os/user"
)
func LogMustRotate(logfile string, maxSize int64) (bool, error) {
fi, err := os.Stat(logfile)
if err != nil {
return false, err
}
// get the size
size := fi.Size()
if size >= maxSize {
return true, nil
}
return false, nil
}
func Rotate(filename string, tmpFilename string) (int64, error) {
usr, _ := user.Current()
configPath := usr.HomeDir
src := configPath + filename
tmp := configPath + tmpFilename
// Check if tmp file exists
exists, err := tmpFileExists(tmp)
if err != nil {
return 0, fmt.Errorf("Error when checking for temp log existence: %s", err.Error())
}
// If it exists, clear it
if exists {
_, err := cleanFile(tmp)
if err != nil {
return 0, fmt.Errorf("failed to clean temp log")
}
}
// Open the source file (main log)
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, fmt.Errorf("failed to stat log file")
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
// Open the tmp log (or create if it doesn't exist)
dst, err := os.OpenFile(tmp, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return 0, fmt.Errorf("failed to open %s temporary log file", tmp)
}
defer dst.Close()
// Copy the contents of main log to tmp log
nBytes, err := io.Copy(dst, source)
if err != nil {
return 0, fmt.Errorf("failed to copy log to temp log")
}
// clear the main log
_, err = cleanFile(src)
if err != nil {
return 0, fmt.Errorf("failed to clean snitch log file")
}
return nBytes, err
}
func tmpFileExists(tmpFilename string) (bool, error) {
if _, err := os.Stat(tmpFilename); os.IsNotExist(err) {
dst, err := os.OpenFile(tmpFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return false, fmt.Errorf("failed to open %s temporary log file", tmpFilename)
}
defer dst.Close()
}
return true, nil
}
func cleanFile(filePath string) (bool, error) {
err := os.Remove(filePath)
if err != nil {
return false, err
}
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return false, err
}
defer file.Close()
return true, err
}