From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756355AbbE2Nhc (ORCPT ); Fri, 29 May 2015 09:37:32 -0400 Received: from mail-pa0-f51.google.com ([209.85.220.51]:32819 "EHLO mail-pa0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756171AbbE2NhQ (ORCPT ); Fri, 29 May 2015 09:37:16 -0400 Date: Fri, 29 May 2015 22:35:47 +0900 From: Namhyung Kim To: Wang Nan Cc: paulus@samba.org, a.p.zijlstra@chello.nl, mingo@redhat.com, acme@kernel.org, jolsa@kernel.org, dsahern@gmail.com, daniel@iogearbox.net, brendan.d.gregg@gmail.com, masami.hiramatsu.pt@hitachi.com, lizefan@huawei.com, linux-kernel@vger.kernel.org, pi3orama@163.com Subject: Re: [RFC PATCH v4 05/29] bpf tools: Allow caller to set printing function Message-ID: <20150529133547.GB10935@danjae.kornet> References: <1432704004-171454-1-git-send-email-wangnan0@huawei.com> <1432704004-171454-6-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1432704004-171454-6-git-send-email-wangnan0@huawei.com> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Wang, On Wed, May 27, 2015 at 05:19:40AM +0000, Wang Nan wrote: > 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, ...); defailt 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. > > Acked-by: Alexei Starovoitov > Signed-off-by: Wang Nan > --- > tools/lib/bpf/libbpf.c | 43 +++++++++++++++++++++++++++++++++++++++++++ > tools/lib/bpf/libbpf.h | 4 ++++ > 2 files changed, 47 insertions(+) > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index c08d6bc..49091c3 100644 > --- a/tools/lib/bpf/libbpf.c > +++ b/tools/lib/bpf/libbpf.c > @@ -7,8 +7,51 @@ > */ > > #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) int (*__pr_warning)(const char *format, ...) = > + __base_pr; > +static __printf(1, 2) int (*__pr_info)(const char *format, ...) = > + __base_pr; > +static __printf(1, 2) int (*__pr_debug)(const char *format, ...) = > + NULL; What about using typedef for this.. like print_fn_t ? Thanks, Namhyung > + > +#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(int (*warn)(const char *format, ...), > + int (*info)(const char *format, ...), > + int (*debug)(const char *format, ...)) > +{ > + __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..430b122 100644 > --- a/tools/lib/bpf/libbpf.h > +++ b/tools/lib/bpf/libbpf.h > @@ -8,4 +8,8 @@ > #ifndef __BPF_LIBBPF_H > #define __BPF_LIBBPF_H > > +void libbpf_set_print(int (*warn)(const char *format, ...), > + int (*info)(const char *format, ...), > + int (*debug)(const char *format, ...)); > + > #endif > -- > 1.8.3.4 >