...
1 package zerolog
2
3 import (
4 "net"
5 "sync"
6 "time"
7 )
8
9 var arrayPool = &sync.Pool{
10 New: func() interface{} {
11 return &Array{
12 buf: make([]byte, 0, 500),
13 }
14 },
15 }
16
17
18
19 type Array struct {
20 buf []byte
21 }
22
23 func putArray(a *Array) {
24
25
26
27
28
29
30 const maxSize = 1 << 16
31 if cap(a.buf) > maxSize {
32 return
33 }
34 arrayPool.Put(a)
35 }
36
37
38 func Arr() *Array {
39 a := arrayPool.Get().(*Array)
40 a.buf = a.buf[:0]
41 return a
42 }
43
44
45
46 func (*Array) MarshalZerologArray(*Array) {
47 }
48
49 func (a *Array) write(dst []byte) []byte {
50 dst = enc.AppendArrayStart(dst)
51 if len(a.buf) > 0 {
52 dst = append(dst, a.buf...)
53 }
54 dst = enc.AppendArrayEnd(dst)
55 putArray(a)
56 return dst
57 }
58
59
60
61 func (a *Array) Object(obj LogObjectMarshaler) *Array {
62 e := Dict()
63 obj.MarshalZerologObject(e)
64 e.buf = enc.AppendEndMarker(e.buf)
65 a.buf = append(enc.AppendArrayDelim(a.buf), e.buf...)
66 putEvent(e)
67 return a
68 }
69
70
71 func (a *Array) Str(val string) *Array {
72 a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), val)
73 return a
74 }
75
76
77 func (a *Array) Bytes(val []byte) *Array {
78 a.buf = enc.AppendBytes(enc.AppendArrayDelim(a.buf), val)
79 return a
80 }
81
82
83 func (a *Array) Hex(val []byte) *Array {
84 a.buf = enc.AppendHex(enc.AppendArrayDelim(a.buf), val)
85 return a
86 }
87
88
89 func (a *Array) RawJSON(val []byte) *Array {
90 a.buf = appendJSON(enc.AppendArrayDelim(a.buf), val)
91 return a
92 }
93
94
95 func (a *Array) Err(err error) *Array {
96 switch m := ErrorMarshalFunc(err).(type) {
97 case LogObjectMarshaler:
98 e := newEvent(nil, 0)
99 e.buf = e.buf[:0]
100 e.appendObject(m)
101 a.buf = append(enc.AppendArrayDelim(a.buf), e.buf...)
102 putEvent(e)
103 case error:
104 if m == nil || isNilValue(m) {
105 a.buf = enc.AppendNil(enc.AppendArrayDelim(a.buf))
106 } else {
107 a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m.Error())
108 }
109 case string:
110 a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m)
111 default:
112 a.buf = enc.AppendInterface(enc.AppendArrayDelim(a.buf), m)
113 }
114
115 return a
116 }
117
118
119 func (a *Array) Bool(b bool) *Array {
120 a.buf = enc.AppendBool(enc.AppendArrayDelim(a.buf), b)
121 return a
122 }
123
124
125 func (a *Array) Int(i int) *Array {
126 a.buf = enc.AppendInt(enc.AppendArrayDelim(a.buf), i)
127 return a
128 }
129
130
131 func (a *Array) Int8(i int8) *Array {
132 a.buf = enc.AppendInt8(enc.AppendArrayDelim(a.buf), i)
133 return a
134 }
135
136
137 func (a *Array) Int16(i int16) *Array {
138 a.buf = enc.AppendInt16(enc.AppendArrayDelim(a.buf), i)
139 return a
140 }
141
142
143 func (a *Array) Int32(i int32) *Array {
144 a.buf = enc.AppendInt32(enc.AppendArrayDelim(a.buf), i)
145 return a
146 }
147
148
149 func (a *Array) Int64(i int64) *Array {
150 a.buf = enc.AppendInt64(enc.AppendArrayDelim(a.buf), i)
151 return a
152 }
153
154
155 func (a *Array) Uint(i uint) *Array {
156 a.buf = enc.AppendUint(enc.AppendArrayDelim(a.buf), i)
157 return a
158 }
159
160
161 func (a *Array) Uint8(i uint8) *Array {
162 a.buf = enc.AppendUint8(enc.AppendArrayDelim(a.buf), i)
163 return a
164 }
165
166
167 func (a *Array) Uint16(i uint16) *Array {
168 a.buf = enc.AppendUint16(enc.AppendArrayDelim(a.buf), i)
169 return a
170 }
171
172
173 func (a *Array) Uint32(i uint32) *Array {
174 a.buf = enc.AppendUint32(enc.AppendArrayDelim(a.buf), i)
175 return a
176 }
177
178
179 func (a *Array) Uint64(i uint64) *Array {
180 a.buf = enc.AppendUint64(enc.AppendArrayDelim(a.buf), i)
181 return a
182 }
183
184
185 func (a *Array) Float32(f float32) *Array {
186 a.buf = enc.AppendFloat32(enc.AppendArrayDelim(a.buf), f)
187 return a
188 }
189
190
191 func (a *Array) Float64(f float64) *Array {
192 a.buf = enc.AppendFloat64(enc.AppendArrayDelim(a.buf), f)
193 return a
194 }
195
196
197 func (a *Array) Time(t time.Time) *Array {
198 a.buf = enc.AppendTime(enc.AppendArrayDelim(a.buf), t, TimeFieldFormat)
199 return a
200 }
201
202
203 func (a *Array) Dur(d time.Duration) *Array {
204 a.buf = enc.AppendDuration(enc.AppendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldInteger)
205 return a
206 }
207
208
209 func (a *Array) Interface(i interface{}) *Array {
210 if obj, ok := i.(LogObjectMarshaler); ok {
211 return a.Object(obj)
212 }
213 a.buf = enc.AppendInterface(enc.AppendArrayDelim(a.buf), i)
214 return a
215 }
216
217
218 func (a *Array) IPAddr(ip net.IP) *Array {
219 a.buf = enc.AppendIPAddr(enc.AppendArrayDelim(a.buf), ip)
220 return a
221 }
222
223
224 func (a *Array) IPPrefix(pfx net.IPNet) *Array {
225 a.buf = enc.AppendIPPrefix(enc.AppendArrayDelim(a.buf), pfx)
226 return a
227 }
228
229
230 func (a *Array) MACAddr(ha net.HardwareAddr) *Array {
231 a.buf = enc.AppendMACAddr(enc.AppendArrayDelim(a.buf), ha)
232 return a
233 }
234
235
236 func (a *Array) Dict(dict *Event) *Array {
237 dict.buf = enc.AppendEndMarker(dict.buf)
238 a.buf = append(enc.AppendArrayDelim(a.buf), dict.buf...)
239 return a
240 }
241
View as plain text