2017-07-26 03:51:49 +00:00
|
|
|
package zerolog
|
|
|
|
|
|
|
|
import (
|
2021-08-03 09:31:54 +00:00
|
|
|
"encoding/json"
|
2018-04-03 21:07:18 +00:00
|
|
|
"net"
|
2017-07-26 03:51:49 +00:00
|
|
|
"sort"
|
|
|
|
"time"
|
2020-02-11 01:14:39 +00:00
|
|
|
"unsafe"
|
2017-07-26 03:51:49 +00:00
|
|
|
)
|
|
|
|
|
2020-02-11 01:14:39 +00:00
|
|
|
func isNilValue(i interface{}) bool {
|
|
|
|
return (*[2]uintptr)(unsafe.Pointer(&i))[1] == 0
|
|
|
|
}
|
|
|
|
|
2021-09-08 16:00:06 +00:00
|
|
|
func appendFields(dst []byte, fields interface{}) []byte {
|
|
|
|
switch fields := fields.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
if n := len(fields); n&0x1 == 1 { // odd number
|
|
|
|
fields = fields[:n-1]
|
|
|
|
}
|
|
|
|
dst = appendFieldList(dst, fields)
|
|
|
|
case map[string]interface{}:
|
|
|
|
keys := make([]string, 0, len(fields))
|
|
|
|
for key := range fields {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
kv := make([]interface{}, 2)
|
|
|
|
for _, key := range keys {
|
|
|
|
kv[0], kv[1] = key, fields[key]
|
|
|
|
dst = appendFieldList(dst, kv)
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
}
|
2021-09-08 16:00:06 +00:00
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendFieldList(dst []byte, kvList []interface{}) []byte {
|
|
|
|
for i, n := 0, len(kvList); i < n; i += 2 {
|
|
|
|
key, val := kvList[i], kvList[i+1]
|
|
|
|
if key, ok := key.(string); ok {
|
|
|
|
dst = enc.AppendKey(dst, key)
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
2018-05-09 10:51:52 +00:00
|
|
|
if val, ok := val.(LogObjectMarshaler); ok {
|
|
|
|
e := newEvent(nil, 0)
|
|
|
|
e.buf = e.buf[:0]
|
|
|
|
e.appendObject(val)
|
|
|
|
dst = append(dst, e.buf...)
|
2018-09-19 07:18:07 +00:00
|
|
|
putEvent(e)
|
2018-05-09 10:51:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch val := val.(type) {
|
2017-07-26 03:51:49 +00:00
|
|
|
case string:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendString(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []byte:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendBytes(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case error:
|
2020-02-11 01:14:39 +00:00
|
|
|
switch m := ErrorMarshalFunc(val).(type) {
|
2018-07-02 19:46:01 +00:00
|
|
|
case LogObjectMarshaler:
|
|
|
|
e := newEvent(nil, 0)
|
|
|
|
e.buf = e.buf[:0]
|
|
|
|
e.appendObject(m)
|
|
|
|
dst = append(dst, 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) {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendString(dst, m.Error())
|
|
|
|
}
|
2018-07-02 19:46:01 +00:00
|
|
|
case string:
|
|
|
|
dst = enc.AppendString(dst, m)
|
|
|
|
default:
|
|
|
|
dst = enc.AppendInterface(dst, m)
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
case []error:
|
2018-07-02 19:46:01 +00:00
|
|
|
dst = enc.AppendArrayStart(dst)
|
|
|
|
for i, err := range val {
|
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)
|
|
|
|
dst = append(dst, 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) {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendString(dst, m.Error())
|
|
|
|
}
|
2018-07-02 19:46:01 +00:00
|
|
|
case string:
|
|
|
|
dst = enc.AppendString(dst, m)
|
|
|
|
default:
|
|
|
|
dst = enc.AppendInterface(dst, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i < (len(val) - 1) {
|
|
|
|
enc.AppendArrayDelim(dst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst = enc.AppendArrayEnd(dst)
|
2017-07-26 03:51:49 +00:00
|
|
|
case bool:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendBool(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case int:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendInt(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case int8:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendInt8(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case int16:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendInt16(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case int32:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendInt32(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case int64:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendInt64(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case uint:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendUint(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case uint8:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendUint8(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case uint16:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendUint16(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case uint32:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendUint32(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case uint64:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendUint64(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case float32:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendFloat32(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case float64:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendFloat64(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case time.Time:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendTime(dst, val, TimeFieldFormat)
|
2017-07-26 03:51:49 +00:00
|
|
|
case time.Duration:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendDuration(dst, val, DurationFieldUnit, DurationFieldInteger)
|
2018-03-26 03:18:47 +00:00
|
|
|
case *string:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendString(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *bool:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendBool(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *int:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendInt(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *int8:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendInt8(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *int16:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendInt16(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *int32:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendInt32(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *int64:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendInt64(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *uint:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendUint(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *uint8:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendUint8(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *uint16:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendUint16(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *uint32:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendUint32(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *uint64:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendUint64(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *float32:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendFloat32(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *float64:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendFloat64(dst, *val)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *time.Time:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendTime(dst, *val, TimeFieldFormat)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2018-03-26 03:18:47 +00:00
|
|
|
case *time.Duration:
|
2018-10-31 17:40:46 +00:00
|
|
|
if val != nil {
|
|
|
|
dst = enc.AppendDuration(dst, *val, DurationFieldUnit, DurationFieldInteger)
|
|
|
|
} else {
|
|
|
|
dst = enc.AppendNil(dst)
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
case []string:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendStrings(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []bool:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendBools(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []int:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendInts(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []int8:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendInts8(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []int16:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendInts16(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []int32:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendInts32(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []int64:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendInts64(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []uint:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendUints(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
// case []uint8:
|
2018-05-10 22:01:41 +00:00
|
|
|
// dst = enc.AppendUints8(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []uint16:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendUints16(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []uint32:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendUints32(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []uint64:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendUints64(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []float32:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendFloats32(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []float64:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendFloats64(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []time.Time:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendTimes(dst, val, TimeFieldFormat)
|
2017-07-26 03:51:49 +00:00
|
|
|
case []time.Duration:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendDurations(dst, val, DurationFieldUnit, DurationFieldInteger)
|
2017-07-26 03:51:49 +00:00
|
|
|
case nil:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendNil(dst)
|
2018-04-03 21:07:18 +00:00
|
|
|
case net.IP:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendIPAddr(dst, val)
|
2018-04-03 21:07:18 +00:00
|
|
|
case net.IPNet:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendIPPrefix(dst, val)
|
2018-04-03 21:07:18 +00:00
|
|
|
case net.HardwareAddr:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendMACAddr(dst, val)
|
2021-08-03 09:31:54 +00:00
|
|
|
case json.RawMessage:
|
|
|
|
dst = appendJSON(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
default:
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = enc.AppendInterface(dst, val)
|
2017-07-26 03:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|