From 46523f33e016ac36b998873041a1ca89528c4dbb Mon Sep 17 00:00:00 2001 From: paul luse Date: Mon, 20 May 2019 18:29:45 -0400 Subject: [PATCH] log: passing user-defined log Change-Id: I993e15a6e75029b0717960d5da31325e7f3522c1 Signed-off-by: zkhatami88 Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/456407 Reviewed-by: Ben Walker Reviewed-by: Darek Stojaczyk Tested-by: SPDK CI Jenkins --- include/spdk/event.h | 13 ++++++++++ include/spdk/log.h | 5 +++- lib/event/app.c | 2 +- lib/log/log.c | 41 +++++++++++++++++++++----------- test/unit/lib/log/log.c/log_ut.c | 2 +- 5 files changed, 46 insertions(+), 17 deletions(-) diff --git a/include/spdk/event.h b/include/spdk/event.h index 6164b3b1f..88532af94 100644 --- a/include/spdk/event.h +++ b/include/spdk/event.h @@ -132,6 +132,19 @@ struct spdk_app_opts { /** Opaque context for use of the env implementation. */ void *env_context; + + /** + * for passing user-provided log call + * + * \param level Log level threshold. + * \param file Name of the current source file. + * \param line Current source file line. + * \param func Current source function name. + * \param format Format string to the message. + */ + void (* log)(int level, const char *file, const int line, + const char *func, const char *format); + }; /** diff --git a/include/spdk/log.h b/include/spdk/log.h index 5d47299bf..bc718618f 100644 --- a/include/spdk/log.h +++ b/include/spdk/log.h @@ -45,11 +45,14 @@ extern "C" { #endif +typedef void logfunc(int level, const char *file, const int line, + const char *func, const char *format); + /** * Initialize the logging module. Messages prior * to this call will be dropped. */ -void spdk_log_open(void); +void spdk_log_open(logfunc *logf); /** * Close the currently active log. Messages after this call diff --git a/lib/event/app.c b/lib/event/app.c index 0e9d11ab6..daee8a1d1 100644 --- a/lib/event/app.c +++ b/lib/event/app.c @@ -624,7 +624,7 @@ spdk_app_start(struct spdk_app_opts *opts, spdk_msg_fn start_fn, goto app_start_setup_conf_err; } - spdk_log_open(); + spdk_log_open(opts->log); SPDK_NOTICELOG("Total cores available: %d\n", spdk_env_get_core_count()); /* diff --git a/lib/log/log.c b/lib/log/log.c index 654dea3a7..07b2a649f 100644 --- a/lib/log/log.c +++ b/lib/log/log.c @@ -50,16 +50,24 @@ static const char *const spdk_level_names[] = { #define MAX_TMPBUF 1024 +static logfunc *g_log = NULL; + void -spdk_log_open(void) +spdk_log_open(logfunc *logf) { - openlog("spdk", LOG_PID, LOG_LOCAL7); + if (logf) { + g_log = logf; + } else { + openlog("spdk", LOG_PID, LOG_LOCAL7); + } } void spdk_log_close(void) { - closelog(); + if (!g_log) { + closelog(); + } } #ifdef SPDK_LOG_BACKTRACE_LVL @@ -126,20 +134,25 @@ spdk_log(enum spdk_log_level level, const char *file, const int line, const char return; } - va_start(ap, format); + if (g_log) { + g_log(level, file, line, func, format); - vsnprintf(buf, sizeof(buf), format, ap); + } else { + va_start(ap, format); - if (level <= g_spdk_log_print_level) { - fprintf(stderr, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf); - spdk_log_unwind_stack(stderr, level); + vsnprintf(buf, sizeof(buf), format, ap); + + if (level <= g_spdk_log_print_level) { + fprintf(stderr, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf); + spdk_log_unwind_stack(stderr, level); + } + + if (level <= g_spdk_log_level) { + syslog(severity, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf); + } + + va_end(ap); } - - if (level <= g_spdk_log_level) { - syslog(severity, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf); - } - - va_end(ap); } static void diff --git a/test/unit/lib/log/log.c/log_ut.c b/test/unit/lib/log/log.c/log_ut.c index c12e43964..a54daddd1 100644 --- a/test/unit/lib/log/log.c/log_ut.c +++ b/test/unit/lib/log/log.c/log_ut.c @@ -74,7 +74,7 @@ log_test(void) CU_ASSERT(spdk_log_get_flag("log") == false); #endif - spdk_log_open(); + spdk_log_open(NULL); spdk_log_set_flag("log"); SPDK_WARNLOG("log warning unit test\n"); SPDK_DEBUGLOG(SPDK_LOG_LOG, "log test\n");