-
Notifications
You must be signed in to change notification settings - Fork 3
/
debug.go
82 lines (72 loc) · 1.53 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
package main
import (
"errors"
"fmt"
//"github.com/emicklei/hopwatch"
"bytes"
"log"
"runtime"
"time"
)
// source https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/C24fRw8HDmI
// from David Wright
type ErrorTrace struct {
err error
trace string
}
func NewErrorTrace(v ...interface{}) error {
msg := fmt.Sprint(v...)
buf := bytes.Buffer{}
skip := 2
addtrace:
pc, file, line, ok := runtime.Caller(skip)
if ok && skip < 6 { // print a max of 6 lines of trace
fun := runtime.FuncForPC(pc)
buf.WriteString(fmt.Sprint(fun.Name(), " -- ", file, ":", line, "\n"))
skip++
goto addtrace
}
if buf.Len() > 0 {
trace := buf.String()
return ErrorTrace{err: errors.New(msg), trace: trace}
}
return errors.New("error generating error")
}
func (et ErrorTrace) Error() string {
return et.err.Error() + "\n " + et.trace
}
func B(v ...interface{}) {
ts := time.Now().Format("2006-02-01 15:04:05: ")
println(ts, NewErrorTrace(v...).Error())
}
// Unused ...
func F(v ...interface{}) {
ts := time.Now().Format("2006-02-01 15:04:05: ")
println(ts, NewErrorTrace(v...).Error())
panic("-----")
}
func D(v ...interface{}) {
if debuggingenabled {
formatstring := ""
for range v {
formatstring += " %+v"
}
log.Printf(formatstring, v...)
}
}
func DP(v ...interface{}) {
if debuggingenabled {
formatstring := ""
for range v {
formatstring += " %+v"
}
log.Printf(formatstring, v...)
}
}
func P(v ...interface{}) {
formatstring := ""
for range v {
formatstring += " %+v"
}
log.Printf(formatstring, v...)
}