2022-03-20 19:19:42 +00:00
|
|
|
package zlog
|
2017-07-26 07:14:43 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-03 21:07:18 +00:00
|
|
|
"net"
|
2017-07-26 07:14:43 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var arrayPool = &sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &Array{
|
|
|
|
buf: make([]byte, 0, 500),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-28 18:49:41 +00:00
|
|
|
// Array is used to prepopulate an array of items
|
|
|
|
// which can be re-used to add to log messages.
|
2017-07-26 07:14:43 +00:00
|
|
|
type Array struct {
|
|
|
|
buf []byte
|
|
|
|
}
|
|
|
|
|
2018-09-19 07:18:07 +00:00
|
|
|
func putArray(a *Array) {
|
|
|
|
// Proper usage of a sync.Pool requires each entry to have approximately
|
|
|
|
// the same memory cost. To obtain this property when the stored type
|
|
|
|
// contains a variably-sized buffer, we add a hard limit on the maximum buffer
|
|
|
|
// to place back in the pool.
|
|
|
|
//
|
|
|
|
// See https://golang.org/issue/23199
|
|
|
|
const maxSize = 1 << 16 // 64KiB
|
|
|
|
if cap(a.buf) > maxSize {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
arrayPool.Put(a)
|
|
|
|
}
|
|
|
|
|
2017-07-26 07:14:43 +00:00
|
|
|
// Arr creates an array to be added to an Event or Context.
|
|
|
|
func Arr() *Array {
|
|
|
|
a := arrayPool.Get().(*Array)
|
|
|
|
a.buf = a.buf[:0]
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2018-03-28 18:49:41 +00:00
|
|
|
// MarshalZerologArray method here is no-op - since data is
|
|
|
|
// already in the needed format.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (*Array) MarshalZerologArray(*Array) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Array) write(dst []byte) []byte {
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendArrayStart(dst)
|
2018-03-28 18:49:41 +00:00
|
|
|
if len(a.buf) > 0 {
|
2021-12-21 12:07:54 +00:00
|
|
|
dst = append(dst, a.buf...)
|
2017-07-26 07:14:43 +00:00
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendArrayEnd(dst)
|
2018-09-19 07:18:07 +00:00
|
|
|
putArray(a)
|
2017-07-26 07:14:43 +00:00
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object marshals an object that implement the LogObjectMarshaler
|
2023-01-27 23:04:14 +00:00
|
|
|
// interface and appends it to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Object(obj LogObjectMarshaler) *Array {
|
|
|
|
e := Dict()
|
|
|
|
obj.MarshalZerologObject(e)
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendEndMarker(e.buf)
|
|
|
|
a.buf = append(enc.AppendArrayDelim(a.buf), e.buf...)
|
2018-09-19 07:18:07 +00:00
|
|
|
putEvent(e)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Str appends the val as a string to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Str(val string) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), val)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Bytes appends the val as a string to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Bytes(val []byte) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendBytes(enc.AppendArrayDelim(a.buf), val)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Hex appends the val as a hex string to the array.
|
2018-03-15 17:29:26 +00:00
|
|
|
func (a *Array) Hex(val []byte) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendHex(enc.AppendArrayDelim(a.buf), val)
|
2018-03-15 17:29:26 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2019-11-04 17:47:06 +00:00
|
|
|
// RawJSON adds already encoded JSON to the array.
|
|
|
|
func (a *Array) RawJSON(val []byte) *Array {
|
|
|
|
a.buf = appendJSON(enc.AppendArrayDelim(a.buf), val)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2018-07-02 19:46:01 +00:00
|
|
|
// Err serializes and appends the err to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Err(err error) *Array {
|
2020-02-11 01:14:39 +00:00
|
|
|
switch m := ErrorMarshalFunc(err).(type) {
|
2018-07-02 19:46:01 +00:00
|
|
|
case LogObjectMarshaler:
|
|
|
|
e := newEvent(nil, 0)
|
|
|
|
e.buf = e.buf[:0]
|
|
|
|
e.appendObject(m)
|
|
|
|
a.buf = append(enc.AppendArrayDelim(a.buf), e.buf...)
|
2018-09-19 07:18:07 +00:00
|
|
|
putEvent(e)
|
2018-07-02 19:46:01 +00:00
|
|
|
case error:
|
2020-02-11 01:14:39 +00:00
|
|
|
if m == nil || isNilValue(m) {
|
|
|
|
a.buf = enc.AppendNil(enc.AppendArrayDelim(a.buf))
|
|
|
|
} else {
|
|
|
|
a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m.Error())
|
|
|
|
}
|
2018-07-02 19:46:01 +00:00
|
|
|
case string:
|
|
|
|
a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m)
|
|
|
|
default:
|
|
|
|
a.buf = enc.AppendInterface(enc.AppendArrayDelim(a.buf), m)
|
|
|
|
}
|
|
|
|
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Bool appends the val as a bool to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Bool(b bool) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendBool(enc.AppendArrayDelim(a.buf), b)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Int appends i as a int to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Int(i int) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendInt(enc.AppendArrayDelim(a.buf), i)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Int8 appends i as a int8 to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Int8(i int8) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendInt8(enc.AppendArrayDelim(a.buf), i)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Int16 appends i as a int16 to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Int16(i int16) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendInt16(enc.AppendArrayDelim(a.buf), i)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Int32 appends i as a int32 to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Int32(i int32) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendInt32(enc.AppendArrayDelim(a.buf), i)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Int64 appends i as a int64 to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Int64(i int64) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendInt64(enc.AppendArrayDelim(a.buf), i)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Uint appends i as a uint to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Uint(i uint) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendUint(enc.AppendArrayDelim(a.buf), i)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Uint8 appends i as a uint8 to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Uint8(i uint8) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendUint8(enc.AppendArrayDelim(a.buf), i)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Uint16 appends i as a uint16 to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Uint16(i uint16) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendUint16(enc.AppendArrayDelim(a.buf), i)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Uint32 appends i as a uint32 to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Uint32(i uint32) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendUint32(enc.AppendArrayDelim(a.buf), i)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Uint64 appends i as a uint64 to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Uint64(i uint64) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendUint64(enc.AppendArrayDelim(a.buf), i)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Float32 appends f as a float32 to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Float32(f float32) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendFloat32(enc.AppendArrayDelim(a.buf), f)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Float64 appends f as a float64 to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Float64(f float64) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendFloat64(enc.AppendArrayDelim(a.buf), f)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-03-19 11:53:15 +00:00
|
|
|
// Time appends t formatted as string using zlog.TimeFieldFormat.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Time(t time.Time) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendTime(enc.AppendArrayDelim(a.buf), t, TimeFieldFormat)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Dur appends d to the array.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Dur(d time.Duration) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendDuration(enc.AppendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldInteger)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-01-27 23:04:14 +00:00
|
|
|
// Interface appends i marshaled using reflection.
|
2017-07-26 07:14:43 +00:00
|
|
|
func (a *Array) Interface(i interface{}) *Array {
|
|
|
|
if obj, ok := i.(LogObjectMarshaler); ok {
|
|
|
|
return a.Object(obj)
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendInterface(enc.AppendArrayDelim(a.buf), i)
|
2017-07-26 07:14:43 +00:00
|
|
|
return a
|
|
|
|
}
|
2018-04-03 21:07:18 +00:00
|
|
|
|
|
|
|
// IPAddr adds IPv4 or IPv6 address to the array
|
|
|
|
func (a *Array) IPAddr(ip net.IP) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendIPAddr(enc.AppendArrayDelim(a.buf), ip)
|
2018-04-03 21:07:18 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPPrefix adds IPv4 or IPv6 Prefix (IP + mask) to the array
|
|
|
|
func (a *Array) IPPrefix(pfx net.IPNet) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendIPPrefix(enc.AppendArrayDelim(a.buf), pfx)
|
2018-04-03 21:07:18 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// MACAddr adds a MAC (Ethernet) address to the array
|
|
|
|
func (a *Array) MACAddr(ha net.HardwareAddr) *Array {
|
2018-05-10 22:01:41 +00:00
|
|
|
a.buf = enc.AppendMACAddr(enc.AppendArrayDelim(a.buf), ha)
|
2018-04-03 21:07:18 +00:00
|
|
|
return a
|
|
|
|
}
|
2021-11-02 00:53:02 +00:00
|
|
|
|
|
|
|
// Dict adds the dict Event to the array
|
|
|
|
func (a *Array) Dict(dict *Event) *Array {
|
|
|
|
dict.buf = enc.AppendEndMarker(dict.buf)
|
|
|
|
a.buf = append(enc.AppendArrayDelim(a.buf), dict.buf...)
|
|
|
|
return a
|
|
|
|
}
|