summaryrefslogtreecommitdiffstats
path: root/tools/lib
diff options
context:
space:
mode:
authorYonghong Song <yhs@fb.com>2019-02-01 16:14:17 -0800
committerAlexei Starovoitov <ast@kernel.org>2019-02-04 09:40:59 -0800
commit6f1ae8b6628b9e054d3a8c959cf472234944a578 (patch)
treea06e49a2ac5d5a35f3fb6a25c1ea689f3c6bfe40 /tools/lib
parent9d100a19ffa519b17a0e998918337da5386e47fb (diff)
downloadlinux-6f1ae8b6628b9e054d3a8c959cf472234944a578.tar.bz2
tools/bpf: simplify libbpf API function libbpf_set_print()
Currently, the libbpf API function libbpf_set_print() takes three function pointer parameters for warning, info and debug printout respectively. This patch changes the API to have just one function pointer parameter and the function pointer has one additional parameter "debugging level". So if in the future, if the debug level is increased, the function signature won't change. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'tools/lib')
-rw-r--r--tools/lib/bpf/libbpf.c33
-rw-r--r--tools/lib/bpf/libbpf.h14
-rw-r--r--tools/lib/bpf/test_libbpf.cpp2
3 files changed, 17 insertions, 32 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 0354af03b038..ce209ab9a1a2 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -54,29 +54,26 @@
#define __printf(a, b) __attribute__((format(printf, a, b)))
-__printf(1, 2)
-static int __base_pr(const char *format, ...)
+__printf(2, 3)
+static int __base_pr(enum libbpf_print_level level, const char *format, ...)
{
va_list args;
int err;
+ if (level == LIBBPF_DEBUG)
+ return 0;
+
va_start(args, format);
err = vfprintf(stderr, format, args);
va_end(args);
return err;
}
-static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
-static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
-static __printf(1, 2) libbpf_print_fn_t __pr_debug;
+static __printf(2, 3) libbpf_print_fn_t __libbpf_pr = __base_pr;
-void libbpf_set_print(libbpf_print_fn_t warn,
- libbpf_print_fn_t info,
- libbpf_print_fn_t debug)
+void libbpf_set_print(libbpf_print_fn_t fn)
{
- __pr_warning = warn;
- __pr_info = info;
- __pr_debug = debug;
+ __libbpf_pr = fn;
}
__printf(2, 3)
@@ -84,17 +81,11 @@ void libbpf_print(enum libbpf_print_level level, const char *format, ...)
{
va_list args;
+ if (!__libbpf_pr)
+ return;
+
va_start(args, format);
- if (level == LIBBPF_WARN) {
- if (__pr_warning)
- __pr_warning(format, args);
- } else if (level == LIBBPF_INFO) {
- if (__pr_info)
- __pr_info(format, args);
- } else {
- if (__pr_debug)
- __pr_debug(format, args);
- }
+ __libbpf_pr(level, format, args);
va_end(args);
}
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 0fb32cc04633..19dbc1bed960 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -53,17 +53,11 @@ enum libbpf_print_level {
LIBBPF_DEBUG,
};
-/*
- * __printf is defined in include/linux/compiler-gcc.h. However,
- * it would be better if libbpf.h didn't depend on Linux header files.
- * So instead of __printf, here we use gcc attribute directly.
- */
-typedef int (*libbpf_print_fn_t)(const char *, ...)
- __attribute__((format(printf, 1, 2)));
+typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
+ const char *, ...)
+ __attribute__((format(printf, 2, 3)));
-LIBBPF_API void libbpf_set_print(libbpf_print_fn_t warn,
- libbpf_print_fn_t info,
- libbpf_print_fn_t debug);
+LIBBPF_API void libbpf_set_print(libbpf_print_fn_t fn);
/* Hide internal to user */
struct bpf_object;
diff --git a/tools/lib/bpf/test_libbpf.cpp b/tools/lib/bpf/test_libbpf.cpp
index be67f5ea2c19..fc134873bb6d 100644
--- a/tools/lib/bpf/test_libbpf.cpp
+++ b/tools/lib/bpf/test_libbpf.cpp
@@ -8,7 +8,7 @@
int main(int argc, char *argv[])
{
/* libbpf.h */
- libbpf_set_print(NULL, NULL, NULL);
+ libbpf_set_print(NULL);
/* bpf.h */
bpf_prog_get_fd_by_id(0);