debug mode

P

pauldepstein

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
 
V

Victor Bazarov

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
 
J

John Carson

Victor Bazarov said:
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;
}
 
J

John Carson

Victor Bazarov said:
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;
}
 
J

Jacek Dziedzic

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

Default User

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
 
J

Jim Langston

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::eek:fstream LogDebugFile("DebugLog.log");
if (LogDebugFile.is_open())
{
LogDebugFile << Message << std::endl;
}
}
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top