-
Notifications
You must be signed in to change notification settings - Fork 5
/
logrecord.go
247 lines (209 loc) · 4.67 KB
/
logrecord.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"github.com/deafbybeheading/femebe/buf"
)
type logRecord struct {
LogTime string
UserName *string
DatabaseName *string
Pid int32
ClientAddr *string
SessionId string
SeqNum int64
PsDisplay *string
SessionStart string
Vxid *string
Txid uint64
ELevel int32
SQLState *string
ErrMessage *string
ErrDetail *string
ErrHint *string
InternalQuery *string
InternalQueryPos int32
ErrContext *string
UserQuery *string
UserQueryPos int32
FileErrPos *string
ApplicationName *string
}
func (lr *logRecord) oneLine() []byte {
buf := bytes.Buffer{}
wd := func() {
buf.WriteByte(' ')
}
ws := func(name string, s string) {
buf.WriteString(fmt.Sprintf("%s=%q", name, s))
}
wns := func(name string, s *string) {
body := func() string {
if s == nil {
return "NULL"
}
return fmt.Sprintf("[%q]", *s)
}()
buf.WriteString(name)
buf.WriteByte('=')
buf.WriteString(body)
}
wnum := func(name string, n interface{}) {
buf.WriteString(fmt.Sprintf("%s=%d", name, n))
}
ws("LogTime", lr.LogTime)
wd()
wns("UserName", lr.UserName)
wd()
wns("DatabaseName", lr.DatabaseName)
wd()
wnum("Pid", lr.Pid)
wd()
wns("ClientAddr", lr.ClientAddr)
wd()
ws("SessionId", lr.SessionId)
wd()
wnum("SeqNum", lr.SeqNum)
wd()
wns("PsDisplay", lr.PsDisplay)
wd()
ws("SessionStart", lr.SessionStart)
wd()
wns("Vxid", lr.Vxid)
wd()
wnum("Txid", lr.Txid)
wd()
wnum("ELevel", lr.ELevel)
wd()
wns("SQLState", lr.SQLState)
wd()
wns("ErrMessage", lr.ErrMessage)
wd()
wns("ErrDetail", lr.ErrDetail)
wd()
wns("ErrHint", lr.ErrHint)
wd()
wns("InternalQuery", lr.InternalQuery)
wd()
wnum("InternalQueryPos", lr.InternalQueryPos)
wd()
wns("ErrContext", lr.ErrContext)
wd()
wns("UserQuery", lr.UserQuery)
wd()
wnum("UserQueryPos", lr.UserQueryPos)
wd()
wns("FileErrPos", lr.FileErrPos)
wd()
wns("ApplicationName", lr.ApplicationName)
return buf.Bytes()
}
func readInt64(r io.Reader) (ret int64, err error) {
var be [8]byte
valBytes := be[0:8]
if _, err = io.ReadFull(r, valBytes); err != nil {
return 0, err
}
return int64(binary.BigEndian.Uint64(valBytes)), nil
}
func readUint64(r io.Reader) (ret uint64, err error) {
var be [8]byte
valBytes := be[0:8]
if _, err = io.ReadFull(r, valBytes); err != nil {
return 0, err
}
return binary.BigEndian.Uint64(valBytes), nil
}
func parseLogRecord(
dst *logRecord, data []byte, exit exitFn) {
b := bytes.NewBuffer(data)
// Read the next nullable string from b, returning a 'nil'
// *string should it be null.
nextNullableString := func() *string {
np, err := buf.ReadByte(b)
if err != nil {
exit(err)
}
switch np {
case 'P':
s, err := buf.ReadCString(b)
if err != nil {
exit(err)
}
return &s
case 'N':
// 'N' is still followed by a NUL byte that
// must be consumed.
_, err := buf.ReadCString(b)
if err != nil {
exit(err)
}
return nil
default:
exit("Expected nullable string "+
"control character, got %c", np)
}
exit("Prior switch should always return")
panic("exit should panic/return, " +
"but the compiler doesn't know that")
}
// Read a non-nullable string from b
nextString := func() string {
s, err := buf.ReadCString(b)
if err != nil {
exit(err)
}
return s
}
nextInt32 := func() int32 {
i32, err := buf.ReadInt32(b)
if err != nil {
exit(err)
}
return i32
}
nextInt64 := func() int64 {
i64, err := readInt64(b)
if err != nil {
exit(err)
}
return i64
}
nextUint64 := func() uint64 {
ui64, err := readUint64(b)
if err != nil {
exit(err)
}
return ui64
}
dst.LogTime = nextString()
dst.UserName = nextNullableString()
dst.DatabaseName = nextNullableString()
dst.Pid = nextInt32()
dst.ClientAddr = nextNullableString()
dst.SessionId = nextString()
dst.SeqNum = nextInt64()
dst.PsDisplay = nextNullableString()
dst.SessionStart = nextString()
dst.Vxid = nextNullableString()
dst.Txid = nextUint64()
dst.ELevel = nextInt32()
dst.SQLState = nextNullableString()
dst.ErrMessage = nextNullableString()
dst.ErrDetail = nextNullableString()
dst.ErrHint = nextNullableString()
dst.InternalQuery = nextNullableString()
dst.InternalQueryPos = nextInt32()
dst.ErrContext = nextNullableString()
dst.UserQuery = nextNullableString()
dst.UserQueryPos = nextInt32()
dst.FileErrPos = nextNullableString()
dst.ApplicationName = nextNullableString()
if b.Len() != 0 {
exit("LogRecord message has mismatched "+
"length header and cString contents: remaining %d",
b.Len())
}
}