-
Notifications
You must be signed in to change notification settings - Fork 4
/
builder.go
255 lines (200 loc) · 6.83 KB
/
builder.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package bilog
import (
"errors"
"io"
"github.com/insolar/assured-ledger/ledger-core/log/adapters/bilog/pbuf"
"github.com/insolar/assured-ledger/ledger-core/log/logoutput"
"github.com/insolar/assured-ledger/ledger-core/log"
"github.com/insolar/assured-ledger/ledger-core/log/adapters/bilog/json"
"github.com/insolar/assured-ledger/ledger-core/log/adapters/bilog/msgencoder"
"github.com/insolar/assured-ledger/ledger-core/log/adapters/bilog/text"
"github.com/insolar/assured-ledger/ledger-core/log/logcommon"
)
// TODO PLAT-45 test performance and memory impact of (recycleBuf)
func NewFactory(encoders msgencoder.FactoryDispatcherFunc, recycleBuf bool) logcommon.Factory {
return binLogFactory{encoders, recycleBuf}
}
var _ logcommon.Factory = binLogFactory{}
type binLogFactory struct {
encoders msgencoder.FactoryDispatcherFunc
recycleBuf bool
// writeDelayPreferTrim bool
}
func (binLogFactory) GetGlobalLogAdapter() logcommon.GlobalLogAdapter {
return binLogGlobalAdapter
}
func (f binLogFactory) CanReuseMsgBuffer() bool {
return !f.recycleBuf
}
func (f binLogFactory) PrepareBareOutput(output logcommon.BareOutput, metrics *logcommon.MetricsHelper, config logcommon.BuildConfig) (io.Writer, error) {
if !config.Instruments.MetricsMode.HasWriteMetric() {
return output.Writer, nil
}
encoder, err := f.createEncoderFactory(config.Output.Format)
if err != nil {
return nil, err
}
var reportFn logcommon.DurationReportFunc
if config.Instruments.MetricsMode&logcommon.LogMetricsWriteDelayReport != 0 {
reportFn = metrics.GetOnWriteDurationReport()
}
if config.Instruments.MetricsMode&logcommon.LogMetricsWriteDelayField != 0 {
return encoder.CreateMetricWriter(output.Writer, logoutput.WriteDurationFieldName, reportFn)
}
return encoder.CreateMetricWriter(output.Writer, "", reportFn)
}
func (f binLogFactory) CreateNewLogger(params logcommon.NewLoggerParams) (logcommon.EmbeddedLogger, error) {
return f.createLogger(params, nil)
}
func (f binLogFactory) createEncoderFactory(outFormat logcommon.LogFormat) (msgencoder.EncoderFactory, error) {
switch outFormat {
case logcommon.JSONFormat:
return json.EncoderManager(), nil
case logcommon.TextFormat:
return text.EncoderManager(), nil
case logcommon.PbufFormat:
return pbuf.EncoderManager(), nil
default:
if f.encoders == nil {
break
}
if encoderFactory := f.encoders(string(outFormat)); encoderFactory != nil {
return encoderFactory, nil
}
}
return nil, errors.New("unknown output format: " + outFormat.String())
}
const minEventBuffer = 512
const maxEventBufferIncrement = minEventBuffer >> 1
const anticipatedFieldBuffer = 32
func estimateBufferSizeByFields(nCount int) int {
if nCount == 0 {
return 0
}
if sz := nCount * anticipatedFieldBuffer; sz < maxEventBufferIncrement {
return sz
}
return maxEventBufferIncrement
}
func (f binLogFactory) createLogger(params logcommon.NewLoggerParams, template *binLogAdapter) (logcommon.EmbeddedLogger, error) {
outFormat := params.Config.Output.Format
if template != nil && outFormat != template.config.Output.Format {
// no field inheritance when format is changed
params.Reqs &^= logcommon.RequiresParentCtxFields
}
// ========== DO NOT modify config beyond this point ================
cfg := params.Config // ensure a separate copy on heap
la := binLogAdapter{
config: &cfg,
writer: params.Config.LoggerOutput,
levelFilter: params.Level,
recycleBuf: f.recycleBuf,
lowLatency: params.Reqs&logcommon.RequiresLowLatency != 0,
expectedEventLen: anticipatedFieldBuffer,
}
encoderFactory, err := f.createEncoderFactory(outFormat)
if err != nil {
return nil, err
}
la.encoder = encoderFactory.CreateEncoder(params.Config.MsgFormat)
if la.encoder == nil {
return nil, errors.New("encoder factory has failed: " + outFormat.String())
}
{ // replacement and inheritance for ctxFields
switch {
case template == nil || params.Reqs&logcommon.RequiresParentCtxFields == 0:
//
case len(template.parentStatic) == 0:
la.parentStatic = template.staticFields
default:
la.parentStatic = template.parentStatic
la.staticFields = template.staticFields
}
la._addFieldsByBuilder(params.Fields)
la.expectedEventLen += len(la.parentStatic)
la.expectedEventLen += len(la.staticFields)
}
{ // replacement and inheritance for dynFields
if template != nil && params.Reqs&logcommon.RequiresParentDynFields != 0 && len(template.dynFields) > 0 {
la.dynFields = template.dynFields
}
la._addDynFieldsByBuilder(params.DynFields)
la.expectedEventLen += estimateBufferSizeByFields(len(la.dynFields))
}
if la.expectedEventLen > maxEventBufferIncrement {
la.expectedEventLen += maxEventBufferIncrement
}
return &la, nil
}
/* =========================== */
type binLogTemplate struct {
binLogFactory
template *binLogAdapter
}
func (b binLogTemplate) GetTemplateLevel() logcommon.Level {
return b.template.levelFilter
}
func (b binLogTemplate) GetLoggerOutput() logcommon.LoggerOutput {
return b.template.GetLoggerOutput()
}
func (b binLogTemplate) GetTemplateConfig() logcommon.Config {
return *b.template.config
}
func (b binLogTemplate) CreateNewLogger(params logcommon.NewLoggerParams) (logcommon.EmbeddedLogger, error) {
return b.createLogger(params, b.template)
}
func (b binLogTemplate) CopyTemplateLogger(params logcommon.CopyLoggerParams) logcommon.EmbeddedLogger {
hasUpdates := false
la := *b.template
la.expectedEventLen = 0
if la.levelFilter != params.Level {
la.levelFilter = params.Level
hasUpdates = true
}
{ // replacement and inheritance for ctxFields
switch {
case params.Reqs&logcommon.RequiresParentCtxFields == 0:
if la.parentStatic != nil || la.staticFields != nil {
la.parentStatic = nil
la.staticFields = nil
hasUpdates = true
}
case len(la.parentStatic) == 0:
la.parentStatic = la.staticFields
la.staticFields = nil
}
if len(params.AppendFields) > 0 {
la._addFieldsByBuilder(params.AppendFields)
hasUpdates = true
}
la.expectedEventLen += len(la.parentStatic)
la.expectedEventLen += len(la.staticFields)
}
{ // replacement and inheritance for dynFields
if params.Reqs&logcommon.RequiresParentDynFields == 0 && la.dynFields != nil {
la.dynFields = nil
hasUpdates = true
}
if len(params.AppendDynFields) > 0 {
la._addDynFieldsByBuilder(params.AppendDynFields)
hasUpdates = true
}
la.expectedEventLen += estimateBufferSizeByFields(len(la.dynFields))
}
if !hasUpdates {
return b.template
}
if la.expectedEventLen > maxEventBufferIncrement {
la.expectedEventLen += maxEventBufferIncrement
}
return &la
}
/* =========================== */
var binLogGlobalAdapter logcommon.GlobalLogAdapter = binLogGlobal{}
type binLogGlobal struct{}
func (binLogGlobal) SetGlobalLoggerFilter(level log.Level) {
setGlobalFilter(level)
}
func (binLogGlobal) GetGlobalLoggerFilter() log.Level {
return getGlobalFilter()
}