#### Archived

This topic is now archived and is closed to further replies.

# any neater methods than #ifdef _DEBUG?

This topic is 6025 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there any neater way to output to a log file than this:

if(sometest)
{
if(anothertets)
{
#ifdef _DEBUG
// report to log file

m_LogFile << "PROCESS: Some Error Message\n";
#endif
}
}

It works fine outputing error and debug info to a log file the only problem I have with it is VC++ 6 is automatically aligning any # command to the far left of the editor window, which is ok in small functions but in any complex function that uses a switch, while or nested if statements it is making the code very hard to read.... Any suggestions as to a to make it easir to read? Cheers, Convict@Large "I code therefore I am" Anon [edited by - convict@large on April 17, 2002 10:23:21 AM]

##### Share on other sites
Have a look to the TRACE macro in MFC. You could use a similar technique:
//debug.h//...#ifdef _DEBUG#define TRACE _trace#elseinline void _trace(LPCTSTR fmt, ...) { }#define TRACE  1 ? (void)0 : _trace#endif//...//debug.cppvoid _trace(char *fmt, ...){char out[1024];	va_list body;	va_start(body, fmt);	vsprintf(out, fmt, body);	va_end(body);	OutputDebugString(out);}

Than just use TRACE() in your code. The above example prints to the active debugger (or to Nirvana, if there isn''t one active ), but you could use the technique to print to almost any target.

Forever trusting who we are
And nothing else matters
- Metallica

##### Share on other sites
Any chance you could explain the debug.h code a bit more? I get some of it but some escapes me. I've commented it to try to figure it out:

//debug.h//...#ifdef _DEBUG          #define TRACE _trace  /* Defines TRACE = _trace if _DEBUG is defined? */#elseinline void _trace(LPCTSTR fmt, ...) { } /* function defined but with no implementation? */#define TRACE  1 ? (void)0 : _trace /* If TRACE = (void)0 as 1 = TRUE? */#endif//...//debug.cpp/*definition & implemetation of function trace but has different parameters?*/

It the bit in debug.h that is confusing me the most if you could explain it a bit more (or just correct my statments) that would be cool.

Cheers,

Convict@Large

"I code therefore I am" Anon

[edited by - convict@large on April 17, 2002 10:46:50 AM]

##### Share on other sites
Oh, sorry, I didn't meant to be cryptical
Here we go:
#ifdef _DEBUG// prototype the _trace() function (see below whyvoid _trace(char *fmt, ...);// if in debug mode TRACE will expand to _trace#define TRACE _trace#else// else define the trace function as being empty (note the {} )inline void _trace(LPCTSTR fmt, ...) { }// and make TRACE expand to this funny looking thingie#define TRACE  1 ? (void)0 : _trace#endif// somwhere in the code// this is why we prototipe traceTRACE("blah\n");//will expand in debug mode to_trace("blah\n");//and in release mode to1 ? (void)0:_trace("blah");// wich, since the release version of "_trace" is inline will be (quasi)1 ? (void)0:{};// wich will be recognized by the compiler as an instruction with no effect, and will not generate any machine instruction

 I forgot to say why we prototype _trace()

Forever trusting who we are
And nothing else matters
- Metallica

[edited by - Gabriel Fleseriu on April 17, 2002 10:57:11 AM]

##### Share on other sites
Ah I get it now! Very cool bit of code that :D Thanks for the explantation!

Cheers,

Convict@Large

"I code therefore I am" Anon

1. 1
2. 2
3. 3
4. 4
5. 5

• 14
• 9
• 9
• 10
• 12
• ### Forum Statistics

• Total Topics
632911
• Total Posts
3009184
• ### Who's Online (See full list)

There are no registered users currently online

×