From 373c938b7b096e1bcb09623c56b7c82b3b7a6744 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 27 May 2017 02:55:04 -0400 Subject: [PATCH] Add bt_log_get_level_from_env() and use it MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This is a common function for all the project's modules to use the same names (`VERBOSE`, `WARN`, `ERROR`, etc.) to set the various log levels based on the value of a specific environment variable. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- cli/babeltrace.c | 26 +---------------- include/babeltrace/logging-internal.h | 42 +++++++++++++++++++++++++++ lib/logging.c | 24 ++------------- plugins/ctf/lttng-live/lttng-live.c | 28 +----------------- 4 files changed, 46 insertions(+), 74 deletions(-) diff --git a/cli/babeltrace.c b/cli/babeltrace.c index e547c40f..9de233bc 100644 --- a/cli/babeltrace.c +++ b/cli/babeltrace.c @@ -1860,31 +1860,7 @@ void warn_command_name_and_directory_clash(struct bt_config *cfg) static void init_log_level(void) { - enum bt_logging_level log_level = BT_LOG_NONE; - const char *log_level_env = getenv("BABELTRACE_CLI_LOG_LEVEL"); - - if (!log_level_env) { - goto set_level; - } - - if (strcmp(log_level_env, "VERBOSE") == 0) { - log_level = BT_LOGGING_LEVEL_VERBOSE; - } else if (strcmp(log_level_env, "DEBUG") == 0) { - log_level = BT_LOGGING_LEVEL_DEBUG; - } else if (strcmp(log_level_env, "INFO") == 0) { - log_level = BT_LOGGING_LEVEL_INFO; - } else if (strcmp(log_level_env, "WARN") == 0) { - log_level = BT_LOGGING_LEVEL_WARN; - } else if (strcmp(log_level_env, "ERROR") == 0) { - log_level = BT_LOGGING_LEVEL_ERROR; - } else if (strcmp(log_level_env, "FATAL") == 0) { - log_level = BT_LOGGING_LEVEL_FATAL; - } else if (strcmp(log_level_env, "NONE") == 0) { - log_level = BT_LOGGING_LEVEL_NONE; - } - -set_level: - bt_cli_log_level = log_level; + bt_cli_log_level = bt_log_get_level_from_env("BABELTRACE_CLI_LOG_LEVEL"); } void set_sigint_handler(void) diff --git a/include/babeltrace/logging-internal.h b/include/babeltrace/logging-internal.h index ffcf1cdc..bbe3596b 100644 --- a/include/babeltrace/logging-internal.h +++ b/include/babeltrace/logging-internal.h @@ -9,6 +9,8 @@ #ifndef BABELTRACE_LOGGING_INTERNAL_H #define BABELTRACE_LOGGING_INTERNAL_H +#include +#include #include /* To detect incompatible changes you can define BT_LOG_VERSION_REQUIRED to be @@ -957,6 +959,46 @@ void bt_log_out_stderr_callback(const bt_log_message *const msg, void *arg); */ #define BT_LOG_STDERR (&_bt_log_stderr_spec) +static inline +int bt_log_get_level_from_env(const char *var) +{ + const char *varval = getenv(var); + int level = BT_LOG_NONE; + + if (!varval) { + goto end; + } + + if (strcmp(varval, "VERBOSE") == 0 || + strcmp(varval, "V") == 0) { + level = BT_LOG_VERBOSE; + } else if (strcmp(varval, "DEBUG") == 0 || + strcmp(varval, "D") == 0) { + level = BT_LOG_DEBUG; + } else if (strcmp(varval, "INFO") == 0 || + strcmp(varval, "I") == 0) { + level = BT_LOG_INFO; + } else if (strcmp(varval, "WARN") == 0 || + strcmp(varval, "WARNING") == 0 || + strcmp(varval, "W") == 0) { + level = BT_LOG_WARN; + } else if (strcmp(varval, "ERROR") == 0 || + strcmp(varval, "E") == 0) { + level = BT_LOG_ERROR; + } else if (strcmp(varval, "FATAL") == 0 || + strcmp(varval, "F") == 0) { + level = BT_LOG_FATAL; + } else if (strcmp(varval, "NONE") == 0 || + strcmp(varval, "N") == 0) { + level = BT_LOG_NONE; + } else { + /* Should we warn here? How? */ + } + +end: + return level; +} + #ifdef __cplusplus } #endif diff --git a/lib/logging.c b/lib/logging.c index 53c2a846..371bc596 100644 --- a/lib/logging.c +++ b/lib/logging.c @@ -48,31 +48,11 @@ void bt_logging_set_global_level(enum bt_logging_level log_level) static void __attribute__((constructor)) bt_logging_ctor(void) { - enum bt_logging_level log_level = BT_LOG_NONE; - const char *log_level_env = getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL"); const char *v_extra = bt_version_get_extra() ? bt_version_get_extra() : ""; - if (!log_level_env) { - goto set_level; - } - - if (strcmp(log_level_env, "VERBOSE") == 0) { - log_level = BT_LOGGING_LEVEL_VERBOSE; - } else if (strcmp(log_level_env, "DEBUG") == 0) { - log_level = BT_LOGGING_LEVEL_DEBUG; - } else if (strcmp(log_level_env, "INFO") == 0) { - log_level = BT_LOGGING_LEVEL_INFO; - } else if (strcmp(log_level_env, "WARN") == 0) { - log_level = BT_LOGGING_LEVEL_WARN; - } else if (strcmp(log_level_env, "ERROR") == 0) { - log_level = BT_LOGGING_LEVEL_ERROR; - } else if (strcmp(log_level_env, "FATAL") == 0) { - log_level = BT_LOGGING_LEVEL_FATAL; - } - -set_level: - bt_logging_set_global_level(log_level); + bt_logging_set_global_level( + bt_log_get_level_from_env("BABELTRACE_LOGGING_GLOBAL_LEVEL")); BT_LOGI("Babeltrace %d.%d.%d%s library loaded: " "major=%d, minor=%d, patch=%d, extra=\"%s\"", bt_version_get_major(), bt_version_get_minor(), diff --git a/plugins/ctf/lttng-live/lttng-live.c b/plugins/ctf/lttng-live/lttng-live.c index ac7d3524..a84c9cc4 100644 --- a/plugins/ctf/lttng-live/lttng-live.c +++ b/plugins/ctf/lttng-live/lttng-live.c @@ -1121,31 +1121,5 @@ end: static void __attribute__((constructor)) bt_lttng_live_logging_ctor(void) { - enum bt_logging_level log_level = BT_LOG_NONE; - const char *log_level_env = getenv(BT_LOGLEVEL_NAME); - - if (!log_level_env) { - return; - } - - if (strcmp(log_level_env, "VERBOSE") == 0) { - log_level = BT_LOGGING_LEVEL_VERBOSE; - } else if (strcmp(log_level_env, "DEBUG") == 0) { - log_level = BT_LOGGING_LEVEL_DEBUG; - } else if (strcmp(log_level_env, "INFO") == 0) { - log_level = BT_LOGGING_LEVEL_INFO; - } else if (strcmp(log_level_env, "WARN") == 0) { - log_level = BT_LOGGING_LEVEL_WARN; - } else if (strcmp(log_level_env, "ERROR") == 0) { - log_level = BT_LOGGING_LEVEL_ERROR; - } else if (strcmp(log_level_env, "FATAL") == 0) { - log_level = BT_LOGGING_LEVEL_FATAL; - } else { - bt_lttng_live_log_level = BT_LOGGING_LEVEL_FATAL; - BT_LOGF("Incorrect log level specified in %s", - BT_LOGLEVEL_NAME); - abort(); - } - - bt_lttng_live_log_level = log_level; + bt_lttng_live_log_level = bt_log_get_level_from_env(BT_LOGLEVEL_NAME); } -- 2.34.1