From 65918cb8ff4baab18f4d56b306f5d6c5606030d1 Mon Sep 17 00:00:00 2001 From: Ben Walker Date: Tue, 13 Jun 2017 11:26:21 -0700 Subject: [PATCH] log: Add a print to stderr threshold Change-Id: I9c1865ff1ca2d8093227c89aa9857ff18d3e346a Signed-off-by: Ben Walker Reviewed-on: https://review.gerrithub.io/365294 Tested-by: SPDK Automated Test System Reviewed-by: Daniel Verkamp Reviewed-by: Jim Harris --- app/iscsi_tgt/iscsi_tgt.c | 7 +++++-- app/nvmf_tgt/nvmf_main.c | 7 +++++-- app/vhost/vhost.c | 7 +++++-- include/spdk/log.h | 14 ++++++++++---- lib/log/log.c | 22 +++++++++++++++++----- 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/app/iscsi_tgt/iscsi_tgt.c b/app/iscsi_tgt/iscsi_tgt.c index 58911982dc..fe4adae2e2 100644 --- a/app/iscsi_tgt/iscsi_tgt.c +++ b/app/iscsi_tgt/iscsi_tgt.c @@ -95,6 +95,7 @@ main(int argc, char **argv) int rc, app_rc; int daemon_mode = 0; struct spdk_app_opts opts = {}; + enum spdk_log_level print_level = SPDK_LOG_NOTICE; /* default value in opts structure */ spdk_app_opts_init(&opts); @@ -134,7 +135,7 @@ main(int argc, char **argv) opts.tpoint_group_mask = optarg; break; case 'q': - spdk_g_notice_stderr_flag = 0; + print_level = SPDK_LOG_WARN; break; case 'm': opts.reactor_mask = optarg; @@ -165,7 +166,7 @@ main(int argc, char **argv) } } - if (spdk_g_notice_stderr_flag == 1 && + if (print_level > SPDK_LOG_WARN && isatty(STDERR_FILENO) && !strncmp(ttyname(STDERR_FILENO), "/dev/tty", strlen("/dev/tty"))) { printf("Warning: printing stderr to console terminal without -q option specified.\n"); @@ -175,6 +176,8 @@ main(int argc, char **argv) sleep(10); } + spdk_log_set_print_level(print_level); + opts.shutdown_cb = spdk_iscsi_shutdown; opts.usr1_handler = spdk_sigusr1; diff --git a/app/nvmf_tgt/nvmf_main.c b/app/nvmf_tgt/nvmf_main.c index 1342740b3c..1cea02f094 100644 --- a/app/nvmf_tgt/nvmf_main.c +++ b/app/nvmf_tgt/nvmf_main.c @@ -74,6 +74,7 @@ main(int argc, char **argv) int ch; int rc; struct spdk_app_opts opts = {}; + enum spdk_log_level print_level = SPDK_LOG_NOTICE; /* default value in opts */ spdk_app_opts_init(&opts); @@ -125,7 +126,7 @@ main(int argc, char **argv) opts.tpoint_group_mask = optarg; break; case 'q': - spdk_g_notice_stderr_flag = 0; + print_level = SPDK_LOG_WARN; break; case 'D': case 'H': @@ -135,7 +136,7 @@ main(int argc, char **argv) } } - if (spdk_g_notice_stderr_flag == 1 && + if (print_level > SPDK_LOG_WARN && isatty(STDERR_FILENO) && !strncmp(ttyname(STDERR_FILENO), "/dev/tty", strlen("/dev/tty"))) { printf("Warning: printing stderr to console terminal without -q option specified.\n"); @@ -145,6 +146,8 @@ main(int argc, char **argv) sleep(10); } + spdk_log_set_print_level(print_level); + rc = spdk_nvmf_tgt_start(&opts); return rc; diff --git a/app/vhost/vhost.c b/app/vhost/vhost.c index 37ae14de9f..d1391e583c 100644 --- a/app/vhost/vhost.c +++ b/app/vhost/vhost.c @@ -83,6 +83,7 @@ main(int argc, char *argv[]) char ch; int rc; const char *socket_path = NULL; + enum spdk_log_level print_level = SPDK_LOG_NOTICE; vhost_app_opts_init(&opts); @@ -110,7 +111,7 @@ main(int argc, char *argv[]) opts.master_core = strtoul(optarg, NULL, 10); break; case 'q': - spdk_g_notice_stderr_flag = 0; + print_level = SPDK_LOG_WARN; break; case 's': opts.mem_size = strtoul(optarg, NULL, 10); @@ -139,7 +140,7 @@ main(int argc, char *argv[]) } } - if (spdk_g_notice_stderr_flag == 1 && + if (print_level > SPDK_LOG_WARN && isatty(STDERR_FILENO) && !strncmp(ttyname(STDERR_FILENO), "/dev/tty", strlen("/dev/tty"))) { printf("Warning: printing stderr to console terminal without -q option specified.\n"); @@ -149,6 +150,8 @@ main(int argc, char *argv[]) sleep(10); } + spdk_log_set_print_level(print_level); + opts.shutdown_cb = spdk_vhost_shutdown_cb; /* Blocks until the application is exiting */ diff --git a/include/spdk/log.h b/include/spdk/log.h index 30e9467afa..6c47a8a4ba 100644 --- a/include/spdk/log.h +++ b/include/spdk/log.h @@ -72,11 +72,17 @@ void spdk_log_set_level(enum spdk_log_level level); */ enum spdk_log_level spdk_log_get_level(void); -/* - * Default: 1 - noticelog messages will print to stderr and syslog. - * Can be set to 0 to print noticelog messages to syslog only. +/** + * Set the current log level threshold for printing to stderr. + * Messages with a level less than or equal to this level + * are also printed to stderr. */ -extern unsigned int spdk_g_notice_stderr_flag; +void spdk_log_set_print_level(enum spdk_log_level level); + +/** + * Get the current log level print threshold. + */ +enum spdk_log_level spdk_log_get_print_level(void); #define SPDK_NOTICELOG(...) \ spdk_log(SPDK_LOG_NOTICE, __FILE__, __LINE__, __func__, __VA_ARGS__) diff --git a/lib/log/log.c b/lib/log/log.c index ef8b19b891..ceadd4591a 100644 --- a/lib/log/log.c +++ b/lib/log/log.c @@ -37,9 +37,9 @@ static TAILQ_HEAD(, spdk_trace_flag) g_trace_flags = TAILQ_HEAD_INITIALIZER(g_trace_flags); -unsigned int spdk_g_notice_stderr_flag = 1; int spdk_g_log_facility = LOG_DAEMON; static enum spdk_log_level g_spdk_log_level = SPDK_LOG_NOTICE; +static enum spdk_log_level g_spdk_log_print_level = SPDK_LOG_NOTICE; SPDK_LOG_REGISTER_TRACE_FLAG("debug", SPDK_TRACE_DEBUG) @@ -114,6 +114,17 @@ spdk_log_get_level(void) { return g_spdk_log_level; } +void +spdk_log_set_print_level(enum spdk_log_level level) +{ + g_spdk_log_print_level = level; +} + +enum spdk_log_level +spdk_log_get_print_level(void) { + return g_spdk_log_print_level; +} + int spdk_set_log_facility(const char *facility) { @@ -175,11 +186,12 @@ spdk_log(enum spdk_log_level level, const char *file, const int line, const char vsnprintf(buf, sizeof(buf), format, ap); - if (level <= g_spdk_log_level) { + if (level <= g_spdk_log_print_level) { fprintf(stderr, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf); - if (level <= SPDK_LOG_NOTICE) { - syslog(severity, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf); - } + } + + if (level <= g_spdk_log_level) { + syslog(severity, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf); } va_end(ap);