forked from davyxu/cellnet
/
util_runtime.go
55 lines (41 loc) · 971 Bytes
/
util_runtime.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
package cellnet
import (
"fmt"
"runtime"
)
// 将绝对路径按需要的节从右压缩
// 例如: c:/Develop/nucleus.git/server/src/core/util/stack.go中
// 当cStripPathSection=3
// 返回 core/util/stack.go
func StripFileName(filename string, part int) string {
slen := len(filename)
slashCount := 0
pos := 0
for i := slen - 1; i >= 0; i-- {
if filename[i] == '/' {
slashCount++
if slashCount >= part {
pos = i
break
}
}
}
// 如果确实找到有斜杠, 将斜杠的位置移除,表示这是一个相对路径
if pos > 0 {
pos++
}
return filename[pos:]
}
const cStripPathSection = 3
// 获取当前调用信息
func GetStackInfo(skip int) (string, int) {
_, file, line, ok := runtime.Caller(skip + 1)
if ok {
return StripFileName(file, cStripPathSection), line
}
return "(unknown)", 0
}
func GetStackInfoString(skip int) string {
file, line := GetStackInfo(skip + 1)
return fmt.Sprintf("%s(%d)", file, line)
}