RE/flex debug logs and assertions. More...
#include <cassert>
Macros | |
#define | ASSERT(c) |
If ASSERT not defined, make ASSERT a no-op. More... | |
#define | DBGCHK(c) (void)0 |
#define | DBGLOG(...) (void)0 |
When compiled with -DDEBUG, adds a timestamped log entry with a printf-formatted message. More... | |
#define | DBGLOGN(...) (void)0 |
When compiled with -DDEBUG, adds a log entry with a printf-formatted message. More... | |
#define | DBGLOGA(...) (void)0 |
When compiled with -DDEBUG, appends a printf-formatted message to the last log entry. More... | |
RE/flex debug logs and assertions.
Exploiting macro magic to simplify debug logging.
DBGLOG(format, ...)
creates a timestamped log entry with a printf-formatted message. The log entry is added to a log file or sent to stderr
as specified:
Source files compiled with | DBGLOG(...) entry added to |
---|---|
c++ -DDEBUG | DEBUG.log |
c++ -DDEBUG=TEST | TEST.log |
c++ -DDEBUG= | stderr |
DBGLOGN(format, ...)
creates a log entry without a timestamp.
DBGLOGA(format, ...)
appends the formatted string to the previous log entry.
DBGCHK(condition)
calls assert(condition)
when compiled in DEBUG mode.
The utility macro DBGSTR(const char *s)
returns string s
or "(null)"
when s == NULL
.
Compiled with -DDEBUG
this example logs the following messages in DEBUG.log
:
The first column records the date (140201 is February 1, 2014) and the time (225654 is 10:56PM + 54 seconds) with microsecond fraction. The second column records the source code file name and the line number of the DBGLOG
command. The third column shows the printf-formatted message.
The DEBUG.log
file is created in the current directory when it does not already exist.
Techniques used:
__VA_ARGS__
.__FILE__
and __LINE__
.DEBUG
as a string in a macro body.#if DEBUG + 0
to test whether macro DEBUG
is set to a value, since DEBUG
is 1 when set without a value (for example at the command line)."" __VA_ARGS__
forces __VA_ARGS__
to start with a literal format string (printf security advisory). #define ASSERT | ( | c | ) |
If ASSERT not defined, make ASSERT a no-op.
#define DBGCHK | ( | c | ) | (void)0 |
#define DBGLOG | ( | ... | ) | (void)0 |
When compiled with -DDEBUG, adds a timestamped log entry with a printf-formatted message.
#define DBGLOGA | ( | ... | ) | (void)0 |
When compiled with -DDEBUG, appends a printf-formatted message to the last log entry.
#define DBGLOGN | ( | ... | ) | (void)0 |
When compiled with -DDEBUG, adds a log entry with a printf-formatted message.