log: passing user-defined log

Change-Id: I993e15a6e75029b0717960d5da31325e7f3522c1
Signed-off-by: zkhatami88 <z.khatami88@gmail.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/456407
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
This commit is contained in:
paul luse 2019-05-20 18:29:45 -04:00 committed by Darek Stojaczyk
parent f87568a095
commit 46523f33e0
5 changed files with 46 additions and 17 deletions

View File

@ -132,6 +132,19 @@ struct spdk_app_opts {
/** Opaque context for use of the env implementation. */ /** Opaque context for use of the env implementation. */
void *env_context; 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);
}; };
/** /**

View File

@ -45,11 +45,14 @@
extern "C" { extern "C" {
#endif #endif
typedef void logfunc(int level, const char *file, const int line,
const char *func, const char *format);
/** /**
* Initialize the logging module. Messages prior * Initialize the logging module. Messages prior
* to this call will be dropped. * 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 * Close the currently active log. Messages after this call

View File

@ -624,7 +624,7 @@ spdk_app_start(struct spdk_app_opts *opts, spdk_msg_fn start_fn,
goto app_start_setup_conf_err; 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()); SPDK_NOTICELOG("Total cores available: %d\n", spdk_env_get_core_count());
/* /*

View File

@ -50,16 +50,24 @@ static const char *const spdk_level_names[] = {
#define MAX_TMPBUF 1024 #define MAX_TMPBUF 1024
static logfunc *g_log = NULL;
void void
spdk_log_open(void) spdk_log_open(logfunc *logf)
{ {
if (logf) {
g_log = logf;
} else {
openlog("spdk", LOG_PID, LOG_LOCAL7); openlog("spdk", LOG_PID, LOG_LOCAL7);
}
} }
void void
spdk_log_close(void) spdk_log_close(void)
{ {
if (!g_log) {
closelog(); closelog();
}
} }
#ifdef SPDK_LOG_BACKTRACE_LVL #ifdef SPDK_LOG_BACKTRACE_LVL
@ -126,6 +134,10 @@ spdk_log(enum spdk_log_level level, const char *file, const int line, const char
return; return;
} }
if (g_log) {
g_log(level, file, line, func, format);
} else {
va_start(ap, format); va_start(ap, format);
vsnprintf(buf, sizeof(buf), format, ap); vsnprintf(buf, sizeof(buf), format, ap);
@ -140,6 +152,7 @@ spdk_log(enum spdk_log_level level, const char *file, const int line, const char
} }
va_end(ap); va_end(ap);
}
} }
static void static void

View File

@ -74,7 +74,7 @@ log_test(void)
CU_ASSERT(spdk_log_get_flag("log") == false); CU_ASSERT(spdk_log_get_flag("log") == false);
#endif #endif
spdk_log_open(); spdk_log_open(NULL);
spdk_log_set_flag("log"); spdk_log_set_flag("log");
SPDK_WARNLOG("log warning unit test\n"); SPDK_WARNLOG("log warning unit test\n");
SPDK_DEBUGLOG(SPDK_LOG_LOG, "log test\n"); SPDK_DEBUGLOG(SPDK_LOG_LOG, "log test\n");