Add bt_log_get_level_from_env() and use it
[babeltrace.git] / include / babeltrace / logging-internal.h
index 5cad0c32efc98a0def7bf1111cca64b9e5526a09..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
@@ -16,7 +18,7 @@
  * compiler command line):
  *
  *   #define BT_LOG_VERSION_REQUIRED 4
- *   #include <babeltrace/log-internal.h>
+ *   #include <babeltrace/logging-internal.h>
  *
  * Compilation will fail when included file has different version.
  */
@@ -74,7 +76,7 @@
  * before including bt_log.h:
  *
  *   #define BT_LOG_LEVEL BT_LOG_VERBOSE
- *   #include <babeltrace/log-internal.h>
+ *   #include <babeltrace/logging-internal.h>
  *
  * If both BT_LOG_DEF_LEVEL and BT_LOG_LEVEL are undefined, then BT_LOG_INFO
  * will be used for release builds (NDEBUG is defined) and BT_LOG_DEBUG
  * Example:
  *
  *   #define BT_LOG_OUTPUT_LEVEL g_module_log_level
- *   #include <babeltrace/log-internal.h>
+ *   #include <babeltrace/logging-internal.h>
  *   static int g_module_log_level = BT_LOG_INFO;
  *   static void foo() {
  *       BT_LOGI("Will check g_module_log_level for output log level");
  * must be the first field in this structure:
  *
  *   #define BT_LOG_OUTPUT_LEVEL (g_config.log_level)
- *   #include <babeltrace/log-internal.h>
+ *   #include <babeltrace/logging-internal.h>
  *   struct config {
  *       int log_level;
  *       unsigned other_field;
  * before including bt_log.h:
  *
  *   #define BT_LOG_TAG "MAIN"
- *   #include <babeltrace/log-internal.h>
+ *   #include <babeltrace/logging-internal.h>
  *
  * If both BT_LOG_DEF_TAG and BT_LOG_TAG are undefined no tag will be added to
  * the log message (tag prefix still could be added though).
  * before including bt_log.h:
  *
  *   #define BT_LOG_SRCLOC BT_LOG_SRCLOC_NONE
- *   #include <babeltrace/log-internal.h>
+ *   #include <babeltrace/logging-internal.h>
  *
  * If both BT_LOG_DEF_SRCLOC and BT_LOG_SRCLOC are undefined, then
  * BT_LOG_SRCLOC_NONE will be used for release builds (NDEBUG is defined) and
  * very careful not to push such temporary changes to source control):
  *
  *   #define BT_LOG_CENSORING BT_LOG_UNCENSORED
- *   #include <babeltrace/log-internal.h>
+ *   #include <babeltrace/logging-internal.h>
  *
  * If both BT_LOG_DEF_CENSORING and BT_LOG_CENSORING are undefined, then
  * BT_LOG_CENSORED will be used for release builds (NDEBUG is defined) and
  *
  * For example, in log_config.c:
  *
- *   #include <babeltrace/log-internal.h>
+ *   #include <babeltrace/logging-internal.h>
  *   BT_LOG_DEFINE_TAG_PREFIX = "MyApp";
  *   BT_LOG_DEFINE_GLOBAL_FORMAT = {CUSTOM_MEM_WIDTH};
  *   BT_LOG_DEFINE_GLOBAL_OUTPUT = {BT_LOG_PUT_STD, custom_output_callback, 0};
  *
  *   // KittyHttpLogging.h
  *   #define BT_LOG_LIBRARY_PREFIX KittyHttp_
- *   #include <babeltrace/log-internal.h>
+ *   #include <babeltrace/logging-internal.h>
  *
  * Regardless of the method chosen, the end result is that bt_log symbols will
  * be prefixed with "KittyHttp_", so if a user of KittyHttp (say DogeBrowser)
 #endif
 
 #if defined(__printflike)
-       #define _BT_LOG_PRINTFLIKE(a, b) __printflike(a, b)
+       #define _BT_LOG_PRINTFLIKE(str_index, first_to_check) \
+               __printflike(str_index, first_to_check)
+#elif defined(__GNUC__)
+       #define _BT_LOG_PRINTFLIKE(str_index, first_to_check) \
+               __attribute__((format(__printf__, str_index, first_to_check)))
 #else
-       #define _BT_LOG_PRINTFLIKE(a, b)
+       #define _BT_LOG_PRINTFLIKE(str_index, first_to_check)
 #endif
 
 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__GNUC__)
@@ -953,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
This page took 0.0402 seconds and 4 git commands to generate.