-
Notifications
You must be signed in to change notification settings - Fork 5
/
debug.go
56 lines (46 loc) · 1.03 KB
/
debug.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
// +build debug
package golgi
import (
"fmt"
"log"
"os"
"strings"
"sync/atomic"
)
const DEBUG = true
func init() {
log.Printf("DEBUG")
}
var TABCOUNT uint32
var logger = log.New(os.Stderr, "", 0)
var replacement = "\n"
func tabcount() int {
return int(atomic.LoadUint32(&TABCOUNT))
}
func enterLogScope() {
atomic.AddUint32(&TABCOUNT, 1)
tabcount := tabcount()
logger.SetPrefix(strings.Repeat("\t", tabcount))
replacement = "\n" + strings.Repeat("\t", tabcount)
}
func leaveLogScope() {
tabcount := tabcount()
tabcount--
if tabcount < 0 {
atomic.StoreUint32(&TABCOUNT, 0)
tabcount = 0
} else {
atomic.StoreUint32(&TABCOUNT, uint32(tabcount))
}
logger.SetPrefix(strings.Repeat("\t", tabcount))
replacement = "\n" + strings.Repeat("\t", tabcount)
}
func logf(format string, others ...interface{}) {
if DEBUG {
// format = strings.Replace(format, "\n", replacement, -1)
s := fmt.Sprintf(format, others...)
s = strings.Replace(s, "\n", replacement, -1)
logger.Println(s)
// logger.Printf(format, others...)
}
}