Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932717AbbHHILC (ORCPT ); Sat, 8 Aug 2015 04:11:02 -0400 Received: from terminus.zytor.com ([198.137.202.10]:53638 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932287AbbHHIKx (ORCPT ); Sat, 8 Aug 2015 04:10:53 -0400 Date: Sat, 8 Aug 2015 01:09:55 -0700 From: tip-bot for Wang Nan Message-ID: Cc: wangnan0@huawei.com, xiakaixu@huawei.com, a.p.zijlstra@chello.nl, linux-kernel@vger.kernel.org, hekuang@huawei.com, daniel@iogearbox.net, tglx@linutronix.de, mingo@kernel.org, acme@redhat.com, masami.hiramatsu.pt@hitachi.com, namhyung@kernel.org, hpa@zytor.com, lizefan@huawei.com, dsahern@gmail.com, ast@plumgrid.com, brendan.d.gregg@gmail.com, jolsa@kernel.org Reply-To: daniel@iogearbox.net, hekuang@huawei.com, mingo@kernel.org, tglx@linutronix.de, brendan.d.gregg@gmail.com, ast@plumgrid.com, dsahern@gmail.com, lizefan@huawei.com, jolsa@kernel.org, acme@redhat.com, masami.hiramatsu.pt@hitachi.com, namhyung@kernel.org, hpa@zytor.com, wangnan0@huawei.com, xiakaixu@huawei.com, a.p.zijlstra@chello.nl, linux-kernel@vger.kernel.org In-Reply-To: <1435716878-189507-5-git-send-email-wangnan0@huawei.com> References: <1435716878-189507-5-git-send-email-wangnan0@huawei.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] bpf tools: Allow caller to set printing function Git-Commit-ID: b3f59d66e22b8be4ccae67c8eaffa2cbb9e54eb1 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3822 Lines: 118 Commit-ID: b3f59d66e22b8be4ccae67c8eaffa2cbb9e54eb1 Gitweb: http://git.kernel.org/tip/b3f59d66e22b8be4ccae67c8eaffa2cbb9e54eb1 Author: Wang Nan AuthorDate: Wed, 1 Jul 2015 02:13:52 +0000 Committer: Arnaldo Carvalho de Melo CommitDate: Fri, 7 Aug 2015 10:16:56 -0300 bpf tools: Allow caller to set printing function By libbpf_set_print(), users of libbpf are allowed to register he/she own debug, info and warning printing functions. Libbpf will use those functions to print messages. If not provided, default info and warning printing functions are fprintf(stderr, ...); default debug printing is NULL. This API is designed to be used by perf, enables it to register its own logging functions to make all logs uniform, instead of separated logging level control. Signed-off-by: Wang Nan Acked-by: Alexei Starovoitov Cc: Brendan Gregg Cc: Daniel Borkmann Cc: David Ahern Cc: He Kuang Cc: Jiri Olsa Cc: Kaixu Xia Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1435716878-189507-5-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 12 ++++++++++++ 2 files changed, 52 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index c08d6bc..6f0c13a 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -7,8 +7,48 @@ */ #include +#include +#include +#include #include #include #include #include "libbpf.h" + +#define __printf(a, b) __attribute__((format(printf, a, b))) + +__printf(1, 2) +static int __base_pr(const char *format, ...) +{ + va_list args; + int err; + + 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; + +#define __pr(func, fmt, ...) \ +do { \ + if ((func)) \ + (func)("libbpf: " fmt, ##__VA_ARGS__); \ +} while (0) + +#define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__) +#define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__) +#define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__) + +void libbpf_set_print(libbpf_print_fn_t warn, + libbpf_print_fn_t info, + libbpf_print_fn_t debug) +{ + __pr_warning = warn; + __pr_info = info; + __pr_debug = debug; +} diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index a6f46d9..8d1eeba 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -8,4 +8,16 @@ #ifndef __BPF_LIBBPF_H #define __BPF_LIBBPF_H +/* + * In include/linux/compiler-gcc.h, __printf is defined. However + * it should be better if libbpf.h doesn't depend on Linux header file. + * So instead of __printf, here we use gcc attribute directly. + */ +typedef int (*libbpf_print_fn_t)(const char *, ...) + __attribute__((format(printf, 1, 2))); + +void libbpf_set_print(libbpf_print_fn_t warn, + libbpf_print_fn_t info, + libbpf_print_fn_t debug); + #endif -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/