-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
log.go
98 lines (86 loc) · 1.77 KB
/
log.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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
var LogOutputNice, LogOutputRpc io.WriteCloser
func replacepid(in string) string {
return strings.Replace(in, "%p", strconv.Itoa(os.Getpid()), -1)
}
func logf(fmtstr string, args ...interface{}) {
if LogOutputNice == nil {
return
}
if len(fmtstr) == 0 || fmtstr[len(fmtstr)-1] != '\n' {
fmtstr = fmtstr + "\n"
}
LogOutputNice.Write([]byte(time.Now().Format(time.RFC3339)))
LogOutputNice.Write([]byte(" "))
fmt.Fprintf(LogOutputNice, fmtstr, args...)
}
func setupLog() {
tmpdir := getLogArchivesDir()
if tmpdir == "" {
return
}
cleanLogArchives(tmpdir)
tmpdir = filepath.Join(tmpdir, fmt.Sprintf("%s-%d", time.Now().Format("20060102150405"), os.Getpid()))
err := os.Mkdir(tmpdir, 0770)
if err != nil {
return
}
fhnice, err1 := os.Create(filepath.Join(tmpdir, "nice.log"))
if err1 != nil {
return
}
fhrpc, err2 := os.Create(filepath.Join(tmpdir, "rpc.log"))
if err2 != nil {
fhnice.Close()
return
}
LogOutputNice = fhnice
LogOutputRpc = fhrpc
}
func getLogArchivesDir() string {
tmpdir := os.TempDir()
if tmpdir == "" {
return ""
}
tmpdir = filepath.Join(tmpdir, "gdlv-archive")
err := os.Mkdir(tmpdir, 0770)
if err != nil {
if !os.IsExist(err) {
return ""
}
}
return tmpdir
}
func cleanLogArchives(tmpdir string) {
dh, err := os.Open(tmpdir)
if err != nil {
return
}
fis, err := dh.Readdir(-1)
if err != nil {
return
}
for _, fi := range fis {
fields := strings.SplitN(fi.Name(), "-", 2)
if len(fields) != 2 {
continue
}
when, err := time.Parse("20060102150405", fields[0])
if err != nil {
continue
}
if time.Since(when) > 240*time.Hour {
// removing old archive
os.RemoveAll(filepath.Join(tmpdir, fi.Name()))
}
}
}