From 5d2d01c46285cd45104c6ed91dd46fb47fc9f224 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Wed, 31 May 2017 12:48:52 -0400 Subject: [PATCH] logging-internal.h: add BT_LOG_LEVEL_EXTERN_SYMBOL(), BT_LOG_INIT_LOG_LEVEL() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit BT_LOG_INIT_LOG_LEVEL() defines a hidden log level symbol and a static constructor which initializes this symbol according to the value of a given environment variable. This macro should be used in a module-wise dedicated logging source file, `logging.c`, as such: #define BT_LOG_OUTPUT_LEVEL some_log_level_symbol #include BT_LOG_INIT_LOG_LEVEL(some_log_level_symbol, "BABELTRACE_SOME_LOG_LEVEL"); BT_LOG_LEVEL_EXTERN_SYMBOL() declares this symbol as `extern`. It should be used in a module-wise dedicated logging header, `logging.h`, as such: #define BT_LOG_OUTPUT_LEVEL some_log_level_symbol #include BT_LOG_LEVEL_EXTERN_SYMBOL(some_log_level_symbol); Any module source file which contains log statements should use the following lines **before any #include line**: #define BT_LOG_TAG "THE-MODULE-TAG" #include "logging.h" If there are subdirectories within a module, it is possible that you need to include "../logging.h" or "../../logging.h", for example. The rule of thumb is that no module file, except `logging.c`, should include directly. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- include/babeltrace/logging-internal.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/include/babeltrace/logging-internal.h b/include/babeltrace/logging-internal.h index 8805306e..df21c21e 100644 --- a/include/babeltrace/logging-internal.h +++ b/include/babeltrace/logging-internal.h @@ -1004,8 +1004,19 @@ 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 */ -- 2.34.1