-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcaller.go
81 lines (68 loc) · 2.21 KB
/
caller.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
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
package logf
import (
"runtime"
"strconv"
"strings"
"unsafe"
)
// EntryCaller holds values returned by runtime.Caller.
type EntryCaller struct {
PC uintptr
File string
Line int
Specified bool
}
// NewEntryCaller creates an instance of EntryCaller with the given number
// of frames to skip.
func NewEntryCaller(skip int) EntryCaller {
var c EntryCaller
c.PC, c.File, c.Line, c.Specified = runtime.Caller(skip + 1)
return c
}
// FileWithPackage cuts a package name and a file name from EntryCaller.File.
func (c EntryCaller) FileWithPackage() string {
// As for os-specific path separator battle here, my opinion coincides
// with the last comment here https://github.com/golang/go/issues/3335.
//
// Go team should simply document the current behavior of always using
// '/' in stack frame data. That's the way it's been implemented for
// years, and packages like github.com/go-stack/stack that have been
// stable for years expect it. Changing the behavior in a future version
// of Go will break working code without a clearly documented benefit.
// Documenting the behavior will help avoid new code from making the
// wrong assumptions.
found := strings.LastIndexByte(c.File, '/')
if found == -1 {
return c.File
}
found = strings.LastIndexByte(c.File[:found], '/')
if found == -1 {
return c.File
}
return c.File[found+1:]
}
// CallerEncoder is the function type to encode the given EntryCaller.
type CallerEncoder func(EntryCaller, TypeEncoder)
// ShortCallerEncoder encodes the given EntryCaller using it's FileWithPackage
// function.
func ShortCallerEncoder(c EntryCaller, m TypeEncoder) {
var callerBuf [64]byte
var b []byte
b = callerBuf[:0]
b = append(b, c.FileWithPackage()...)
b = append(b, ':')
b = strconv.AppendInt(b, int64(c.Line), 10)
m.EncodeTypeUnsafeBytes(noescape(unsafe.Pointer(&b)))
runtime.KeepAlive(&b)
}
// FullCallerEncoder encodes the given EntryCaller using a full file path.
func FullCallerEncoder(c EntryCaller, m TypeEncoder) {
var callerBuf [256]byte
var b []byte
b = callerBuf[:0]
b = append(b, c.File...)
b = append(b, ':')
b = strconv.AppendInt(b, int64(c.Line), 10)
m.EncodeTypeUnsafeBytes(noescape(unsafe.Pointer(&b)))
runtime.KeepAlive(&b)
}