Fix: define macros for logging levels
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 21 Oct 2019 17:59:50 +0000 (13:59 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 23 Oct 2019 15:30:44 +0000 (11:30 -0400)
commit9103e903a89377e9cfad13905d4f4b650aecd061
treecb9e5cdff50e479513cccf669082209610a99940
parent8cca03c6f0678cf878a62ef7863cf6ec7b6dff2d
Fix: define macros for logging levels

Compiling with -Wundef, we get:

      CC       common.lo
    In file included from /home/smarchi/src/babeltrace/src/common/common.c:27:0:
    /home/smarchi/src/babeltrace/src/logging/log.h:58:24: warning: "BT_LOGGING_LEVEL_TRACE" is not defined, evaluates to 0 [-Wundef]
     #define BT_LOG_TRACE   BT_LOGGING_LEVEL_TRACE
                            ^
    /home/smarchi/src/babeltrace/src/logging/log.h:617:35: note: in definition of macro ‘BT_LOG_ENABLED’
     #define BT_LOG_ENABLED(lvl)     ((lvl) >= _BT_MINIMAL_LOG_LEVEL)
                                       ^~~
    /home/smarchi/src/babeltrace/src/logging/log.h:618:48: note: in expansion of macro ‘BT_LOG_TRACE’
     #define BT_LOG_ENABLED_TRACE    BT_LOG_ENABLED(BT_LOG_TRACE)
                                                    ^~~~~~~~~~~~
    /home/smarchi/src/babeltrace/src/logging/log.h:852:5: note: in expansion of macro ‘BT_LOG_ENABLED_TRACE’
     #if BT_LOG_ENABLED_TRACE
         ^~~~~~~~~~~~~~~~~~~~
    /home/smarchi/src/babeltrace/src/logging/log.h:59:24: warning: "BT_LOGGING_LEVEL_DEBUG" is not defined, evaluates to 0 [-Wundef]
     #define BT_LOG_DEBUG   BT_LOGGING_LEVEL_DEBUG
                            ^
    ../../src/common/config.h:26:30: note: in expansion of macro ‘BT_LOG_DEBUG’
     #define BT_MINIMAL_LOG_LEVEL BT_LOG_DEBUG
                                  ^~~~~~~~~~~~
    /home/smarchi/src/babeltrace/src/logging/log.h:92:32: note: in expansion of macro ‘BT_MINIMAL_LOG_LEVEL’
      #define _BT_MINIMAL_LOG_LEVEL BT_MINIMAL_LOG_LEVEL
                                    ^~~~~~~~~~~~~~~~~~~~
    /home/smarchi/src/babeltrace/src/logging/log.h:617:43: note: in expansion of macro ‘_BT_MINIMAL_LOG_LEVEL’
     #define BT_LOG_ENABLED(lvl)     ((lvl) >= _BT_MINIMAL_LOG_LEVEL)
                                               ^~~~~~~~~~~~~~~~~~~~~
    /home/smarchi/src/babeltrace/src/logging/log.h:618:33: note: in expansion of macro ‘BT_LOG_ENABLED’
     #define BT_LOG_ENABLED_TRACE    BT_LOG_ENABLED(BT_LOG_TRACE)
                                     ^~~~~~~~~~~~~~
    /home/smarchi/src/babeltrace/src/logging/log.h:852:5: note: in expansion of macro ‘BT_LOG_ENABLED_TRACE’
     #if BT_LOG_ENABLED_TRACE
         ^~~~~~~~~~~~~~~~~~~~

This shows that while BT_LOGGING_LEVEL_* are not preprocessor macros,
they are being used in preprocessor conditions.  This makes the logging
macros (e.g. BT_LOGT) always defined to the concrete logging code, even
though the minimal compile-time log level should make them defined to
nothing.

Fix that by defining macros for logging levels (not exposed in the API),
which we access in log.c, the library's logging system.  The macros are
defined in a new file, logging-defs.h, because it's not possible to
include logging.h twice.

Now that the BT_LOG_ENABLED_TRACE macro is working as intended, it then
exposes this problem when building with the default setting of
BABELTRACE_MINIMAL_LOG_LEVEL=DEBUG.

    /home/smarchi/src/babeltrace/src/plugins/ctf/common/metadata/parser.y:52:0: error: "YYDEBUG" redefined [-Werror]
     # define YYDEBUG 0

    In file included from /home/smarchi/src/babeltrace/src/plugins/ctf/common/metadata/parser.y:44:0:
    /home/smarchi/src/babeltrace/src/plugins/ctf/common/metadata/parser.h:37:0: note: this is the location of the previous definition
     # define YYDEBUG 1

When building parser.c, parser.h is first included and defines YYDEBUG
with:

    #ifndef YYDEBUG
    # define YYDEBUG 1
    #endif

Then, we come with:

    #if BT_LOG_ENABLED_TRACE
    # define YYDEBUG 1
    # define YYFPRINTF(_stream, _fmt, args...) BT_LOGT(_fmt, ## args)
    #else
    # define YYDEBUG 0
    #endif

If we want to control YYDEBUG based on BT_LOG_ENABLED_TRACE, we need to
define that before including parser.h.  But that's not the end of our
troubles!  If YYDEBUG is defined as 0 when building parser.c, it will
not generated a definition for the variable yydebug.  Some other files
declare their own "extern int yydebug;" unconditionally and access it.
If yydebug has no definition, this results in undefined references to
yydebug.

To solve it, I want to make all files who use yydebug include some file
to obtain the yydebug declaration, which will be properly gated with the
`#if BT_LOG_ENABLED_TRACE` condition.  That new file is `parser-wrap.h`.
To make sure that nobody includes parser.h directly (which would lead to
yydebug always being declared for them), I've made it so you need to
define a special macro (ALLOW_INCLUDE_PARSER_H) to include it, which
only parser-wrap.h does.

Change-Id: I01ddaa3c8e4d3e5c119ecf221203023b678cc6fb
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2228
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
14 files changed:
include/Makefile.am
include/babeltrace2/babeltrace.h
include/babeltrace2/logging-defs.h [new file with mode: 0644]
include/babeltrace2/logging.h
src/logging/log.h
src/plugins/ctf/common/metadata/Makefile.am
src/plugins/ctf/common/metadata/decoder-packetized-file-stream-to-buf.c
src/plugins/ctf/common/metadata/decoder.c
src/plugins/ctf/common/metadata/lexer.l
src/plugins/ctf/common/metadata/parser-wrap.h [new file with mode: 0644]
src/plugins/ctf/common/metadata/parser.y
src/plugins/ctf/common/metadata/visitor-generate-ir.c
src/plugins/ctf/common/metadata/visitor-parent-links.c
src/plugins/ctf/common/metadata/visitor-semantic-validator.c
This page took 0.026247 seconds and 4 git commands to generate.