debug.h File Reference

updated Mon Apr 10 2017 by Robert van Engelen
 
Macros
debug.h File Reference

RE/flex debug logs and assertions. More...

#include <cassert>
Include dependency graph for debug.h:
This graph shows which files directly or indirectly include this file:

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...
 

Detailed Description

RE/flex debug logs and assertions.

Author
Robert van Engelen - engel.nosp@m.en@g.nosp@m.enivi.nosp@m.a.co.nosp@m.m

Exploiting macro magic to simplify debug logging.

Usage

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.

Example

#include <reflex/debug.h>
int main(int argc, char *argv[])
{
FILE *fd;
DBGLOG("Program start");
if ((fd = fopen("foo.bar", "r")) == NULL)
{
DBGLOG("Error %d: %s ", errno, DBGSTR(strerror(errno)));
for (int i = 1; i < argc; ++i)
DBGLOGA(" %s", argv[1]);
}
else
{
DBGCHK(fd != NULL);
// OK, so go ahead to read foo.bar ...
// ...
fclose(fd);
}
DBGLOG("Program end");
}

Compiled with -DDEBUG this example logs the following messages in DEBUG.log:

140201/225654.692194 example.cpp:11 Program has started
140201/225654.692564 example.cpp:15 Error 2: No such file or directory
140201/225654.692577 example.cpp:17 Program ended

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:

Macro Definition Documentation

#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.