]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
libbpf: Configure log verbosity with env variable
authorMykyta Yatsenko <yatsenko@meta.com>
Fri, 24 May 2024 13:18:40 +0000 (14:18 +0100)
committerAndrii Nakryiko <andrii@kernel.org>
Tue, 28 May 2024 23:25:06 +0000 (16:25 -0700)
Configure logging verbosity by setting LIBBPF_LOG_LEVEL environment
variable, which is applied only to default logger. Once user set their
custom logging callback, it is up to them to handle filtering.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20240524131840.114289-1-yatsenko@meta.com
Documentation/bpf/libbpf/libbpf_overview.rst
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf.h

index f36a2d4ffea2b3193b65b76363ec37f9cd616885..f4d22f0c62b0e72a9073d2bd118d391e309dea8e 100644 (file)
@@ -219,6 +219,14 @@ compilation and skeleton generation. Using Libbpf-rs will make building user
 space part of the BPF application easier. Note that the BPF program themselves
 must still be written in plain C.
 
+libbpf logging
+==============
+
+By default, libbpf logs informational and warning messages to stderr. The
+verbosity of these messages can be controlled by setting the environment
+variable LIBBPF_LOG_LEVEL to either warn, info, or debug. A custom log
+callback can be set using ``libbpf_set_print()``.
+
 Additional Documentation
 ========================
 
index 5401f2df463d24f52b6430252048f4bd5e04e4f5..d1627a2ca30bb817213c126265de2df2dff837f6 100644 (file)
@@ -229,7 +229,30 @@ 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)
+       const char *env_var = "LIBBPF_LOG_LEVEL";
+       static enum libbpf_print_level min_level = LIBBPF_INFO;
+       static bool initialized;
+
+       if (!initialized) {
+               char *verbosity;
+
+               initialized = true;
+               verbosity = getenv(env_var);
+               if (verbosity) {
+                       if (strcasecmp(verbosity, "warn") == 0)
+                               min_level = LIBBPF_WARN;
+                       else if (strcasecmp(verbosity, "debug") == 0)
+                               min_level = LIBBPF_DEBUG;
+                       else if (strcasecmp(verbosity, "info") == 0)
+                               min_level = LIBBPF_INFO;
+                       else
+                               fprintf(stderr, "libbpf: unrecognized '%s' envvar value: '%s', should be one of 'warn', 'debug', or 'info'.\n",
+                                       env_var, verbosity);
+               }
+       }
+
+       /* if too verbose, skip logging  */
+       if (level > min_level)
                return 0;
 
        return vfprintf(stderr, format, args);
index c3f77d9260feaea1181ea49f65e033af03490e34..26e4e35528c5335101d6cdf19e1eff5503fce9dd 100644 (file)
@@ -98,7 +98,10 @@ typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
 
 /**
  * @brief **libbpf_set_print()** sets user-provided log callback function to
- * be used for libbpf warnings and informational messages.
+ * be used for libbpf warnings and informational messages. If the user callback
+ * is not set, messages are logged to stderr by default. The verbosity of these
+ * messages can be controlled by setting the environment variable
+ * LIBBPF_LOG_LEVEL to either warn, info, or debug.
  * @param fn The log print function. If NULL, libbpf won't print anything.
  * @return Pointer to old print function.
  *