-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logger_cb: remove default callbacks (#294)
The logic related to filtering by callbacks must be a decision of the library consumers, as it is not possible to know which is the best strategy for each case. This also adds to the related selftest other example that shows how to filter using callbacks. Be warned that if filters are not defined via callbacks, all output is passed to logFallback() which outputs to stderr.
- Loading branch information
Showing
3 changed files
with
83 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,52 @@ | ||
package libbpfgo | ||
|
||
import "testing" | ||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
func TestLogFilterOutput(t *testing.T) { | ||
tests := []struct { | ||
libbpfPrintLevel int | ||
output string | ||
expectedResult bool | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLogFallback(t *testing.T) { | ||
tt := []struct { | ||
message string | ||
}{ | ||
{ | ||
output: "libbpf: prog 'trace_check_map_func_compatibility': failed to create kprobe 'check_map_func_compatibility+0x0' perf event: No such file or directory\n", | ||
expectedResult: true, | ||
}, | ||
{ | ||
output: "libbpf: Kernel error message: Exclusivity flag on\n", | ||
expectedResult: true, | ||
}, | ||
{ | ||
output: "libbpf: prog 'cgroup_skb_ingress': failed to attach to cgroup 'cgroup': Invalid argument\n", | ||
expectedResult: true, | ||
}, | ||
{ | ||
output: "libbpf: prog 'cgroup_skb_egress': failed to attach to cgroup 'cgroup': Invalid argument\n", | ||
expectedResult: true, | ||
message: "This is a warning message", | ||
}, | ||
{ | ||
output: "This is not a log message that should be filtered\n", | ||
expectedResult: false, | ||
message: "This is a information message", | ||
}, | ||
{ | ||
output: "libbpf: This is not a log message that should be filtered\n", | ||
expectedResult: false, | ||
message: "This is a debug message", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
result := LogFilterOutput(test.libbpfPrintLevel, test.output) | ||
if result != test.expectedResult { | ||
t.Errorf("For input '%s', expected %v but got %v", test.output, test.expectedResult, result) | ||
} | ||
for _, tc := range tt { | ||
var buf bytes.Buffer | ||
|
||
r, w, err := os.Pipe() | ||
require.NoError(t, err, "failed to create pipe") | ||
|
||
writeEnd := os.NewFile(uintptr(w.Fd()), "pipe") | ||
|
||
oldStderr := os.Stderr | ||
os.Stderr = writeEnd | ||
|
||
// level is ignored | ||
logFallback(LibbpfInfoLevel, tc.message) | ||
|
||
os.Stderr = oldStderr | ||
|
||
err = writeEnd.Close() | ||
require.NoError(t, err, "failed to close writeEnd") | ||
_, err = io.Copy(&buf, r) | ||
require.NoError(t, err, "failed to copy from read end to buffer") | ||
|
||
// The message should be printed to stderr with a newline | ||
assert.Equal(t, tc.message+"\n", buf.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters