|  | debug.h File Reference updated Fri Dec 9 2016 |

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

Macros
debug.h File Reference

RE/flex debug logs. More...

This graph shows which files directly or indirectly include this file:

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

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

Copyright
  (c) 2015-2016, Robert van Engelen, Genivia Inc. All rights reserved.
  (c) BSD-3 License - see LICENSE.txt

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.

The utility macro `DBGSTR(const char *s)` returns string `s` or `"(null)"` when `s == NULL`.

## Example

#include "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
{
// 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:

- Variadic macros with `__VA_ARGS__`.
- Standard predefined macros `__FILE__` and `__LINE__`.
- Macro "stringification": expand content of macro `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).

## Macro Definition Documentation

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