-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdebug.go
88 lines (77 loc) · 1.74 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
package sqlca
import (
"fmt"
"github.com/civet148/log"
"reflect"
"runtime"
"strings"
)
//assert bool and string/struct/slice/map nil, call panic
func assert(v interface{}, strMsg string, args ...interface{}) {
if isNilOrFalse(v) {
log.Errorf(strMsg, args...)
}
}
// judgement: variant is a pointer type ?
func isPtrType(v interface{}) bool {
typ := reflect.TypeOf(v)
if typ.Kind() == reflect.Ptr {
return true
}
return false
}
// judgement: bool, integer, string, struct, slice, map is nil or false?
func isNilOrFalse(v interface{}) bool {
switch v.(type) {
case string:
if v.(string) == "" {
return true
}
case bool:
return !v.(bool)
case int8, int16, int, int32, int64, uint8, uint16, uint, uint32, uint64:
{
if fmt.Sprintf("%v", v) == "0" {
return true
}
}
default:
{
val := reflect.ValueOf(v)
return val.IsNil()
}
}
return false
}
func getCaller(skip int) (strFile, strFunc string, nLine int) {
pc, f, n, ok := runtime.Caller(skip)
if ok {
strFile = f
nLine = n
strFunc = getFuncNameFromPC(pc)
}
return
}
// get function name from call stack
func getFuncNameFromPC(pc uintptr) (name string) {
n := runtime.FuncForPC(pc).Name()
ns := strings.Split(n, ".")
name = ns[len(ns)-1]
return
}
func fmtParentCaller(strFmt string) string {
strFunc, strFile, nLine := getCaller(1)
strFmt = fmt.Sprintf("<%v:%v %v()> ", strFile, nLine, strFunc) + strFmt
return strFmt
}
func (e *Engine) setDebug(ok bool) {
if ok {
log.SetLevel(log.LEVEL_DEBUG)
} else {
log.SetLevel(log.LEVEL_INFO)
}
}
func (e *Engine) panic(strFmt string, args ...interface{}) {
//strFmt = fmtParentCaller(strFmt)
log.Fatalf(strFmt, args...)
}