Skip to contentSkip to conversation

libbpf: configure log verbosity with env variable

PATCH In review Mykyta Yatsenko [email protected] 2 participants 2 messages May 23, 2024

Subsystems (2)
lore
Share & tools
lore

Mykyta Yatsenko [email protected]

author
#0 lore raw

PATCH 1/1 libbpf: configure log verbosity with env variable

From: Mykyta Yatsenko <[email protected]>

Configure logging verbosity by setting LIBBPF_LOG environment variable. Only applying to default logger. Once user set their custom logging callback, it is up to them to filter.

---
 tools/lib/bpf/libbpf.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +17 -1
index 5401f2df463d..8805607073da 100644
@@ -229,7 +229,23 @@ static const char * const prog_type_name[] = {
 static int __base_pr(enum libbpf_print_level level, const char *format,
 		     va_list args)
 {
-	if (level == LIBBPF_DEBUG)
+	static enum libbpf_print_level env_level = LIBBPF_INFO;
+	static bool initialized;
+
+	if (!initialized) {
+		char *verbosity;
+
+		initialized = true;
+		verbosity = getenv("LIBBPF_LOG");
+		if (verbosity) {
+			if (strcmp(verbosity, "warn") == 0)
+				env_level = LIBBPF_WARN;
+			else if (strcmp(verbosity, "debug") == 0)
+				env_level = LIBBPF_DEBUG;
+		}
+	}
+
+	if (env_level < level)
 		return 0;
 
 	return vfprintf(stderr, format, args);
Signature · 1 line
2.45.0
#1 lore raw
Quoted 17 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Configure logging verbosity by setting LIBBPF_LOG environment variable. Only applying to default logger. Once user set their custom logging callback, it is up to them to filter.

---
 tools/lib/bpf/libbpf.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 5401f2df463d..8805607073da 100644

tools/lib/bpf/libbpf.c static const char * const prog_type_name[] = { · line 232

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -229,7 +229,23 @@ static const char * const prog_type_name[] = {
 static int __base_pr(enum libbpf_print_level level, const char *format,
                     va_list args)
 {
-       if (level == LIBBPF_DEBUG)
+       static enum libbpf_print_level env_level = LIBBPF_INFO;

this doesn't have to come from just the environment, we might add an API to set the default log level programmatically (in the future, not saying we should do it right now). Maybe more generic "min_level" or "log_level"?

tools/lib/bpf/libbpf.c static const char * const prog_type_name[] = { · line 239

Quoted 7 lines
tools/lib/bpf/libbpf.c +7 -0
+       static bool initialized;
+
+       if (!initialized) {
+               char *verbosity;
+
+               initialized = true;
+               verbosity = getenv("LIBBPF_LOG");

I'd be confused whether LIBBPF_LOG is log *level* or some sort of file path to which default log should go (and who knows, maybe we'll add that). So let's maybe call it LIBBPF_LOG_LEVEL=... ?

tools/lib/bpf/libbpf.c static const char * const prog_type_name[] = { · line 241

Quoted 2 lines
tools/lib/bpf/libbpf.c +2 -0
+               if (verbosity) {
+                       if (strcmp(verbosity, "warn") == 0)

let's do case-insensitive comparison?

tools/lib/bpf/libbpf.c static const char * const prog_type_name[] = { · line 244

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+                               env_level = LIBBPF_WARN;
+                       else if (strcmp(verbosity, "debug") == 0)
+                               env_level = LIBBPF_DEBUG;

let's handle the "info" level as well?

and for unrecognized value, let's print a warning directly with fprintf(stderr)?

Let's also add this LIBBPF_LOG_LEVEL envvar description to the doc comment for libbpf_set_print() and a short section somewhere in Documentation/bpf/libbpf/libbpf_overview.rst ?

tools/lib/bpf/libbpf.c static const char * const prog_type_name[] = { · line 248

Quoted 4 lines
tools/lib/bpf/libbpf.c +4 -0
+               }
+       }
+
+       if (env_level < level)

super nitpicky, sorry, but my brain refuses to intuitively understand this, can you invert the condition please (and maybe add a comment):

/* if too verbose, skip logging */
if (level > min_level)

    return 0;

But in general this is a useful feature, thanks!

pw-bot: cr

Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -1
                return 0;
 
        return vfprintf(stderr, format, args);
--

2.45.0