diff mbox series

[bpf-next] libbpf: configure log verbosity with env variable

Message ID 20240524131840.114289-1-yatsenko@meta.com (mailing list archive)
State Accepted
Commit eb4e7726279a344c82e3c23be396bcfd0a4d5669
Delegated to: BPF
Headers show
Series [bpf-next] libbpf: configure log verbosity with env variable | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-VM_Test-43 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-45 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-46 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-44 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-PR success PR summary
netdev/tree_selection success Clearly marked for bpf-next
netdev/apply fail Patch does not apply to bpf-next-0

Commit Message

Mykyta Yatsenko mykyta.yatsenko5@gmail.com May 24, 2024, 1:18 p.m. UTC
From: Mykyta Yatsenko <yatsenko@meta.com>

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>
---
 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(-)

Comments

Andrii Nakryiko May 28, 2024, 11:27 p.m. UTC | #1
On Fri, May 24, 2024 at 6:21 AM Mykyta Yatsenko
<mykyta.yatsenko5@gmail.com> wrote:
>
> From: Mykyta Yatsenko <yatsenko@meta.com>
>
> 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>
> ---
>  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!

> diff --git a/Documentation/bpf/libbpf/libbpf_overview.rst b/Documentation/bpf/libbpf/libbpf_overview.rst
> index f36a2d4ffea2..982dfd71a13d 100644
> --- a/Documentation/bpf/libbpf/libbpf_overview.rst
> +++ b/Documentation/bpf/libbpf/libbpf_overview.rst
> @@ -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

>  Additional Documentation
>  ========================
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 5401f2df463d..d0465ca74afc 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -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

> +       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);

I've added "libbpf: " prefix and expanded the message with supported values.

> +               }
> +       }
> +
> +       /* if too verbose, skip logging  */
> +       if (level > min_level)
>                 return 0;
>
>         return vfprintf(stderr, format, args);
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index c3f77d9260fe..26e4e35528c5 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -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
>
patchwork-bot+netdevbpf@kernel.org May 29, 2024, 1:25 a.m. UTC | #2
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Fri, 24 May 2024 14:18:40 +0100 you wrote:
> From: Mykyta Yatsenko <yatsenko@meta.com>
> 
> 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>
> 
> [...]

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!
diff mbox series

Patch

diff --git a/Documentation/bpf/libbpf/libbpf_overview.rst b/Documentation/bpf/libbpf/libbpf_overview.rst
index f36a2d4ffea2..982dfd71a13d 100644
--- a/Documentation/bpf/libbpf/libbpf_overview.rst
+++ b/Documentation/bpf/libbpf/libbpf_overview.rst
@@ -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
 ========================
 
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 5401f2df463d..d0465ca74afc 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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);
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index c3f77d9260fe..26e4e35528c5 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -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.
  *