/
codelocation.go
49 lines (41 loc) · 997 Bytes
/
codelocation.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
package runtime
import (
"fmt"
"runtime"
libconsts "social/lib/consts"
)
// CodeLocation 代码位置
type CodeLocation struct {
FileName string //文件名
FuncName string //函数名
Line int //行数
}
// Error 错误信息
func (p *CodeLocation) Error() string {
return fmt.Sprintf("file:%v line:%v func:%v", p.FileName, p.Line, p.FuncName)
}
// String 错误信息
func (p *CodeLocation) String() string {
return p.Error()
}
// GetCodeLocation 获取代码位置
//
// 参数:
// skip:The argument skip is the number of stack frames to ascend, with 0 identifying the caller of Caller.
func GetCodeLocation(skip int) *CodeLocation {
c := &CodeLocation{
FileName: libconsts.Unknown,
FuncName: libconsts.Unknown,
}
pc, fileName, line, ok := runtime.Caller(skip)
if ok {
c.FileName = fileName
c.Line = line
c.FuncName = runtime.FuncForPC(pc).Name()
}
return c
}
// Location 获取代码位置
func Location() string {
return GetCodeLocation(2).String()
}