1package zerolog 2 3import ( 4 "net" 5 "sync" 6 "time" 7) 8 9var arrayPool = &sync.Pool{ 10 New: func() interface{} { 11 return &Array{ 12 buf: make([]byte, 0, 500), 13 } 14 }, 15} 16 17// Array is used to prepopulate an array of items 18// which can be re-used to add to log messages. 19type Array struct { 20 buf []byte 21} 22 23func putArray(a *Array) { 24 // Proper usage of a sync.Pool requires each entry to have approximately 25 // the same memory cost. To obtain this property when the stored type 26 // contains a variably-sized buffer, we add a hard limit on the maximum buffer 27 // to place back in the pool. 28 // 29 // See https://golang.org/issue/23199 30 const maxSize = 1 << 16 // 64KiB 31 if cap(a.buf) > maxSize { 32 return 33 } 34 arrayPool.Put(a) 35} 36 37// Arr creates an array to be added to an Event or Context. 38func Arr() *Array { 39 a := arrayPool.Get().(*Array) 40 a.buf = a.buf[:0] 41 return a 42} 43 44// MarshalZerologArray method here is no-op - since data is 45// already in the needed format. 46func (*Array) MarshalZerologArray(*Array) { 47} 48 49func (a *Array) write(dst []byte) []byte { 50 dst = enc.AppendArrayStart(dst) 51 if len(a.buf) > 0 { 52 dst = append(append(dst, a.buf...)) 53 } 54 dst = enc.AppendArrayEnd(dst) 55 putArray(a) 56 return dst 57} 58 59// Object marshals an object that implement the LogObjectMarshaler 60// interface and append append it to the array. 61func (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// Str append append the val as a string to the array. 71func (a *Array) Str(val string) *Array { 72 a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), val) 73 return a 74} 75 76// Bytes append append the val as a string to the array. 77func (a *Array) Bytes(val []byte) *Array { 78 a.buf = enc.AppendBytes(enc.AppendArrayDelim(a.buf), val) 79 return a 80} 81 82// Hex append append the val as a hex string to the array. 83func (a *Array) Hex(val []byte) *Array { 84 a.buf = enc.AppendHex(enc.AppendArrayDelim(a.buf), val) 85 return a 86} 87 88// Err serializes and appends the err to the array. 89func (a *Array) Err(err error) *Array { 90 marshaled := ErrorMarshalFunc(err) 91 switch m := marshaled.(type) { 92 case LogObjectMarshaler: 93 e := newEvent(nil, 0) 94 e.buf = e.buf[:0] 95 e.appendObject(m) 96 a.buf = append(enc.AppendArrayDelim(a.buf), e.buf...) 97 putEvent(e) 98 case error: 99 a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m.Error()) 100 case string: 101 a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m) 102 default: 103 a.buf = enc.AppendInterface(enc.AppendArrayDelim(a.buf), m) 104 } 105 106 return a 107} 108 109// Bool append append the val as a bool to the array. 110func (a *Array) Bool(b bool) *Array { 111 a.buf = enc.AppendBool(enc.AppendArrayDelim(a.buf), b) 112 return a 113} 114 115// Int append append i as a int to the array. 116func (a *Array) Int(i int) *Array { 117 a.buf = enc.AppendInt(enc.AppendArrayDelim(a.buf), i) 118 return a 119} 120 121// Int8 append append i as a int8 to the array. 122func (a *Array) Int8(i int8) *Array { 123 a.buf = enc.AppendInt8(enc.AppendArrayDelim(a.buf), i) 124 return a 125} 126 127// Int16 append append i as a int16 to the array. 128func (a *Array) Int16(i int16) *Array { 129 a.buf = enc.AppendInt16(enc.AppendArrayDelim(a.buf), i) 130 return a 131} 132 133// Int32 append append i as a int32 to the array. 134func (a *Array) Int32(i int32) *Array { 135 a.buf = enc.AppendInt32(enc.AppendArrayDelim(a.buf), i) 136 return a 137} 138 139// Int64 append append i as a int64 to the array. 140func (a *Array) Int64(i int64) *Array { 141 a.buf = enc.AppendInt64(enc.AppendArrayDelim(a.buf), i) 142 return a 143} 144 145// Uint append append i as a uint to the array. 146func (a *Array) Uint(i uint) *Array { 147 a.buf = enc.AppendUint(enc.AppendArrayDelim(a.buf), i) 148 return a 149} 150 151// Uint8 append append i as a uint8 to the array. 152func (a *Array) Uint8(i uint8) *Array { 153 a.buf = enc.AppendUint8(enc.AppendArrayDelim(a.buf), i) 154 return a 155} 156 157// Uint16 append append i as a uint16 to the array. 158func (a *Array) Uint16(i uint16) *Array { 159 a.buf = enc.AppendUint16(enc.AppendArrayDelim(a.buf), i) 160 return a 161} 162 163// Uint32 append append i as a uint32 to the array. 164func (a *Array) Uint32(i uint32) *Array { 165 a.buf = enc.AppendUint32(enc.AppendArrayDelim(a.buf), i) 166 return a 167} 168 169// Uint64 append append i as a uint64 to the array. 170func (a *Array) Uint64(i uint64) *Array { 171 a.buf = enc.AppendUint64(enc.AppendArrayDelim(a.buf), i) 172 return a 173} 174 175// Float32 append append f as a float32 to the array. 176func (a *Array) Float32(f float32) *Array { 177 a.buf = enc.AppendFloat32(enc.AppendArrayDelim(a.buf), f) 178 return a 179} 180 181// Float64 append append f as a float64 to the array. 182func (a *Array) Float64(f float64) *Array { 183 a.buf = enc.AppendFloat64(enc.AppendArrayDelim(a.buf), f) 184 return a 185} 186 187// Time append append t formated as string using zerolog.TimeFieldFormat. 188func (a *Array) Time(t time.Time) *Array { 189 a.buf = enc.AppendTime(enc.AppendArrayDelim(a.buf), t, TimeFieldFormat) 190 return a 191} 192 193// Dur append append d to the array. 194func (a *Array) Dur(d time.Duration) *Array { 195 a.buf = enc.AppendDuration(enc.AppendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldInteger) 196 return a 197} 198 199// Interface append append i marshaled using reflection. 200func (a *Array) Interface(i interface{}) *Array { 201 if obj, ok := i.(LogObjectMarshaler); ok { 202 return a.Object(obj) 203 } 204 a.buf = enc.AppendInterface(enc.AppendArrayDelim(a.buf), i) 205 return a 206} 207 208// IPAddr adds IPv4 or IPv6 address to the array 209func (a *Array) IPAddr(ip net.IP) *Array { 210 a.buf = enc.AppendIPAddr(enc.AppendArrayDelim(a.buf), ip) 211 return a 212} 213 214// IPPrefix adds IPv4 or IPv6 Prefix (IP + mask) to the array 215func (a *Array) IPPrefix(pfx net.IPNet) *Array { 216 a.buf = enc.AppendIPPrefix(enc.AppendArrayDelim(a.buf), pfx) 217 return a 218} 219 220// MACAddr adds a MAC (Ethernet) address to the array 221func (a *Array) MACAddr(ha net.HardwareAddr) *Array { 222 a.buf = enc.AppendMACAddr(enc.AppendArrayDelim(a.buf), ha) 223 return a 224} 225