-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreflection.go-
More file actions
139 lines (111 loc) · 2.96 KB
/
reflection.go-
File metadata and controls
139 lines (111 loc) · 2.96 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package helpers
import (
"fmt"
"regexp"
"runtime"
"runtime/debug"
"strings"
)
func getFuncName(skip int) string {
pc, _, _, _ := runtime.Caller(skip)
var functionName = runtime.FuncForPC(pc).Name()
functionName = strings.Replace(functionName, strToReplace, strToReplaceWith, 1)
functionName = strings.Replace(functionName, "(", "", 1)
functionName = strings.Replace(functionName, ")", "", 1)
functionName = strings.Replace(functionName, "*", "", 1)
return functionName + "()"
}
// GetLineNumber -
func GetLineNumber() int {
pc := make([]uintptr, 15)
n := runtime.Callers(3, pc)
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
return frame.Line
}
// GetThisFuncName -
func GetThisFuncName() string {
return getFuncName(2)
}
// GetThisFuncNameSkip1 -
func GetThisFuncNameSkip1() string {
return getFuncName(3)
}
// GetThisFuncNameSkip2 -
func GetThisFuncNameSkip2() string {
return getFuncName(4)
}
// GetThisFuncNameSkip3 -
func GetThisFuncNameSkip3() string {
return getFuncName(5)
}
// GetThisFuncNameSkip4 -
func GetThisFuncNameSkip4() string {
return getFuncName(6)
}
// GetThisFuncNameSkip5 -
func GetThisFuncNameSkip5() string {
return getFuncName(7)
}
// GetThisFuncNameWithLine -
func GetThisFuncNameWithLine() string {
return fmt.Sprintf("%s : %d", getFuncName(2), GetLineNumber())
}
// GetStackTrace2 -
func GetStackTrace2(prefixToRemove string) string {
pc := make([]uintptr, 15)
n := runtime.Callers(2, pc)
frames := runtime.CallersFrames(pc[:n])
var stack = ""
frame, more := frames.Next()
for more {
// stack += newLineSeparator
funcName := strings.ReplaceAll(frame.Function, prefixToRemove, "")
stack += funcName + "()/" + stack
frame, more = frames.Next()
}
if len(stack) > 0 {
stack = stack[0 : len(stack)-1]
}
return stack
}
// GetStackTrace -
func GetStackTrace(prefixesToRemove []string) string {
// debug.PrintStack()
prefixesToRemove = append(prefixesToRemove, "created by ")
prefixesToRemove = append(prefixesToRemove, ".()")
stackTrace := debug.Stack()
lines := strings.Split(string(stackTrace), "\n")
stack := ""
for _, funcName := range lines {
if strings.Index(funcName, "goroutine") != -1 ||
strings.Index(funcName, "runtime/debug") != -1 ||
strings.Index(funcName, ".go:") != -1 ||
strings.Index(funcName, "GetStackTrace") != -1 ||
len(strings.TrimSpace(funcName)) <= 0 {
continue
}
regExp := regexp.MustCompile(`\((.*?)\)`)
regExpResult := regExp.FindAllStringSubmatch(funcName, -1)
for _, arr := range regExpResult {
if len(arr) > 1 {
toRemove := arr[1]
if strings.Index(toRemove, "0x") != -1 {
funcName = strings.Replace(funcName, toRemove, "", -1)
}
}
}
for _, prefixeToRemove := range prefixesToRemove {
funcName = strings.Replace(funcName, prefixeToRemove, "", -1)
}
if strings.Index(funcName, "(") == -1 {
funcName += "()"
}
if len(stack) > 0 {
stack = stack + " | " + funcName
} else {
stack = funcName
}
}
return stack
}