logging-internal.h: add BT_LOG_LEVEL_EXTERN_SYMBOL(), BT_LOG_INIT_LOG_LEVEL()
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 31 May 2017 16:48:52 +0000 (12:48 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 9 Jun 2017 20:58:13 +0000 (16:58 -0400)
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 <babeltrace/logging-internal.h>

    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 <babeltrace/logging-internal.h>

    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
<babeltrace/logging-internal.h> directly.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
include/babeltrace/logging-internal.h

index 8805306e9424228a052dbc0a8b44f8c094264559..df21c21ef9ad5eba3cd27322c97dfa2d5f50e195 100644 (file)
@@ -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 */
This page took 0.025061 seconds and 4 git commands to generate.