Debugging macros

T

The Directive

Where can I find C++ code for debugging macros like TRACE, DEBUG and
etc. that are independent of any proprietary code. I rather not have
to reinvent the wheel.

In the mean time, I'm trying to define a TRACE macro like this:

#ifdef DEBUG
#define TRACE(arg) cout << (arg)
#else
#define TRACE(arg)
#endif

For example:

TRACE("Hello world!");

It seems to work but is this the best way? I'm not sure about the
empty define statement. I've seen third party code use a "(void)0"
construct instead of an empty define stement. Which is the correct or
best way? I want the code completely removed when DEBUG is not
defined (Of course, the only thing left would be the simicolon ";"
after the TRACE stement.)
 
N

Nick Hounsome

The Directive said:
Where can I find C++ code for debugging macros like TRACE, DEBUG and
etc. that are independent of any proprietary code. I rather not have
to reinvent the wheel.

In the mean time, I'm trying to define a TRACE macro like this:

#ifdef DEBUG
#define TRACE(arg) cout << (arg)

1. Use brackets around the whole thing but not around arg. This allows
TRACE("x is " << setw(4) << x) etc. which wont work with (arg).
2. You probably want an endl
3. You should be using fully qualified names because the std namespace is
not part of your defined interface i.e.
#else
#define TRACE(arg)

#define TRACE(arg) sizeof(std::cout << arg << std::endl)

The advantage of this is that your arg is checked by the compiler even when
not used but there is no runtime overhead.
I have worked on many progs before where I turned debug on and the prog
wouldn't compile because someone had chamged the code but not the debug.
#endif

For example:

TRACE("Hello world!");

It seems to work but is this the best way? I'm not sure about the
empty define statement. I've seen third party code use a "(void)0"
construct instead of an empty define stement. Which is the correct or

The problem with my no debug solution above and (void)0 is that many
compilers will warn you about a statement with no effect which can onscure
real problems.
best way? I want the code completely removed when DEBUG is not
defined (Of course, the only thing left would be the simicolon ";"
after the TRACE stement.)

No - the sizeof approach is better for the reasons that I give.
 
T

The Directive

[Snip]
#define TRACE(arg) sizeof(std::cout << arg << std::endl)

The advantage of this is that your arg is checked by the compiler even when
not used but there is no runtime overhead.

There's no runtime overhead? Absolutely sure? Isn't this code still
present at runtime? Or does the compiler remove it after checking the
argument? I want the code to be completly removed so that it does not
ship with the release code.
 
R

Ron Natalie

The Directive said:
[Snip]
#define TRACE(arg) sizeof(std::cout << arg << std::endl)

The advantage of this is that your arg is checked by the compiler even when
not used but there is no runtime overhead.

There's no runtime overhead? Absolutely sure? Isn't this code still
present at runtime?

It turns into a constant (sizeof never evaluates it's operand). If insert constant
expressions into the code that aren't used, most compilers will optimize them away.
What is it to do with a statement like:

5;
anyhow?
 
G

Graham Dumpleton

Where can I find C++ code for debugging macros like TRACE, DEBUG and
etc. that are independent of any proprietary code. I rather not have
to reinvent the wheel.

In the mean time, I'm trying to define a TRACE macro like this:

#ifdef DEBUG
#define TRACE(arg) cout << (arg)
#else
#define TRACE(arg)
#endif

For example:

TRACE("Hello world!");

It seems to work but is this the best way?

A better scheme is to harness the use of the C++ streams classes.

#ifdef DEBUG
#define tracer if (0) ; else cerr
#else
#define tracer if (1) ; else cerr
#endif

tracer << "something bad happened" << endl;

What is good about this is that you can format more than text strings, ie.,
anything which can be formatted into a stream. The code also looks more
natural as a result. And no the code will not be present when DEBUG isn't
defined as any sane compiler will realise the code in the else part can't
ever be called and leave it out.

For a look at a quite comprehensive debugging mechanism using these
concepts, look at the OSE library at "http://ose.sourceforge.net". There
is a chapter in the library manual on the program debugging support.
This might give you some ideas even if you want to stear clear of third
party libraries.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top