-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencode_source.go
More file actions
61 lines (53 loc) · 1.09 KB
/
encode_source.go
File metadata and controls
61 lines (53 loc) · 1.09 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
package zlog
import (
"log/slog"
"runtime"
"strconv"
"strings"
"github.com/icefed/zlog/buffer"
)
func buildSource(pc uintptr) *slog.Source {
fs := runtime.CallersFrames([]uintptr{pc})
f, _ := fs.Next()
return &slog.Source{
Function: f.Function,
File: f.File,
Line: f.Line,
}
}
func formatSourceValueFromPC(buf *buffer.Buffer, pc uintptr) {
fs := runtime.CallersFrames([]uintptr{pc})
f, _ := fs.Next()
defer func() {
buf.WriteByte(':')
*buf = strconv.AppendInt(*buf, int64(f.Line), 10)
}()
i := strings.LastIndexByte(f.File, '/')
if i < 0 {
buf.WriteString(f.File)
return
}
i = strings.LastIndexByte(f.File[:i], '/')
if i < 0 {
buf.WriteString(f.File)
return
}
buf.WriteString(f.File[i+1:])
}
func formatSourceValue(buf *buffer.Buffer, s *slog.Source) {
defer func() {
buf.WriteByte(':')
*buf = strconv.AppendInt(*buf, int64(s.Line), 10)
}()
i := strings.LastIndexByte(s.File, '/')
if i < 0 {
buf.WriteString(s.File)
return
}
i = strings.LastIndexByte(s.File[:i], '/')
if i < 0 {
buf.WriteString(s.File)
return
}
buf.WriteString(s.File[i+1:])
}