C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Debugging macros

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
The Directive
Guest





PostPosted: Wed Dec 31, 2003 1:23 am    Post subject: Debugging macros Reply with quote



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.)
Back to top
Nick Hounsome
Guest





PostPosted: Wed Dec 31, 2003 8:12 am    Post subject: Re: Debugging macros Reply with quote




"The Directive" <the_directive (AT) hotmail (DOT) com> wrote

Quote:
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.
#include #define TRACE(arg) (std::cout << arg << std::endl)

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

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

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



Back to top
The Directive
Guest





PostPosted: Wed Dec 31, 2003 8:38 pm    Post subject: Re: Debugging macros Reply with quote



[Snip]

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

Back to top
Ron Natalie
Guest





PostPosted: Wed Dec 31, 2003 8:57 pm    Post subject: Re: Debugging macros Reply with quote


"The Directive" <the_directive (AT) hotmail (DOT) com> wrote

Quote:
[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?

Back to top
The Directive
Guest





PostPosted: Thu Jan 01, 2004 9:31 pm    Post subject: Re: Debugging macros Reply with quote

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

Check out this article:

http://www.codeproject.com/cpp/GuimaMacro.asp#xx701517xx

Back to top
Graham Dumpleton
Guest





PostPosted: Sat Jan 03, 2004 10:38 pm    Post subject: Re: Debugging macros Reply with quote

[email]the_directive (AT) hotmail (DOT) com[/email] (The Directive) wrote in message news:<8477bc58.0312301723.371d2ad2 (AT) posting (DOT) google.com>...
Quote:
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.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.