-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.go
More file actions
38 lines (34 loc) · 766 Bytes
/
Copy pathdebug.go
File metadata and controls
38 lines (34 loc) · 766 Bytes
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
package main
import (
"fmt"
"os"
"sync"
)
// Diagnostic logging. Off by default so end users get a clean console. Enable
// with the -debug flag; output then goes to debug.log (gitignored via *.log),
// never to the console.
var (
debugEnabled bool
debugFile *os.File
debugOnce sync.Once
debugMu sync.Mutex
)
// debugf writes a diagnostic line to debug.log when -debug is active; otherwise
// it does nothing.
func debugf(format string, a ...interface{}) {
if !debugEnabled {
return
}
debugOnce.Do(func() {
f, err := os.OpenFile("debug.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err == nil {
debugFile = f
}
})
if debugFile == nil {
return
}
debugMu.Lock()
fmt.Fprintf(debugFile, format, a...)
debugMu.Unlock()
}