-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.go
54 lines (46 loc) · 1.11 KB
/
frame.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
package mirror
import (
"fmt"
"regexp"
"runtime"
"strings"
)
type Frame struct {
Package string
Function string
File string
Line int
Formatted string
}
func fromRuntimeFrame(frame runtime.Frame) Frame {
packageName, functionName := splitFunction(frame.Function)
fileName := toFileName(frame.File)
line := frame.Line
return Frame{
Package: packageName,
Function: functionName,
File: fileName,
Line: line,
Formatted: toFormatted(packageName, functionName, fileName, line),
}
}
var re = regexp.MustCompile(`[\*\(\)]`)
func splitFunction(function string) (packageName string, functionName string) {
i := strings.LastIndex(function, "/") + 1
j := strings.Index(function[i:], ".")
return function[:i+j], re.ReplaceAllString(function[i+j+1:], "")
}
func toFileName(file string) string {
return file[strings.LastIndex(file, "/")+1:]
}
func toFormatted(
packageName string,
functionName string,
fileName string,
line int,
) string {
return fmt.Sprintf("\tat %s.%s(%s:%d)", packageName, functionName, fileName, line)
}
func (frame Frame) String() string {
return frame.Formatted
}