-
Notifications
You must be signed in to change notification settings - Fork 4
/
formatter.go
168 lines (148 loc) · 3.68 KB
/
formatter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package logfmt
import (
"reflect"
"github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
)
type FormatFunc func(...interface{}) string
type FormatfFunc func(string, ...interface{}) string
type MsgFormatConfig struct {
Sformat FormatFunc
Sformatf FormatfFunc
MFactory MarshallerFactory
TimeFmt string
}
type FieldReporterFunc func(collector LogObjectMetricCollector, fieldName string, v interface{})
type MarshallerFactory interface {
CreateErrorMarshaller(error) LogObjectMarshaller
CreateLogObjectMarshaller(reflect.Value) LogObjectMarshaller
//RegisterFieldReporter(fieldType reflect.Type, fn FieldReporterFunc)
}
func fmtLogStruct(a interface{}, mFactory MarshallerFactory, optionalStruct bool, treatErrorAsStruct bool) (LogObjectMarshaller, *string) {
if mFactory == nil {
panic(throw.IllegalValue())
}
if vv, ok := a.(LogObject); ok {
m := vv.GetLogObjectMarshaller()
if m != nil {
return m, nil
}
vr := reflect.ValueOf(vv)
if vr.Kind() == reflect.Ptr {
vr = vr.Elem()
}
m = mFactory.CreateLogObjectMarshaller(vr)
if m != nil {
return m, nil
}
}
switch vv := a.(type) {
case LogObjectMarshaller:
return vv, nil
case string: // the most obvious case
return nil, &vv
case *string: // handled separately to avoid unnecessary reflect
return nil, vv
case nil:
return nil, nil
case error:
if treatErrorAsStruct {
break
}
// use marshalling before prepareValue() for errors
m := mFactory.CreateErrorMarshaller(vv)
if m != nil {
return m, nil
}
default:
if s, t, isNil := prepareValue(a); t != reflect.Invalid {
if isNil {
return nil, nil
}
return nil, &s
}
}
switch vr := reflect.ValueOf(a); vr.Kind() {
case reflect.Ptr:
if vr.IsNil() {
break
}
vr = vr.Elem()
if vr.Kind() != reflect.Struct {
break
}
fallthrough
case reflect.Struct:
if !optionalStruct || len(vr.Type().PkgPath()) == 0 {
return mFactory.CreateLogObjectMarshaller(vr), nil
}
}
return nil, nil
}
func (v MsgFormatConfig) fmtLogStruct(a interface{}, optionalStruct bool) (LogObjectMarshaller, *string) {
return fmtLogStruct(a, v.MFactory, optionalStruct, false)
}
func (v MsgFormatConfig) FmtLogStruct(a interface{}) (LogObjectMarshaller, string) {
if m, s := v.fmtLogStruct(a, false); m != nil {
return m, ""
} else if s != nil {
return m, *s
}
return nil, v.Sformat(a)
}
func (v MsgFormatConfig) FmtLogStructOrObject(a interface{}) (LogObjectMarshaller, string) {
if m, s := v.fmtLogStruct(a, true); m != nil {
return m, ""
} else if s != nil {
return nil, *s
}
return nil, v.Sformat(a)
}
func (v MsgFormatConfig) FmtLogObject(a ...interface{}) string {
return v.Sformat(a...)
}
func (v MsgFormatConfig) PrepareMutedLogObject(a ...interface{}) LogObjectMarshaller {
if len(a) != 1 {
return nil
}
switch vv := a[0].(type) {
case LogObject:
m := vv.GetLogObjectMarshaller()
if m != nil {
return m
}
vr := reflect.ValueOf(vv)
if vr.Kind() == reflect.Ptr {
vr = vr.Elem()
}
return v.MFactory.CreateLogObjectMarshaller(vr)
case LogObjectMarshaller:
return vv
case string, *string, nil: // the most obvious case(s) - avoid reflect
return nil
case error:
m := v.MFactory.CreateErrorMarshaller(vv)
if m != nil {
return m
}
}
switch vr := reflect.ValueOf(a[0]); vr.Kind() {
case reflect.Ptr:
if vr.IsNil() {
return nil
}
vr = vr.Elem()
if vr.Kind() != reflect.Struct {
return nil
}
fallthrough
case reflect.Struct:
if len(vr.Type().Name()) == 0 { // only unnamed objects are handled by default
return v.MFactory.CreateLogObjectMarshaller(vr)
}
}
return nil
}
type MsgTemplate struct{}
func (*MsgTemplate) GetLogObjectMarshaller() LogObjectMarshaller {
return nil
}