By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

debug mode

P: n/a
To help me debug, I am writing a lot of information into a stream which
I call debug.

However, because of the large amount of time taken to print this
information, I only want this printed while I am in debugging mode as
indicated by a preprocessor directive:

#define DEBUG

In other words, I need code which says "Whenever I write debug <<
...... I only want that command executed if I am in debug mode. "

In other words, I want code that always interprets statements of the
form:

debug << x << endl;

as #ifdef DEBUG debug << x << endl; (without typing #ifdef DEBUG
each time.)

So how do I do this? Is there some command that says "Open a stream
for debug mode only?"
Thank you,

Paul Epstein

Jan 19 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
pa**********@att.net wrote:
To help me debug, I am writing a lot of information into a stream
which I call debug.

However, because of the large amount of time taken to print this
information, I only want this printed while I am in debugging mode as
indicated by a preprocessor directive:

#define DEBUG

In other words, I need code which says "Whenever I write debug <<
..... I only want that command executed if I am in debug mode. "

In other words, I want code that always interprets statements of the
form:

debug << x << endl;

as #ifdef DEBUG debug << x << endl; (without typing #ifdef DEBUG
each time.)

So how do I do this? Is there some command that says "Open a stream
for debug mode only?"


You could define your 'debug' differently depending on whether the
DEBUG macro is defined or not. Something like

#ifdef DEBUG
myostream debug; // line 133
#else
nullstream debug;
#endif

I don't know what your 'debug' is in reality, that's why on line 133
'myostream' is used (I don't know what to use, you do). As to the
other, non-DEBUG portion, look "nullstream" on the Web. There is
probably a way to define a stream that simply eats everything without
any side effect.

V
Jan 19 '06 #2

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Q6********************@comcast.com
pa**********@att.net wrote:
To help me debug, I am writing a lot of information into a stream
which I call debug.

However, because of the large amount of time taken to print this
information, I only want this printed while I am in debugging mode
as indicated by a preprocessor directive:

#define DEBUG

In other words, I need code which says "Whenever I write debug <<
..... I only want that command executed if I am in debug mode. "

In other words, I want code that always interprets statements of the
form:

debug << x << endl;

as #ifdef DEBUG debug << x << endl; (without typing #ifdef DEBUG
each time.)

So how do I do this? Is there some command that says "Open a stream
for debug mode only?"


You could define your 'debug' differently depending on whether the
DEBUG macro is defined or not. Something like

#ifdef DEBUG
myostream debug; // line 133
#else
nullstream debug;
#endif

I don't know what your 'debug' is in reality, that's why on line 133
'myostream' is used (I don't know what to use, you do). As to the
other, non-DEBUG portion, look "nullstream" on the Web. There is
probably a way to define a stream that simply eats everything without
any side effect.

V


How about this?

#include <string>

struct nullstream
{
template<class T>
nullstream & operator<<(const T& t)
{
return *this;
}
};
int main()
{
nullstream ns;

ns << 10 << 5.076 << "string literal" << std::string("std::string
temp") << '\n';

return 0;
}
--
John Carson
Jan 19 '06 #3

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Q6********************@comcast.com
pa**********@att.net wrote:
To help me debug, I am writing a lot of information into a stream
which I call debug.

However, because of the large amount of time taken to print this
information, I only want this printed while I am in debugging mode
as indicated by a preprocessor directive:

#define DEBUG

In other words, I need code which says "Whenever I write debug <<
..... I only want that command executed if I am in debug mode. "

In other words, I want code that always interprets statements of the
form:

debug << x << endl;

as #ifdef DEBUG debug << x << endl; (without typing #ifdef DEBUG
each time.)

So how do I do this? Is there some command that says "Open a stream
for debug mode only?"


You could define your 'debug' differently depending on whether the
DEBUG macro is defined or not. Something like

#ifdef DEBUG
myostream debug; // line 133
#else
nullstream debug;
#endif

I don't know what your 'debug' is in reality, that's why on line 133
'myostream' is used (I don't know what to use, you do). As to the
other, non-DEBUG portion, look "nullstream" on the Web. There is
probably a way to define a stream that simply eats everything without
any side effect.

V


To have zero runtime cost in release mode, an alternative (at the cost of
some extra typing) is:

#ifdef _DEBUG
#define D(x) x
#else
#define D(x)
#endif

int main()
{
int x = 5;
D(debug << x << endl;)
return 0;
}
--
John Carson
Jan 19 '06 #4

P: n/a
pa**********@att.net wrote:
To help me debug, I am writing a lot of information into a stream which
I call debug.

However, because of the large amount of time taken to print this
information, I only want this printed while I am in debugging mode as
indicated by a preprocessor directive:

#define DEBUG

In other words, I need code which says "Whenever I write debug <<
..... I only want that command executed if I am in debug mode. "

In other words, I want code that always interprets statements of the
form:

debug << x << endl;

as #ifdef DEBUG debug << x << endl; (without typing #ifdef DEBUG
each time.)


A quick and imperfect solution is to

#ifdef DEBUG
#define debug (std::cerr)
#else
#define debug if(0) (std::cerr)
#endif

// ...
debug << "This will only be printed if DEBUG is defined".

HTH,
- J.
Jan 19 '06 #5

P: n/a
pa**********@att.net wrote:
To help me debug, I am writing a lot of information into a stream
which I call debug.

However, because of the large amount of time taken to print this
information, I only want this printed while I am in debugging mode as
indicated by a preprocessor directive:

#define DEBUG

In other words, I need code which says "Whenever I write debug <<
..... I only want that command executed if I am in debug mode. "

In other words, I want code that always interprets statements of the
form:

debug << x << endl;

as #ifdef DEBUG debug << x << endl; (without typing #ifdef DEBUG
each time.)

So how do I do this? Is there some command that says "Open a stream
for debug mode only?"

One common way is to create a function-like macro that can be switched
on and off.

#ifdef DEBUG
#define DEBUG_REPORT(x) \
do {debug << x << endl} while (0)
#else
#define DEBUG_REPORT(x)
#endif
The do {} while(0) is a method to create a block on the fly that will
be terminated with a ; so you could have multiple statements within it
if you wanted.
It's used like this:
DEBUG_REPORT(value);
When you're not in debug, the macro is a no-op.
Brian
Jan 19 '06 #6

P: n/a

<pa**********@att.net> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
To help me debug, I am writing a lot of information into a stream which
I call debug.

However, because of the large amount of time taken to print this
information, I only want this printed while I am in debugging mode as
indicated by a preprocessor directive:

#define DEBUG

In other words, I need code which says "Whenever I write debug <<
..... I only want that command executed if I am in debug mode. "

In other words, I want code that always interprets statements of the
form:

debug << x << endl;

as #ifdef DEBUG debug << x << endl; (without typing #ifdef DEBUG
each time.)

So how do I do this? Is there some command that says "Open a stream
for debug mode only?"
Thank you,

Paul Epstein


This is how I handle it, although I have the extra overhead of a function
call.

void LogDebug( const std::string& Message )
{
#ifndef LOGDEBUG
return;
#endif
static std::ofstream LogDebugFile("DebugLog.log");
if (LogDebugFile.is_open())
{
LogDebugFile << Message << std::endl;
}
}
Jan 22 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.