libbpf: configure log verbosity with env variable
Subsystems (3)
Mykyta Yatsenko [email protected]
PATCH 1/1 libbpf: configure log verbosity with env variable
From: Mykyta Yatsenko <[email protected]>
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.
--- Documentation/bpf/libbpf/libbpf_overview.rst | 7 ++++++ tools/lib/bpf/libbpf.c | 24 +++++++++++++++++++- tools/lib/bpf/libbpf.h | 5 +++- 3 files changed, 34 insertions(+), 2 deletions(-)
Documentation/bpf/libbpf/libbpf_overview.rst +7 -0
@@ -219,6 +219,13 @@ 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 ========================
tools/lib/bpf/libbpf.c +23 -1
@@ -229,7 +229,29 @@ 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 min_level =LIBBPF_INFO ; +static const char *env_var ="LIBBPF_LOG_LEVEL" ; +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,"Unexpected value of %s env variable\n" , env_var); + } + } + +/* if too verbose, skip logging */ +if (level > min_level)return 0 ;return vfprintf (stderr, format, args);
tools/lib/bpf/libbpf.h +4 -1
@@ -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. *
Signature · 1 line
2.45.0
Quoted 7 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Configure logging verbosity by setting
LIBBPF_LOG_LEVELenvironment variable, which is applied only to default logger. Once user set their custom logging callback, it is up to them to handle filtering.
Quoted 7 lines
Signed-off-by Mykyta Yatsenko <[email protected]>--- Documentation/bpf/libbpf/libbpf_overview.rst | 7 ++++++ tools/lib/bpf/libbpf.c | 24 +++++++++++++++++++- tools/lib/bpf/libbpf.h | 5 +++- 3 files changed, 34 insertions(+), 2 deletions(-)
I did a few tweaks, mentioned below, and applied to bpf-next. Thanks!
Quoted 5 lines
Documentation/bpf/libbpf/libbpf_overview.rst +0
-0
Documentation/bpf/libbpf/libbpf_overview.rst compilation and skeleton generation. Using Libbpf-rs will make building user · line 228
Quoted 10 lines
Documentation/bpf/libbpf/libbpf_overview.rst +7
-0@@ -219,6 +219,13 @@ 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()``. +
reformatted this to use line length consistent with the rest of the document
Quoted 8 lines
Documentation/bpf/libbpf/libbpf_overview.rst +0
-0Additional Documentation ========================tools/lib/bpf/libbpf.c +0
-0
tools/lib/bpf/libbpf.c static const char * const prog_type_name[] = { · line 233
Quoted 6 lines
tools/lib/bpf/libbpf.c +2
-1@@ -229,7 +229,29 @@ 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 min_level =LIBBPF_INFO ; +static const char *env_var ="LIBBPF_LOG_LEVEL" ;
no need for this to be static
Quoted 6 lines
tools/lib/bpf/libbpf.c +6
-0+static bool initialized; + +if (!initialized) { +char *verbosity; + + initialized = true;
tools/lib/bpf/libbpf.c static const char * const prog_type_name[] = { · line 249
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ 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,"Unexpected value of %s env variable\n" , env_var);
I've added "libbpf: " prefix and expanded the message with supported values.
Quoted 27 lines
tools/lib/bpf/libbpf.c +5
-0+ } + } + +/* if too verbose, skip logging */ +if (level > min_level)return 0 ;return vfprintf (stderr, format, args);tools/lib/bpf/libbpf.h +4
-2@@ -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. * -- 2.45.0
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 9 lines · On Fri, 24 May 2024 14:18:40 +0100 you wrote:
From: Mykyta Yatsenko <[email protected]>
Configure logging verbosity by setting
LIBBPF_LOG_LEVELenvironment 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 <[email protected]>[...]
Here is the summary with links:
- [bpf-next] libbpf: configure log verbosity with env variable
https://git.kernel.org/bpf/bpf-next/c/eb4e7726279a
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html