473,324 Members | 2,248 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,324 software developers and data experts.

debug mode

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
6 3638
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
"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
"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
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
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

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike C. Fletcher | last post by:
Every few months I get to working on a Python C extension (built with distutils) and discover a pointer error or the like where I'd like to be able to step into my DLL and see where the extension...
3
by: Max M. Power | last post by:
How can I tell if my C# code is running in debug mode? I need to execute some extra code only when my project has been compiled in debug mode. In release mode I don't want to execute this extra...
12
by: nospam | last post by:
All the documentation says that leaving an ASP.NET application in debug mode has a big performance hit. I can't detect any difference between debug and non-debug modes. Am I missing something or is...
7
by: Srinivasa Rao | last post by:
I have read in one article that when we compile the application in release mode, all the debug classes and properties will be automatically removed from the code. I tried to implement this thing by...
3
by: Rena | last post by:
Hi all, I have created a app. project and a library which will be used by the main project, however i do not know how to get into debug mode inside the library, although break point is set. is...
3
by: Haldun ALIML | last post by:
Suppose that you have below property in some class, #if DEBUG public string DebugInfo { get { return "INDEX : " + _name + "\n" + "Index Owner : " + _owner.Name + "\n" + "Index Column Count:...
2
by: Dave Johansen | last post by:
I just converted a solution from Visual Studio 2003 to Visual Studio 2005 and the Debug mode seems to be running just fine, but the Release mode crashes on the following code: std::ifstream...
0
by: =?Utf-8?B?SmVmLnB0Yw==?= | last post by:
Hi, I am currently facing exactly the same issue with Visual Studio 2005. Did you find a way to solve this problem ? "AntonioSACE" wrote:
3
by: =?Utf-8?B?bG10dGFn?= | last post by:
We have developed a number of different applications (ASP.NET web site, Windows services, DLLs, Windows forms, etc.) in C# 2.0. We have developed and unit tested all these applications/components...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: 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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.