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:
parent
f87568a095
commit
46523f33e0
@ -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);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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
|
||||||
|
@ -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());
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -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)
|
||||||
{
|
{
|
||||||
openlog("spdk", LOG_PID, LOG_LOCAL7);
|
if (logf) {
|
||||||
|
g_log = logf;
|
||||||
|
} else {
|
||||||
|
openlog("spdk", LOG_PID, LOG_LOCAL7);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
spdk_log_close(void)
|
spdk_log_close(void)
|
||||||
{
|
{
|
||||||
closelog();
|
if (!g_log) {
|
||||||
|
closelog();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SPDK_LOG_BACKTRACE_LVL
|
#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;
|
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) {
|
vsnprintf(buf, sizeof(buf), format, ap);
|
||||||
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_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
|
static void
|
||||||
|
@ -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");
|
||||||
|
Loading…
Reference in New Issue
Block a user