funcr: don't copy each options field, copy options
No functional change.
Tim Hockin
2 years ago
167 | 167 | |
168 | 168 | func newFormatter(opts Options, outfmt outputFormat) Formatter { |
169 | 169 | f := Formatter{ |
170 | outputFormat: outfmt, | |
171 | prefix: "", | |
172 | values: nil, | |
173 | depth: 0, | |
174 | logCaller: opts.LogCaller, | |
175 | logCallerFunc: opts.LogCallerFunc, | |
176 | logTimestamp: opts.LogTimestamp, | |
177 | verbosity: opts.Verbosity, | |
170 | outputFormat: outfmt, | |
171 | prefix: "", | |
172 | values: nil, | |
173 | depth: 0, | |
174 | opts: opts, | |
178 | 175 | } |
179 | 176 | return f |
180 | 177 | } |
183 | 180 | // implementation. It should be constructed with NewFormatter. Some of |
184 | 181 | // its methods directly implement logr.LogSink. |
185 | 182 | type Formatter struct { |
186 | outputFormat outputFormat | |
187 | prefix string | |
188 | values []interface{} | |
189 | valuesStr string | |
190 | depth int | |
191 | logCaller MessageClass | |
192 | logCallerFunc bool | |
193 | logTimestamp bool | |
194 | verbosity int | |
183 | outputFormat outputFormat | |
184 | prefix string | |
185 | values []interface{} | |
186 | valuesStr string | |
187 | depth int | |
188 | opts Options | |
195 | 189 | } |
196 | 190 | |
197 | 191 | // outputFormat indicates which outputFormat to use. |
489 | 483 | return callerID{"<unknown>", 0, ""} |
490 | 484 | } |
491 | 485 | fn := "" |
492 | if f.logCallerFunc { | |
486 | if f.opts.LogCallerFunc { | |
493 | 487 | if fp := runtime.FuncForPC(pc); fp != nil { |
494 | 488 | fn = fp.Name() |
495 | 489 | } |
507 | 501 | |
508 | 502 | // Enabled checks whether an info message at the given level should be logged. |
509 | 503 | func (f Formatter) Enabled(level int) bool { |
510 | return level <= f.verbosity | |
504 | return level <= f.opts.Verbosity | |
511 | 505 | } |
512 | 506 | |
513 | 507 | // GetDepth returns the current depth of this Formatter. This is useful for |
526 | 520 | args = append(args, "logger", prefix) |
527 | 521 | prefix = "" |
528 | 522 | } |
529 | if f.logTimestamp { | |
523 | if f.opts.LogTimestamp { | |
530 | 524 | args = append(args, "ts", time.Now().Format(timestampFmt)) |
531 | 525 | } |
532 | if f.logCaller == All || f.logCaller == Info { | |
526 | if policy := f.opts.LogCaller; policy == All || policy == Info { | |
533 | 527 | args = append(args, "caller", f.caller()) |
534 | 528 | } |
535 | 529 | args = append(args, "level", level, "msg", msg) |
546 | 540 | args = append(args, "logger", prefix) |
547 | 541 | prefix = "" |
548 | 542 | } |
549 | if f.logTimestamp { | |
543 | if f.opts.LogTimestamp { | |
550 | 544 | args = append(args, "ts", time.Now().Format(timestampFmt)) |
551 | 545 | } |
552 | if f.logCaller == All || f.logCaller == Error { | |
546 | if policy := f.opts.LogCaller; policy == All || policy == Error { | |
553 | 547 | args = append(args, "caller", f.caller()) |
554 | 548 | } |
555 | 549 | args = append(args, "msg", msg) |