-
Notifications
You must be signed in to change notification settings - Fork 95
/
logger_cb.go
71 lines (58 loc) · 1.64 KB
/
logger_cb.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
package libbpfgo
/*
#include <bpf/libbpf.h>
*/
import "C"
import (
"fmt"
"os"
)
// This callback definition needs to be in a different file from where it is
// declared in C Otherwise, multiple definition compilation error will occur
// loggerCallback is called by libbpf_print_fn() which in turn is called by
// libbpf
//
// revive:disable
//
//export loggerCallback
func loggerCallback(libbpfPrintLevel int, libbpfOutput *C.char) {
goOutput := C.GoString(libbpfOutput)
for _, fnFilterOut := range callbacks.LogFilters {
if fnFilterOut != nil {
if fnFilterOut(libbpfPrintLevel, goOutput) {
return
}
}
}
// pass received output to callback, leaving formatting to consumer
callbacks.Log(libbpfPrintLevel, goOutput)
}
const (
// libbpf print levels
LibbpfWarnLevel = int(C.LIBBPF_WARN)
LibbpfInfoLevel = int(C.LIBBPF_INFO)
LibbpfDebugLevel = int(C.LIBBPF_DEBUG)
)
// Callbacks stores the callbacks to be used by libbpfgo
type Callbacks struct {
Log func(level int, msg string)
LogFilters []func(libLevel int, msg string) bool
}
// callbacks is initialized with default callbacks, but can be changed by SetLoggerCbs
var callbacks = Callbacks{
Log: logFallback,
LogFilters: []func(libLevel int, msg string) bool{},
}
// SetLoggerCbs receives Callbacks type to be used to log libbpf outputs and to filter out those outputs
func SetLoggerCbs(cbs Callbacks) {
if cbs.Log == nil { // guarantee that there is always an outputter
cbs.Log = logFallback
}
callbacks = cbs
}
// logFallback is the default logger callback
// - level is ignored
func logFallback(level int, msg string) {
fmt.Fprint(os.Stderr, msg)
}
// revive:enable