Click to See Complete Forum and Search --> : Debugging


John_Sheridan
October 15th, 2001, 02:15 AM
Hallo!

I want to debug my programm (e.g. a C++ DLL) by using my own CDebugOut class which has overloaded >> operators (like std::cout does) to output differnt types of variables and add a timestamp.

In the file stdafx.h I have defined following defines:


#ifndef _DEBUG_RELEASE
#define _DEBUG_RELEASE

#include "DebugOut.h"

extern class STSRV::CDebugOut *oLog;

#define _STSRV_DEBUG_OUT (*oLog)
#define _STSRV_DEBUG_OUT_ENDL \
"\n"; oLog->print();

// *** Ausgabe hex/dez bei
#define _COUT_DEC (*oLog).dec();
#define _COUT_HEX (*oLog).hex();

#endif // _DEBUG_RELEASE





In the code I am using the code like this:

_STSRV_DEBUG_OUT << "Variable x = " << x << _STSRV_DEBUG_OUT_ENDL;



These defines has the advantage that the define _STSRV_DEBUG_OUT or _STSRV_DEBUG_OUT_ENDL can be defined as std::cout or std::endl or even as std::ofstream or what ever you like.

The problem is following: This defines should only be available in Debugmode. I tried to declare the define _STSRV_DEBUG_OUT as following:


#define _STSRV_DEBUG_OUT \
#ifdef _DEBUG \
(*oLog)

#define _STSRV_DEBUG_OUT_ENDL \
"\n"; oLog->print(); } \
#endif // _DEBUG





But I get compiler error if I use a second precompiler directive in a macro or define.
Is it not allowed to use a second precompiler command in a macro?

The compiler errors are following:
error C2121: '#' : invalid character : possibly the result of a macro expansion
error C2065: 'ifdef' : undeclared identifier
....

Thanx for your help!

easye21
October 16th, 2001, 12:14 PM
I don't know exactly what you're trying to do with all those precompile headers and you have one
_DEBUG already in there.But I think you could do this :
#ifdef _DEBUG
[put all things exclusive
to Debug build here.]
#endif
In the first example I see no #ifdef _DEBUG
and in second example I think you're using the
"\" incorrectly. It should only be used for a continuation of a precompile header and not for beginning another.
If any problems still arise then there's a error in what youre actually defining for the debug build and not the #ifdef _DEBUG macro itself.
Hope this helps.

John_Sheridan
October 17th, 2001, 02:04 AM
Hallo!

I want to debug my program in following way:

int f()
{
int x = f2();
// I want to debug the variable x in that way:
_DEBUG_OUT << "Variable x from f2() = " << x << DEBOUG_OUT_ENDL;

// and not in that way: (to make less lines)
#ifdef _DEBUG
_DEBUG_OUT << "Variable x from f2() = " << x << DEBOUG_OUT_ENDL;
#endif // _DEBUG

}




If I would declare my own debug class only in the predefined _DEBUG command I would get a compiler error in the release mode because the overloaded operator >> is not defined.

The sign \ stands for a newline in a macro or define, so you can write a macro or define in more lines.

e.g.:

#define MACRO(str) \
if (str == "") \
return _NO_ERROR \
else \
return _ERROR // The last line has no \




With friendly greetings, John Sheridan