Skip to content

Commit

Permalink
logger: pass raw libbpf print output
Browse files Browse the repository at this point in the history
Due to libbpf output being escaped, we should pass the raw output to the
consumer, letting them decide how to format it, since them might want
to, for example, use it in a logger or print directly to stderr.

Reported-by: @yanivagman
  • Loading branch information
geyslan committed Mar 31, 2023
1 parent 9f21a9d commit e99ef72
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
8 changes: 2 additions & 6 deletions logger_cb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import "C"
import (
"fmt"
"os"
"strings"
)

// This callback definition needs to be in a different file from where it is declared in C
Expand All @@ -19,7 +18,6 @@ import (
//export loggerCallback
func loggerCallback(libbpfPrintLevel int, libbpfOutput *C.char) {
goOutput := C.GoString(libbpfOutput)
goOutput = strings.TrimSuffix(goOutput, "\n")

for _, fnFilterOut := range callbacks.LogFilters {
if fnFilterOut != nil {
Expand All @@ -29,6 +27,7 @@ func loggerCallback(libbpfPrintLevel int, libbpfOutput *C.char) {
}
}

// pass received output to callback, leaving formatting to consumer
callbacks.Log(libbpfPrintLevel, goOutput)
}

Expand Down Expand Up @@ -62,9 +61,6 @@ func SetLoggerCbs(cbs Callbacks) {

// logFallback is the default logger callback
// - level is ignored
// - output, suffixed with a newline, is printed to stderr
func logFallback(level int, msg string) {
var outMsg = msg + "\n"

fmt.Fprint(os.Stderr, outMsg)
fmt.Fprint(os.Stderr, msg)
}
2 changes: 1 addition & 1 deletion logger_cb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ func TestLogFallback(t *testing.T) {
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())
assert.Equal(t, tc.message, buf.String())
}
}

0 comments on commit e99ef72

Please sign in to comment.