Add bt_log_get_level_from_env() and use it
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sat, 27 May 2017 06:55:04 +0000 (02:55 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 9 Jun 2017 20:58:12 +0000 (16:58 -0400)
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 <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
cli/babeltrace.c
include/babeltrace/logging-internal.h
lib/logging.c
plugins/ctf/lttng-live/lttng-live.c

index e547c40f19849db8aaab5d840077f6560657cfa8..9de233bc1672718ec3bc3c31cd029b469a3e0b60 100644 (file)
@@ -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)
index ffcf1cdcf6fa3b0019a844753bc33ba180ffc9d5..bbe3596b905fbd9724ff91533afe68a42e87d8a5 100644 (file)
@@ -9,6 +9,8 @@
 #ifndef BABELTRACE_LOGGING_INTERNAL_H
 #define BABELTRACE_LOGGING_INTERNAL_H
 
+#include <stdlib.h>
+#include <stdio.h>
 #include <babeltrace/logging.h>
 
 /* 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
index 53c2a8467475373ba497c1b6109bee0b120ec8f1..371bc596048295f7a66c732ff3237c38e00aefef 100644 (file)
@@ -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(),
index ac7d35249f8c60ff62179f68d8d631020dc9242f..a84c9cc459a60cc225c4813c393794dc8197080b 100644 (file)
@@ -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);
 }
This page took 0.04998 seconds and 4 git commands to generate.