-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
89 lines (81 loc) · 1.64 KB
/
utils.go
File metadata and controls
89 lines (81 loc) · 1.64 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
package log
import (
"os"
"runtime"
"runtime/debug"
"strings"
)
func getCallerFrame(skip int) (frame runtime.Frame, ok bool) {
const skipOffset = 2 // skip getCallerFrame and Callers
pc := make([]uintptr, 1)
numFrames := runtime.Callers(skip+skipOffset, pc[:])
if numFrames < 1 {
return
}
frame, _ = runtime.CallersFrames(pc).Next()
return frame, frame.PC != 0
}
func getFolderFile(s string) string {
const pathCount = 0
b := 0
a := 0
for i := len(s) - 2; i > 0; i-- {
if os.IsPathSeparator(s[i]) {
a = i + 1
if b > pathCount {
break
}
b++
}
}
return s[a:]
}
func getStack(skip int) []string {
const seperator2 = ": "
stack := string(debug.Stack())
// + 2 -> skip: "runtime/debug.stack" and "daneshvar/sesame/logger.(*logger).stack"
// * 2 -> 2 lines per a call
// + 1 skip: "goroutine 1 [running]:"
stacks := make([]string, 0)
if skip > 0 {
skip = (skip+2)*2 + 1
}
prev := 0
line := ""
for i := range stack {
if stack[i] == '\n' {
if skip <= 0 {
s := stack[prev:i]
if skip < 0 {
if strings.Contains(s, "runtime/panic") {
skip = 0
}
} else {
k := 2
for j := len(s) - 1; j >= 0; j-- {
if s[j] == '/' {
k--
if k == 0 {
if p := strings.Index(s, " +0x"); p > 0 {
stacks = append(stacks, line+getFolderFile(strings.TrimSpace(s[:p])))
line = ""
} else {
stacks = append(stacks, line+strings.TrimSpace(s))
line = ""
}
break
}
}
}
if k != 0 {
line += strings.TrimSpace(s) + seperator2
}
}
} else {
skip--
}
prev = i + 1
}
}
return stacks
}