forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
99 lines (79 loc) · 2.02 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
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
package gno
import (
"fmt"
"net/http"
"os"
_ "net/http/pprof"
)
// NOTE: the golang compiler doesn't seem to be intelligent
// enough to remove steps when const debug is True,
// so it is still faster to first check the truth value
// before calling debug.Println or debug.Printf.
type debugging bool
// using a const is probably faster.
// const debug debugging = true // or flip
var debug debugging = false
func init() {
debug = os.Getenv("DEBUG") == "1"
if debug {
go func() {
// e.g.
// curl -sK -v http://localhost:8080/debug/pprof/profile?seconds=30 > cpu.out
// curl -sK -v http://localhost:8080/debug/pprof/heap > heap.out
// curl -sK -v http://localhost:8080/debug/pprof/allocs > allocs.out
// see https://gist.github.com/slok/33dad1d0d0bae07977e6d32bcc010188.
http.ListenAndServe("localhost:8080", nil)
}()
}
}
// runtime debugging flag.
var enabled bool = true
func (d debugging) Println(args ...interface{}) {
if d {
if enabled {
fmt.Println(append([]interface{}{"DEBUG:"}, args...)...)
}
}
}
func (d debugging) Printf(format string, args ...interface{}) {
if d {
if enabled {
fmt.Printf("DEBUG: "+format, args...)
}
}
}
var derrors []string = nil
// Instead of actually panic'ing, which messes with tests, errors are sometimes
// collected onto `var derrors`. tests/file_test.go checks derrors after each
// test, and the file test fails if any unexpected debug errrors were found.
func (d debugging) Errorf(format string, args ...interface{}) {
if d {
if enabled {
derrors = append(derrors, fmt.Sprintf(format, args...))
}
}
}
//----------------------------------------
// Exposed errors accessors
// File tests may access debug errors.
func HasDebugErrors() bool {
return len(derrors) > 0
}
func GetDebugErrors() []string {
return derrors
}
func ClearDebugErrors() {
derrors = nil
}
func IsDebug() bool {
return bool(debug)
}
func IsDebugEnabled() bool {
return bool(debug) && enabled
}
func DisableDebug() {
enabled = false
}
func EnableDebug() {
enabled = true
}