-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.go
52 lines (45 loc) · 1019 Bytes
/
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
package gotils
import (
"bytes"
"encoding/json"
"log"
"runtime"
"strings"
)
func DebugToJSONStringNoIndentEscaped(o interface{}) string {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", "")
err := encoder.Encode(o)
if err == nil {
return buffer.String()
}
return "{DebugToJSONStringNoIndentEscaped:ERROR}"
}
func DebugRuntimeCallerFuncion(targetName string) string {
pc := make([]uintptr, 20)
n := runtime.Callers(0, pc)
frames := runtime.CallersFrames(pc[:n])
isReturnNext := false
for {
frame, more := frames.Next()
funcShortName := "failed_to_parse"
comps := strings.Split(frame.Function, ".")
if len(comps) > 0 {
funcShortName = comps[len(comps)-1]
}
if isReturnNext {
return funcShortName
}
// return the next one on top of this one:
if targetName == funcShortName {
isReturnNext = true
}
if !more {
break
}
}
log.Println("ERROR: DebugCallerFuncion:", targetName)
return StringNone
}