From: Henri Doreau Date: Fri, 6 Sep 2013 07:43:48 +0000 (+0200) Subject: LU-3692 api: More flexible logging API. X-Git-Tag: 2.5.51~63 X-Git-Url: https://git.whamcloud.com/?p=fs%2Flustre-release.git;a=commitdiff_plain;h=8846f1ee40510218c83c86a4a369b6575f7e5d2a LU-3692 api: More flexible logging API. Allow applications to register their own logging callback instead of writing every log message to stderr/stdout. Registered callbacks are global to the process, just like the log level. Signed-off-by: Henri Doreau Change-Id: I5ccfd1e24f1786408ce67b2cd87d53443bcd028a Reviewed-on: http://review.whamcloud.com/7572 Reviewed-by: Andreas Dilger Tested-by: Hudson Tested-by: Maloo Reviewed-by: John L. Hammond Reviewed-by: Oleg Drokin --- diff --git a/lustre/include/lustre/lustre_user.h b/lustre/include/lustre/lustre_user.h index 5c9e0a7..5f47ada 100644 --- a/lustre/include/lustre/lustre_user.h +++ b/lustre/include/lustre/lustre_user.h @@ -47,6 +47,7 @@ */ #ifndef __KERNEL__ +#include #include #endif #include diff --git a/lustre/include/lustre/lustreapi.h b/lustre/include/lustre/lustreapi.h index 3212bfe..3eb04a5 100644 --- a/lustre/include/lustre/lustreapi.h +++ b/lustre/include/lustre/lustreapi.h @@ -42,9 +42,11 @@ * @{ */ +#include #include -typedef void (*llapi_cb_t)(char *obd_type_name, char *obd_name, char *obd_uuid, void *args); +typedef void (*llapi_cb_t)(char *obd_type_name, char *obd_name, char *obd_uuid, + void *args); /* lustreapi message severity level */ enum llapi_message_level { @@ -58,17 +60,23 @@ enum llapi_message_level { LLAPI_MSG_MAX }; +typedef void (*llapi_log_callback_t)(enum llapi_message_level level, int err, + const char *fmt, va_list ap); + + /* the bottom three bits reserved for llapi_message_level */ #define LLAPI_MSG_MASK 0x00000007 #define LLAPI_MSG_NO_ERRNO 0x00000010 extern void llapi_msg_set_level(int level); +extern llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb); +extern llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb); -void llapi_error(int level, int rc, const char *fmt, ...) +void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...) __attribute__((__format__(__printf__, 3, 4))); #define llapi_err_noerrno(level, fmt, a...) \ llapi_error((level) | LLAPI_MSG_NO_ERRNO, 0, fmt, ## a) -void llapi_printf(int level, const char *fmt, ...) +void llapi_printf(enum llapi_message_level level, const char *fmt, ...) __attribute__((__format__(__printf__, 2, 3))); extern int llapi_file_create(const char *name, unsigned long long stripe_size, diff --git a/lustre/utils/liblustreapi.c b/lustre/utils/liblustreapi.c index 3051bdf..7b470f3 100644 --- a/lustre/utils/liblustreapi.c +++ b/lustre/utils/liblustreapi.c @@ -130,41 +130,90 @@ void llapi_msg_set_level(int level) llapi_msg_level = level; } -/* llapi_error will preserve errno */ -void llapi_error(int level, int _rc, const char *fmt, ...) +static void error_callback_default(enum llapi_message_level level, int err, + const char *fmt, va_list ap) +{ + vfprintf(stderr, fmt, ap); + if (level & LLAPI_MSG_NO_ERRNO) + fprintf(stderr, "\n"); + else + fprintf(stderr, ": %s (%d)\n", strerror(err), err); +} + +static void info_callback_default(enum llapi_message_level level, int err, + const char *fmt, va_list ap) { - va_list args; - int tmp_errno = errno; - /* to protect using errno as _rc argument */ - int rc = abs(_rc); + vfprintf(stdout, fmt, ap); +} - if ((level & LLAPI_MSG_MASK) > llapi_msg_level) - return; +static llapi_log_callback_t llapi_error_callback = error_callback_default; +static llapi_log_callback_t llapi_info_callback = info_callback_default; - va_start(args, fmt); - vfprintf(stderr, fmt, args); - va_end(args); - if (level & LLAPI_MSG_NO_ERRNO) - fprintf(stderr, "\n"); - else - fprintf(stderr, ": %s (%d)\n", strerror(rc), rc); - errno = tmp_errno; +/* llapi_error will preserve errno */ +void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...) +{ + va_list args; + int tmp_errno = errno; + + if ((level & LLAPI_MSG_MASK) > llapi_msg_level) + return; + + va_start(args, fmt); + llapi_error_callback(level, abs(err), fmt, args); + va_end(args); + errno = tmp_errno; } /* llapi_printf will preserve errno */ -void llapi_printf(int level, const char *fmt, ...) +void llapi_printf(enum llapi_message_level level, const char *fmt, ...) { - va_list args; - int tmp_errno = errno; + va_list args; + int tmp_errno = errno; + + if ((level & LLAPI_MSG_MASK) > llapi_msg_level) + return; + + va_start(args, fmt); + llapi_info_callback(level, 0, fmt, args); + va_end(args); + errno = tmp_errno; +} - if ((level & LLAPI_MSG_MASK) > llapi_msg_level) - return; +/** + * Set a custom error logging function. Passing in NULL will reset the logging + * callback to its default value. + * + * This function returns the value of the old callback. + */ +llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb) +{ + llapi_log_callback_t old = llapi_error_callback; + + if (cb != NULL) + llapi_error_callback = cb; + else + llapi_error_callback = error_callback_default; + + return old; +} + +/** + * Set a custom info logging function. Passing in NULL will reset the logging + * callback to its default value. + * + * This function returns the value of the old callback. + */ +llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb) +{ + llapi_log_callback_t old = llapi_info_callback; + + if (cb != NULL) + llapi_info_callback = cb; + else + llapi_info_callback = info_callback_default; - va_start(args, fmt); - vfprintf(stdout, fmt, args); - va_end(args); - errno = tmp_errno; + return old; } /**