473,320 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Debugging macros

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.)
Jul 22 '05 #1
5 3662

"The Directive" <th***********@hotmail.com> wrote in message
news:84**************************@posting.google.c om...
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 <iostream>
#define TRACE(arg) (std::cout << arg << std::endl)
#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.
Jul 22 '05 #2
[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.
Jul 22 '05 #3

"The Directive" <th***********@hotmail.com> wrote in message news:84**************************@posting.google.c om...
[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?
Jul 22 '05 #4
> 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/Guima...asp#xx701517xx
Jul 22 '05 #5
th***********@hotmail.com (The Directive) wrote in message news:<84**************************@posting.google. com>...
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.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
1
by: Rennie deGraaf | last post by:
I want to use throw() specifiers on some of my methods while debugging, to help me keep track of what is going on, but remove them in release builds. My method of doing this is #ifdef DEBUG...
9
by: kbc | last post by:
Hi, I am programming in C. ( You know that __FILE__ is a 'macro' which may be used to print debug messages. ) 3 questions : a) I have 10000 functions in my project. I am...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
28
by: Martin Jørgensen | last post by:
Hi, I have a "funny" question, which I think is pretty "healthy" to examine... This program is being investigated: - - - - - - - #include <iostream> using namespace std; #define DAYS 7
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
15
by: Giff | last post by:
Hi again, I need a suggestion. Right now I am developing an application and I want to be able to output on screen some variables. Anyway, I want to remove these output operations when passing...
16
by: Prayag Narula | last post by:
Hi, I want to redefine fprintf for debugging purposes. That is I want that all the output that is going to the stdout should be logged in a file. I tried something like #define fprintf...
33
by: fmassei | last post by:
Hello! I made a short piece of code that I find very useful for debugging, and I wanted to ask you if it is correct, somehow acceptable or if I simply reinvented the wheel. To deal with some bad...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.