Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Configure log verbosity with env variable

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

authored by

Mykyta Yatsenko and committed by
Andrii Nakryiko
eb4e7726 4b3529ed

+36 -2
+8
Documentation/bpf/libbpf/libbpf_overview.rst
··· 219 219 space part of the BPF application easier. Note that the BPF program themselves 220 220 must still be written in plain C. 221 221 222 + libbpf logging 223 + ============== 224 + 225 + By default, libbpf logs informational and warning messages to stderr. The 226 + verbosity of these messages can be controlled by setting the environment 227 + variable LIBBPF_LOG_LEVEL to either warn, info, or debug. A custom log 228 + callback can be set using ``libbpf_set_print()``. 229 + 222 230 Additional Documentation 223 231 ======================== 224 232
+24 -1
tools/lib/bpf/libbpf.c
··· 229 229 static int __base_pr(enum libbpf_print_level level, const char *format, 230 230 va_list args) 231 231 { 232 - if (level == LIBBPF_DEBUG) 232 + const char *env_var = "LIBBPF_LOG_LEVEL"; 233 + static enum libbpf_print_level min_level = LIBBPF_INFO; 234 + static bool initialized; 235 + 236 + if (!initialized) { 237 + char *verbosity; 238 + 239 + initialized = true; 240 + verbosity = getenv(env_var); 241 + if (verbosity) { 242 + if (strcasecmp(verbosity, "warn") == 0) 243 + min_level = LIBBPF_WARN; 244 + else if (strcasecmp(verbosity, "debug") == 0) 245 + min_level = LIBBPF_DEBUG; 246 + else if (strcasecmp(verbosity, "info") == 0) 247 + min_level = LIBBPF_INFO; 248 + else 249 + fprintf(stderr, "libbpf: unrecognized '%s' envvar value: '%s', should be one of 'warn', 'debug', or 'info'.\n", 250 + env_var, verbosity); 251 + } 252 + } 253 + 254 + /* if too verbose, skip logging */ 255 + if (level > min_level) 233 256 return 0; 234 257 235 258 return vfprintf(stderr, format, args);
+4 -1
tools/lib/bpf/libbpf.h
··· 98 98 99 99 /** 100 100 * @brief **libbpf_set_print()** sets user-provided log callback function to 101 - * be used for libbpf warnings and informational messages. 101 + * be used for libbpf warnings and informational messages. If the user callback 102 + * is not set, messages are logged to stderr by default. The verbosity of these 103 + * messages can be controlled by setting the environment variable 104 + * LIBBPF_LOG_LEVEL to either warn, info, or debug. 102 105 * @param fn The log print function. If NULL, libbpf won't print anything. 103 106 * @return Pointer to old print function. 104 107 *