-
Notifications
You must be signed in to change notification settings - Fork 42
/
tsv.go
229 lines (205 loc) · 5 KB
/
tsv.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package log
import (
"io"
"net"
"os"
"strconv"
"sync"
)
// TSVLogger represents an active logging object that generates lines of TSV output to an io.Writer.
type TSVLogger struct {
Separator byte
Writer io.Writer
}
// TSVEvent represents a tsv log event. It is instanced by one of TSVLogger and finalized by the Msg method.
type TSVEvent struct {
buf []byte
w io.Writer
sep byte
}
var tepool = sync.Pool{
New: func() interface{} {
return new(TSVEvent)
},
}
// New starts a new tsv message.
func (l *TSVLogger) New() (e *TSVEvent) {
e = tepool.Get().(*TSVEvent)
e.sep = l.Separator
if l.Writer != nil {
e.w = l.Writer
} else {
e.w = os.Stderr
}
if e.sep == 0 {
e.sep = '\t'
}
e.buf = e.buf[:0]
return
}
// Timestamp adds the current time as UNIX timestamp
func (e *TSVEvent) Timestamp() *TSVEvent {
i := len(e.buf)
e.buf = append(e.buf, "0123456789\t"...)
sec, _ := walltime()
// separator
e.buf[i+10] = e.sep
// seconds
is := sec % 100 * 2
sec /= 100
e.buf[i+9] = smallsString[is+1]
e.buf[i+8] = smallsString[is]
is = sec % 100 * 2
sec /= 100
e.buf[i+7] = smallsString[is+1]
e.buf[i+6] = smallsString[is]
is = sec % 100 * 2
sec /= 100
e.buf[i+5] = smallsString[is+1]
e.buf[i+4] = smallsString[is]
is = sec % 100 * 2
sec /= 100
e.buf[i+3] = smallsString[is+1]
e.buf[i+2] = smallsString[is]
is = sec % 100 * 2
e.buf[i+1] = smallsString[is+1]
e.buf[i] = smallsString[is]
return e
}
// TimestampMS adds the current time with milliseconds as UNIX timestamp
func (e *TSVEvent) TimestampMS() *TSVEvent {
i := len(e.buf)
e.buf = append(e.buf, "0123456789000\t"...)
sec, nsec := walltime()
// separator
e.buf[i+13] = e.sep
// milli seconds
a := int64(nsec) / 1000000
is := a % 100 * 2
e.buf[i+12] = smallsString[is+1]
e.buf[i+11] = smallsString[is]
e.buf[i+10] = byte('0' + a/100)
// seconds
is = sec % 100 * 2
sec /= 100
e.buf[i+9] = smallsString[is+1]
e.buf[i+8] = smallsString[is]
is = sec % 100 * 2
sec /= 100
e.buf[i+7] = smallsString[is+1]
e.buf[i+6] = smallsString[is]
is = sec % 100 * 2
sec /= 100
e.buf[i+5] = smallsString[is+1]
e.buf[i+4] = smallsString[is]
is = sec % 100 * 2
sec /= 100
e.buf[i+3] = smallsString[is+1]
e.buf[i+2] = smallsString[is]
is = sec % 100 * 2
e.buf[i+1] = smallsString[is+1]
e.buf[i] = smallsString[is]
return e
}
// Bool append the b as a bool to the event.
func (e *TSVEvent) Bool(b bool) *TSVEvent {
if b {
e.buf = append(e.buf, '1', e.sep)
} else {
e.buf = append(e.buf, '0', e.sep)
}
return e
}
// Byte append the b as a byte to the event.
func (e *TSVEvent) Byte(b byte) *TSVEvent {
e.buf = append(e.buf, b, e.sep)
return e
}
// Float64 adds a float64 to the event.
func (e *TSVEvent) Float64(f float64) *TSVEvent {
e.buf = strconv.AppendFloat(e.buf, f, 'f', -1, 64)
e.buf = append(e.buf, e.sep)
return e
}
// Int64 adds a int64 to the event.
func (e *TSVEvent) Int64(i int64) *TSVEvent {
e.buf = strconv.AppendInt(e.buf, i, 10)
e.buf = append(e.buf, e.sep)
return e
}
// Uint64 adds a uint64 to the event.
func (e *TSVEvent) Uint64(i uint64) *TSVEvent {
e.buf = strconv.AppendUint(e.buf, i, 10)
e.buf = append(e.buf, e.sep)
return e
}
// Float32 adds a float32 to the event.
func (e *TSVEvent) Float32(f float32) *TSVEvent {
return e.Float64(float64(f))
}
// Int adds a int to the event.
func (e *TSVEvent) Int(i int) *TSVEvent {
return e.Int64(int64(i))
}
// Int32 adds a int32 to the event.
func (e *TSVEvent) Int32(i int32) *TSVEvent {
return e.Int64(int64(i))
}
// Int16 adds a int16 to the event.
func (e *TSVEvent) Int16(i int16) *TSVEvent {
return e.Int64(int64(i))
}
// Int8 adds a int8 to the event.
func (e *TSVEvent) Int8(i int8) *TSVEvent {
return e.Int64(int64(i))
}
// Uint32 adds a uint32 to the event.
func (e *TSVEvent) Uint32(i uint32) *TSVEvent {
return e.Uint64(uint64(i))
}
// Uint16 adds a uint16 to the event.
func (e *TSVEvent) Uint16(i uint16) *TSVEvent {
return e.Uint64(uint64(i))
}
// Uint8 adds a uint8 to the event.
func (e *TSVEvent) Uint8(i uint8) *TSVEvent {
return e.Uint64(uint64(i))
}
// Str adds a string to the event.
func (e *TSVEvent) Str(val string) *TSVEvent {
e.buf = append(e.buf, val...)
e.buf = append(e.buf, e.sep)
return e
}
// Bytes adds a bytes as string to the event.
func (e *TSVEvent) Bytes(val []byte) *TSVEvent {
e.buf = append(e.buf, val...)
e.buf = append(e.buf, e.sep)
return e
}
// IPAddr adds IPv4 or IPv6 Address to the event
func (e *TSVEvent) IPAddr(ip net.IP) *TSVEvent {
if ip4 := ip.To4(); ip4 != nil {
e.buf = strconv.AppendInt(e.buf, int64(ip4[0]), 10)
e.buf = append(e.buf, '.')
e.buf = strconv.AppendInt(e.buf, int64(ip4[1]), 10)
e.buf = append(e.buf, '.')
e.buf = strconv.AppendInt(e.buf, int64(ip4[2]), 10)
e.buf = append(e.buf, '.')
e.buf = strconv.AppendInt(e.buf, int64(ip4[3]), 10)
} else {
e.buf = append(e.buf, ip.String()...)
}
e.buf = append(e.buf, e.sep)
return e
}
// Msg sends the event.
func (e *TSVEvent) Msg() {
if len(e.buf) != 0 {
e.buf[len(e.buf)-1] = '\n'
}
e.w.Write(e.buf)
if cap(e.buf) <= bbcap {
tepool.Put(e)
}
}