| 1 |
/* |
| 2 |
Copyright 2021 The logr Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package logr |
| 18 |
|
| 19 |
// testLogSink is a trivial LogSink implementation, just for testing, which |
| 20 |
// calls (optional) hooks on each method. |
| 21 |
type testLogSink struct { |
| 22 |
fnInit func(ri RuntimeInfo) |
| 23 |
fnEnabled func(lvl int) bool |
| 24 |
fnInfo func(lvl int, msg string, kv ...any) |
| 25 |
fnError func(err error, msg string, kv ...any) |
| 26 |
fnWithValues func(kv ...any) |
| 27 |
fnWithName func(name string) |
| 28 |
|
| 29 |
withValues []any |
| 30 |
} |
| 31 |
|
| 32 |
var _ LogSink = &testLogSink{} |
| 33 |
|
| 34 |
func (ls *testLogSink) Init(ri RuntimeInfo) { |
| 35 |
if ls.fnInit != nil { |
| 36 |
ls.fnInit(ri) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func (ls *testLogSink) Enabled(lvl int) bool { |
| 41 |
if ls.fnEnabled != nil { |
| 42 |
return ls.fnEnabled(lvl) |
| 43 |
} |
| 44 |
return false |
| 45 |
} |
| 46 |
|
| 47 |
func (ls *testLogSink) Info(lvl int, msg string, kv ...any) { |
| 48 |
if ls.fnInfo != nil { |
| 49 |
ls.fnInfo(lvl, msg, kv...) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func (ls *testLogSink) Error(err error, msg string, kv ...any) { |
| 54 |
if ls.fnError != nil { |
| 55 |
ls.fnError(err, msg, kv...) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func (ls *testLogSink) WithValues(kv ...any) LogSink { |
| 60 |
if ls.fnWithValues != nil { |
| 61 |
ls.fnWithValues(kv...) |
| 62 |
} |
| 63 |
out := *ls |
| 64 |
n := len(out.withValues) |
| 65 |
out.withValues = append(out.withValues[:n:n], kv...) |
| 66 |
return &out |
| 67 |
} |
| 68 |
|
| 69 |
func (ls *testLogSink) WithName(name string) LogSink { |
| 70 |
if ls.fnWithName != nil { |
| 71 |
ls.fnWithName(name) |
| 72 |
} |
| 73 |
out := *ls |
| 74 |
return &out |
| 75 |
} |
| 76 |
|
| 77 |
type testCallDepthLogSink struct { |
| 78 |
testLogSink |
| 79 |
callDepth int |
| 80 |
fnWithCallDepth func(depth int) |
| 81 |
} |
| 82 |
|
| 83 |
var _ CallDepthLogSink = &testCallDepthLogSink{} |
| 84 |
|
| 85 |
func (ls *testCallDepthLogSink) WithCallDepth(depth int) LogSink { |
| 86 |
if ls.fnWithCallDepth != nil { |
| 87 |
ls.fnWithCallDepth(depth) |
| 88 |
} |
| 89 |
out := *ls |
| 90 |
out.callDepth += depth |
| 91 |
return &out |
| 92 |
} |