This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappend.go
155 lines (130 loc) · 4.42 KB
/
append.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package rec
import (
"math"
"runtime"
"strconv"
"strings"
"time"
)
// apppendUnicodeEscapeSequence converts a single byte into bytes sequence of Unicode Escape Sequence and appends to dst.
func apppendUnicodeEscapeSequence(dst []byte, singleByte byte) []byte {
const hextable string = "0123456789abcdef"
// cf. https://github.com/golang/go/blob/70deaa33ebd91944484526ab368fa19c499ff29f/src/encoding/hex/hex.go#L28-L29
dst = append(dst, '\\', 'u', '0', '0', hextable[singleByte>>4], hextable[singleByte&0x0f])
return dst
}
// nolint: cyclop
// appendJSONEscapedString.
func appendJSONEscapedString(dst []byte, s string) []byte {
for i := 0; i < len(s); i++ {
if s[i] != '"' && s[i] != '\\' && s[i] > 0x1F {
dst = append(dst, s[i])
continue
}
// cf. https://tools.ietf.org/html/rfc8259#section-7
// ... MUST be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).
switch s[i] {
case '"', '\\':
dst = append(dst, '\\', s[i])
case '\b' /* 0x08 */ :
dst = append(dst, '\\', 'b')
case '\f' /* 0x0C */ :
dst = append(dst, '\\', 'f')
case '\n' /* 0x0A */ :
dst = append(dst, '\\', 'n')
case '\r' /* 0x0D */ :
dst = append(dst, '\\', 'r')
case '\t' /* 0x09 */ :
dst = append(dst, '\\', 't')
default:
dst = apppendUnicodeEscapeSequence(dst, s[i])
}
}
return dst
}
func appendFloatFieldValue(dst []byte, value float64, bitSize int) []byte {
switch {
case math.IsNaN(value):
return append(dst, `"NaN"`...)
case math.IsInf(value, 1):
return append(dst, `"+Inf"`...)
case math.IsInf(value, -1):
return append(dst, `"-Inf"`...)
}
return strconv.AppendFloat(dst, value, 'f', -1, bitSize)
}
const (
// TimeFormatUnixDecimal is time format for rec.Config.TimestampFieldFormat and rec.TimeFormat() and rec.TimeFormatPtr()
// UNIXDECIMAL format style ... 1638645867.123456789.
TimeFormatUnixDecimal = "UNIXDECIMAL"
// TimeFormatUnix is time format for rec.Config.TimestampFieldFormat and rec.TimeFormat() and rec.TimeFormatPtr()
// UNIX format style ... 1638645867.
TimeFormatUnix = "UNIX"
// TimeFormatUnixMilli is time format for rec.Config.TimestampFieldFormat and rec.TimeFormat() and rec.TimeFormatPtr()
// UNIXMILLI format style ... 1638645867123.
TimeFormatUnixMilli = "UNIXMILLI"
// TimeFormatUnixMicro is time format for rec.Config.TimestampFieldFormat and rec.TimeFormat() and rec.TimeFormatPtr()
// UNIXMICRO format style ... 1638645867123456.
TimeFormatUnixMicro = "UNIXMICRO"
)
func appendTimeFieldValue(dst []byte, t time.Time, format string) []byte {
const base = 10
switch format {
case "", TimeFormatUnixDecimal:
dst = strconv.AppendInt(dst, t.Unix(), base)
if nsec := t.Nanosecond(); nsec != 0 {
dst = append(dst, '.')
for i := base; nsec%i == 0; { // 0.100000000 -> 0.1
nsec /= i
}
dst = strconv.AppendInt(dst, int64(nsec), base)
}
case TimeFormatUnix:
dst = strconv.AppendInt(dst, t.Unix(), base)
case TimeFormatUnixMilli:
dst = strconv.AppendInt(dst, t.UnixMilli(), base)
case TimeFormatUnixMicro:
dst = strconv.AppendInt(dst, t.UnixMicro(), base)
default:
dst = append(t.AppendFormat(append(dst, '"'), format), '"')
}
return dst
}
func appendCaller(dst []byte, callerSkip int, useShortCaller bool) []byte {
pc := pcPool.Get().(*programcounter) // nolint: forcetypeassert
defer pcPool.Put(pc)
var frame runtime.Frame
if runtime.Callers(callerSkip, pc.PC) > 0 {
frame, _ = runtime.CallersFrames(pc.PC).Next()
}
return appendCallerFromFrame(dst, frame, useShortCaller)
}
// appendCallerFromFrame was split off from appendCaller in order to test different behaviors depending on the contents of the `runtime.Frame`.
func appendCallerFromFrame(dst []byte, frame runtime.Frame, useShortCaller bool) []byte {
const base = 10
if useShortCaller {
dst = appendJSONEscapedString(dst, extractShortPath(frame.File))
} else {
dst = appendJSONEscapedString(dst, frame.File)
}
dst = append(dst, ':')
dst = strconv.AppendInt(dst, int64(frame.Line), base)
return dst
}
func extractShortPath(path string) string {
// path == /path/to/directory/file
// ~ <- idx
idx := strings.LastIndexByte(path, '/')
if idx == -1 {
return path
}
// path[:idx] == /path/to/directory
// ~ <- idx
idx = strings.LastIndexByte(path[:idx], '/')
if idx == -1 {
return path
}
// path == /path/to/directory/file
// ~~~~~~~~~~~~~~ <- filepath[idx+1:]
return path[idx+1:]
}