-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdebug.go
77 lines (65 loc) · 1.56 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
package main
import (
"fmt"
"log"
"os"
"path"
"runtime"
"strconv"
"golang.org/x/crypto/ssh/terminal"
)
const (
logFlags = log.Ldate | log.Ltime
)
var (
debugLogger = log.New(os.Stderr, "", logFlags)
debugLevel = 0 // set to 0 (zero) for disabling debug
prefixFmt = ""
formatMap = map[bool]string{
true: "\033[33mDEBUG L%d [%s:%d]\033[0m \t",
false: "DEBUG L%d [%s:%d] \t",
}
)
func init() {
// Determine if output format to use according to the destination of the
// debug messages. If stderr is a terminal we can use colors
prefixFmt = formatMap[terminal.IsTerminal(int(os.Stderr.Fd()))]
// Set the debug level from the environmental variable
if env := os.Getenv("NETPERF_DEBUG"); env != "" {
if value, err := strconv.ParseInt(env, 10, 32); err == nil {
debugLevel = clamp(int(value), 0, 5)
}
}
}
func setDebugLevel(level int) {
debugLevel = level
}
func isDebugActive() bool {
return debugLevel > 0
}
// Show a debug message
func debug(level int, format string, v ...interface{}) {
if debugLevel > 0 && level <= debugLevel {
_, file, line, _ := runtime.Caller(1)
debugLogger.SetPrefix(fmt.Sprintf(prefixFmt, level, path.Base(file), line))
debugLogger.Printf(format, v...)
}
}
// Returns the minimum value among two integers
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
// Returns the maximum value among two integers
func maxInt(a, b int) int {
if a < b {
return b
}
return a
}
// Keeps a given value within the specified interval
func clamp(val, min, max int) int {
return minInt(maxInt(min, val), max)
}