-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
75 lines (61 loc) · 947 Bytes
/
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
package main
import (
"fmt"
"log"
"os"
"os/user"
"path"
"strings"
)
var (
linit bool = false
lfp *os.File
)
func initLog(name string) error {
if !optDebug {
return nil
}
u, err := user.Current()
if err != nil {
return err
}
f := path.Join(u.HomeDir, "."+name+".log")
lfp, err := os.OpenFile(f, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
return err
}
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
log.SetOutput(lfp)
linit = true
dbg(strings.Repeat("=", 20))
dbg(lfp.Name())
if optVerbose {
fmt.Println(lfp.Name())
}
return nil
}
func cleanupLog() {
if !optDebug {
return
}
lfp.Close()
linit = false
}
func dbg(args ...interface{}) {
if !optDebug {
return
}
assert(linit)
globalLock()
log.Println(args...)
globalUnlock()
}
func dbgf(f string, args ...interface{}) {
if !optDebug {
return
}
assert(linit)
globalLock()
log.Printf(f, args...)
globalUnlock()
}