X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=include%2Fbabeltrace%2Flogging-internal.h;h=df21c21ef9ad5eba3cd27322c97dfa2d5f50e195;hb=675860f431adca1ab381c80955c402f8480d8259;hp=1480a0829c12335f485d2b5e4db186bd4947fa60;hpb=40c958cfbacf5f2deb768ce216aa2395dd888462;p=babeltrace.git diff --git a/include/babeltrace/logging-internal.h b/include/babeltrace/logging-internal.h index 1480a082..df21c21e 100644 --- a/include/babeltrace/logging-internal.h +++ b/include/babeltrace/logging-internal.h @@ -9,7 +9,10 @@ #ifndef BABELTRACE_LOGGING_INTERNAL_H #define BABELTRACE_LOGGING_INTERNAL_H +#include +#include #include +#include /* 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 @@ -153,7 +156,11 @@ #if defined(BT_LOG_OUTPUT_LEVEL) #define _BT_LOG_OUTPUT_LEVEL BT_LOG_OUTPUT_LEVEL #else - #define _BT_LOG_OUTPUT_LEVEL _bt_log_global_output_lvl + /* + * We disallow this to make sure Babeltrace modules always + * have their own local log level. + */ + #error No log level symbol specified: please define BT_LOG_OUTPUT_LEVEL before including this header. #endif /* "Tag" is a compound string that could be associated with a log message. It @@ -434,9 +441,13 @@ #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,8 +964,59 @@ 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; +} + +#define BT_LOG_LEVEL_EXTERN_SYMBOL(_level_sym) \ + extern int _level_sym + +#define BT_LOG_INIT_LOG_LEVEL(_level_sym, _env_var) \ + BT_HIDDEN int _level_sym = BT_LOG_NONE; \ + static \ + void __attribute__((constructor)) _bt_log_level_ctor(void) \ + { \ + _level_sym = bt_log_get_level_from_env(_env_var); \ + } + #ifdef __cplusplus } #endif -#endif +#endif /* BABELTRACE_LOGGING_INTERNAL_H */