1// Copyright (c) 2016 Uber Technologies, Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21package zap
22
23import (
24	"sync"
25	"time"
26
27	"go.uber.org/zap/zapcore"
28)
29
30var _errArrayElemPool = sync.Pool{New: func() interface{} {
31	return &errArrayElem{}
32}}
33
34// Array constructs a field with the given key and ArrayMarshaler. It provides
35// a flexible, but still type-safe and efficient, way to add array-like types
36// to the logging context. The struct's MarshalLogArray method is called lazily.
37func Array(key string, val zapcore.ArrayMarshaler) zapcore.Field {
38	return zapcore.Field{Key: key, Type: zapcore.ArrayMarshalerType, Interface: val}
39}
40
41// Bools constructs a field that carries a slice of bools.
42func Bools(key string, bs []bool) zapcore.Field {
43	return Array(key, bools(bs))
44}
45
46// ByteStrings constructs a field that carries a slice of []byte, each of which
47// must be UTF-8 encoded text.
48func ByteStrings(key string, bss [][]byte) zapcore.Field {
49	return Array(key, byteStringsArray(bss))
50}
51
52// Complex128s constructs a field that carries a slice of complex numbers.
53func Complex128s(key string, nums []complex128) zapcore.Field {
54	return Array(key, complex128s(nums))
55}
56
57// Complex64s constructs a field that carries a slice of complex numbers.
58func Complex64s(key string, nums []complex64) zapcore.Field {
59	return Array(key, complex64s(nums))
60}
61
62// Durations constructs a field that carries a slice of time.Durations.
63func Durations(key string, ds []time.Duration) zapcore.Field {
64	return Array(key, durations(ds))
65}
66
67// Float64s constructs a field that carries a slice of floats.
68func Float64s(key string, nums []float64) zapcore.Field {
69	return Array(key, float64s(nums))
70}
71
72// Float32s constructs a field that carries a slice of floats.
73func Float32s(key string, nums []float32) zapcore.Field {
74	return Array(key, float32s(nums))
75}
76
77// Ints constructs a field that carries a slice of integers.
78func Ints(key string, nums []int) zapcore.Field {
79	return Array(key, ints(nums))
80}
81
82// Int64s constructs a field that carries a slice of integers.
83func Int64s(key string, nums []int64) zapcore.Field {
84	return Array(key, int64s(nums))
85}
86
87// Int32s constructs a field that carries a slice of integers.
88func Int32s(key string, nums []int32) zapcore.Field {
89	return Array(key, int32s(nums))
90}
91
92// Int16s constructs a field that carries a slice of integers.
93func Int16s(key string, nums []int16) zapcore.Field {
94	return Array(key, int16s(nums))
95}
96
97// Int8s constructs a field that carries a slice of integers.
98func Int8s(key string, nums []int8) zapcore.Field {
99	return Array(key, int8s(nums))
100}
101
102// Strings constructs a field that carries a slice of strings.
103func Strings(key string, ss []string) zapcore.Field {
104	return Array(key, stringArray(ss))
105}
106
107// Times constructs a field that carries a slice of time.Times.
108func Times(key string, ts []time.Time) zapcore.Field {
109	return Array(key, times(ts))
110}
111
112// Uints constructs a field that carries a slice of unsigned integers.
113func Uints(key string, nums []uint) zapcore.Field {
114	return Array(key, uints(nums))
115}
116
117// Uint64s constructs a field that carries a slice of unsigned integers.
118func Uint64s(key string, nums []uint64) zapcore.Field {
119	return Array(key, uint64s(nums))
120}
121
122// Uint32s constructs a field that carries a slice of unsigned integers.
123func Uint32s(key string, nums []uint32) zapcore.Field {
124	return Array(key, uint32s(nums))
125}
126
127// Uint16s constructs a field that carries a slice of unsigned integers.
128func Uint16s(key string, nums []uint16) zapcore.Field {
129	return Array(key, uint16s(nums))
130}
131
132// Uint8s constructs a field that carries a slice of unsigned integers.
133func Uint8s(key string, nums []uint8) zapcore.Field {
134	return Array(key, uint8s(nums))
135}
136
137// Uintptrs constructs a field that carries a slice of pointer addresses.
138func Uintptrs(key string, us []uintptr) zapcore.Field {
139	return Array(key, uintptrs(us))
140}
141
142// Errors constructs a field that carries a slice of errors.
143func Errors(key string, errs []error) zapcore.Field {
144	return Array(key, errArray(errs))
145}
146
147type bools []bool
148
149func (bs bools) MarshalLogArray(arr zapcore.ArrayEncoder) error {
150	for i := range bs {
151		arr.AppendBool(bs[i])
152	}
153	return nil
154}
155
156type byteStringsArray [][]byte
157
158func (bss byteStringsArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
159	for i := range bss {
160		arr.AppendByteString(bss[i])
161	}
162	return nil
163}
164
165type complex128s []complex128
166
167func (nums complex128s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
168	for i := range nums {
169		arr.AppendComplex128(nums[i])
170	}
171	return nil
172}
173
174type complex64s []complex64
175
176func (nums complex64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
177	for i := range nums {
178		arr.AppendComplex64(nums[i])
179	}
180	return nil
181}
182
183type durations []time.Duration
184
185func (ds durations) MarshalLogArray(arr zapcore.ArrayEncoder) error {
186	for i := range ds {
187		arr.AppendDuration(ds[i])
188	}
189	return nil
190}
191
192type float64s []float64
193
194func (nums float64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
195	for i := range nums {
196		arr.AppendFloat64(nums[i])
197	}
198	return nil
199}
200
201type float32s []float32
202
203func (nums float32s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
204	for i := range nums {
205		arr.AppendFloat32(nums[i])
206	}
207	return nil
208}
209
210type ints []int
211
212func (nums ints) MarshalLogArray(arr zapcore.ArrayEncoder) error {
213	for i := range nums {
214		arr.AppendInt(nums[i])
215	}
216	return nil
217}
218
219type int64s []int64
220
221func (nums int64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
222	for i := range nums {
223		arr.AppendInt64(nums[i])
224	}
225	return nil
226}
227
228type int32s []int32
229
230func (nums int32s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
231	for i := range nums {
232		arr.AppendInt32(nums[i])
233	}
234	return nil
235}
236
237type int16s []int16
238
239func (nums int16s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
240	for i := range nums {
241		arr.AppendInt16(nums[i])
242	}
243	return nil
244}
245
246type int8s []int8
247
248func (nums int8s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
249	for i := range nums {
250		arr.AppendInt8(nums[i])
251	}
252	return nil
253}
254
255type stringArray []string
256
257func (ss stringArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
258	for i := range ss {
259		arr.AppendString(ss[i])
260	}
261	return nil
262}
263
264type times []time.Time
265
266func (ts times) MarshalLogArray(arr zapcore.ArrayEncoder) error {
267	for i := range ts {
268		arr.AppendTime(ts[i])
269	}
270	return nil
271}
272
273type uints []uint
274
275func (nums uints) MarshalLogArray(arr zapcore.ArrayEncoder) error {
276	for i := range nums {
277		arr.AppendUint(nums[i])
278	}
279	return nil
280}
281
282type uint64s []uint64
283
284func (nums uint64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
285	for i := range nums {
286		arr.AppendUint64(nums[i])
287	}
288	return nil
289}
290
291type uint32s []uint32
292
293func (nums uint32s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
294	for i := range nums {
295		arr.AppendUint32(nums[i])
296	}
297	return nil
298}
299
300type uint16s []uint16
301
302func (nums uint16s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
303	for i := range nums {
304		arr.AppendUint16(nums[i])
305	}
306	return nil
307}
308
309type uint8s []uint8
310
311func (nums uint8s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
312	for i := range nums {
313		arr.AppendUint8(nums[i])
314	}
315	return nil
316}
317
318type uintptrs []uintptr
319
320func (nums uintptrs) MarshalLogArray(arr zapcore.ArrayEncoder) error {
321	for i := range nums {
322		arr.AppendUintptr(nums[i])
323	}
324	return nil
325}
326
327type errArray []error
328
329func (errs errArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
330	for i := range errs {
331		if errs[i] == nil {
332			continue
333		}
334		// To represent each error as an object with an "error" attribute and
335		// potentially an "errorVerbose" attribute, we need to wrap it in a
336		// type that implements LogObjectMarshaler. To prevent this from
337		// allocating, pool the wrapper type.
338		elem := _errArrayElemPool.Get().(*errArrayElem)
339		elem.error = errs[i]
340		arr.AppendObject(elem)
341		elem.error = nil
342		_errArrayElemPool.Put(elem)
343	}
344	return nil
345}
346
347type errArrayElem struct {
348	error
349}
350
351func (e *errArrayElem) MarshalLogObject(enc zapcore.ObjectEncoder) error {
352	// Re-use the error field's logic, which supports non-standard error types.
353	Error(e.error).AddTo(enc)
354	return nil
355}
356