src/logging/log.h: add utility functions for log level to/from string
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sat, 15 Jun 2019 01:06:03 +0000 (21:06 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 20 Jun 2019 18:01:16 +0000 (14:01 -0400)
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I389b5da1a574523a986d2884126b8f862765d914
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1451
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
src/logging/log.h

index ce24318f61b98424e665b4ccb280d33fb5fc21a9..7368323d4310bdf314ae50205ae61903ef93940d 100644 (file)
@@ -15,6 +15,7 @@
 #include <string.h>
 #include <babeltrace2/logging.h>
 #include "common/macros.h"
+#include "common/assert.h"
 
 /* To detect incompatible changes you can define BT_LOG_VERSION_REQUIRED to be
  * the current value of BT_LOG_VERSION before including this file (or via
@@ -999,40 +1000,111 @@ void bt_log_out_stderr_callback(const bt_log_message *const msg, void *arg);
  */
 #define BT_LOG_STDERR (&_bt_log_stderr_spec)
 
+/*
+ * Returns the equivalent letter of the log level `level`.
+ *
+ * `level` must be a valid log level.
+ */
 static inline
-int bt_log_get_level_from_env(const char *var)
+char bt_log_get_letter_from_level(int level)
 {
-       const char *varval = getenv(var);
-       int level = BT_LOG_NONE;
-
-       if (!varval) {
-               goto end;
+       char letter;
+
+       switch (level) {
+       case BT_LOG_VERBOSE:
+               letter = 'V';
+               break;
+       case BT_LOG_DEBUG:
+               letter = 'D';
+               break;
+       case BT_LOG_INFO:
+               letter = 'I';
+               break;
+       case BT_LOG_WARN:
+               letter = 'W';
+               break;
+       case BT_LOG_ERROR:
+               letter = 'E';
+               break;
+       case BT_LOG_FATAL:
+               letter = 'F';
+               break;
+       case BT_LOG_NONE:
+               letter = 'N';
+               break;
+       default:
+               abort();
        }
 
-       if (strcmp(varval, "VERBOSE") == 0 ||
-                       strcmp(varval, "V") == 0) {
+       return letter;
+}
+
+/*
+ * Returns the log level for the string `str`, or -1 if `str` is not a
+ * valid log level string.
+ */
+static inline
+int bt_log_get_level_from_string(const char *str)
+{
+       int level = -1;
+
+       BT_ASSERT(str);
+
+       if (strcmp(str, "VERBOSE") == 0 ||
+                       strcmp(str, "V") == 0) {
                level = BT_LOG_VERBOSE;
-       } else if (strcmp(varval, "DEBUG") == 0 ||
-                       strcmp(varval, "D") == 0) {
+       } else if (strcmp(str, "DEBUG") == 0 ||
+                       strcmp(str, "D") == 0) {
                level = BT_LOG_DEBUG;
-       } else if (strcmp(varval, "INFO") == 0 ||
-                       strcmp(varval, "I") == 0) {
+       } else if (strcmp(str, "INFO") == 0 ||
+                       strcmp(str, "I") == 0) {
                level = BT_LOG_INFO;
-       } else if (strcmp(varval, "WARN") == 0 ||
-                       strcmp(varval, "WARNING") == 0 ||
-                       strcmp(varval, "W") == 0) {
+       } else if (strcmp(str, "WARN") == 0 ||
+                       strcmp(str, "WARNING") == 0 ||
+                       strcmp(str, "W") == 0) {
                level = BT_LOG_WARN;
-       } else if (strcmp(varval, "ERROR") == 0 ||
-                       strcmp(varval, "E") == 0) {
+       } else if (strcmp(str, "ERROR") == 0 ||
+                       strcmp(str, "E") == 0) {
                level = BT_LOG_ERROR;
-       } else if (strcmp(varval, "FATAL") == 0 ||
-                       strcmp(varval, "F") == 0) {
+       } else if (strcmp(str, "FATAL") == 0 ||
+                       strcmp(str, "F") == 0) {
                level = BT_LOG_FATAL;
-       } else if (strcmp(varval, "NONE") == 0 ||
-                       strcmp(varval, "N") == 0) {
+       } else if (strcmp(str, "NONE") == 0 ||
+                       strcmp(str, "N") == 0) {
                level = BT_LOG_NONE;
        } else {
-               /* Should we warn here? How? */
+               /* FIXME: Should we warn here? How? */
+       }
+
+       return level;
+}
+
+/*
+ * Returns the log level for the letter `letter`, or -1 if `letter` is
+ * not a valid log level string.
+ */
+static inline
+int bt_log_get_level_from_letter(char letter)
+{
+       char str[] = {letter, '\0'};
+
+       return bt_log_get_level_from_string(str);
+}
+
+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;
+       }
+
+       level = bt_log_get_level_from_string(varval);
+       if (level < 0) {
+               /* FIXME: Should we warn here? How? */
+               level = BT_LOG_NONE;
        }
 
 end:
This page took 0.027487 seconds and 4 git commands to generate.