Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++: Simple debug output wrapper

Started by Infinite_Daremo Sep 7, 2010 at 3:48 PM 8 replies 2.2k views
Original Post
Infinite_Daremo
Infinite_Daremo
Im looking for a simple ( or not so simple, either will do ) solution to the following.

Currently in debug i have a function define for logging output

#ifdef _DEBUGvoid Log(const char *pText){    printf(pText);#ifdef _WINDOWS.... // and a fair few others}#endif


Now to use this i have to wrap it up like so:

#ifdef _DEBUG
Log("harrow world");
#endif

Just wondering if there is a way to just call: Log() and the compiler would know to skip this in a release build? Im using VS2008 if theres a clever trick for that. I plan to extend it into a class later so not having the class initialised in release would be beneficial too.

I know its being completely anal but i hate duplicating that over and over. Am i right to assume that some compilers if they see a function body is empty they will remove the function call completely? I don't really want to rely on the compiler for this though.

fastcall22
fastcall22
#ifdef _DEBUGvoid Log(const char *pText){    printf(pText);}#elsevoid Log( const char* ) {}#endif


Or:

#ifdef _DEBUG#define Log(x)    Log_impl(x)#else#define Log(x)#endifvoid Log_impl( const char* pText ) {    // ...}
rip-off
rip-off
Research how assert() macros are implemented.
rip-off
rip-off
Quote:
Original post by Infinite_Daremo
Lol. So all compilers will compile this out?


If the function body is inlined in a header, then any reasonable compiler will. Some compilers may be able to optimise it if it is implemented in a source file.
Infinite_Daremo
Infinite_Daremo
Yeh that similar to what i wanted.I just went the route of a blank definition of release. Seems to work, cant possibly test it though :)
the_edd
the_edd
Quote:
Original post by Infinite_Daremo
Yeh that similar to what i wanted.I just went the route of a blank definition of release. Seems to work, cant possibly test it though :)


You can if you tell your compiler to save the intermediate assembly code. If there aren't any instances of calls to Log_impl, then you know it's been omitted.
The Communist Duck
The Communist Duck
I currently do:
#ifndef NDEBUG#define LOG(x) std::cout << "On line " << __LINE__ << " in function " << __FUNC__ << "," << x << std::endl#else#define LOG(x)#endif

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.