useful c++ assert macro snippets
December 31st, 2009
No comments
i just coded on this little bit, and i thought it might be worth to share the information as an example of how to roll your own assert, how to jump into the debugger and how to add log messages with proper calling information:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // this is the master swith to debug the stream locking/unlocking #define DEBUGSTREAMFACTORIES #define OGREFUNCTIONSTRING String(__FUNCTION__)+" @ "+String(__FILE__)+":"+StringConverter::toString(__LINE__) #ifdef DEBUGSTREAMFACTORIES # define LOCKSTREAMS() do { LogManager::getSingleton().logMessage("***LOCK: "+OGREFUNCTIONSTRING); lockStreams(); } while(0) # define UNLOCKSTREAMS() do { LogManager::getSingleton().logMessage("***UNLOCK: "+OGREFUNCTIONSTRING); unlockStreams(); } while(0) # ifdef WIN32 // __debugbreak will break into the debugger in visual studio # define MYASSERT(x) do { if(!x) { LogManager::getSingleton().logMessage("***ASSERT FAILED: "+OGREFUNCTIONSTRING); __debugbreak(); }; } while(0) # else //!WIN32 # define MYASSERT(x) assert(x) # endif //WIN32 #else //!DEBUGSTREAMFACTORIES # define LOCKSTREAMS() ((void)0) # define UNLOCKSTREAMS() ((void)0) # define MYASSERT(x) ((void)0) #endif //DEBUGSTREAMFACTORIES |
also, you might enjoy this very well written tips and tricks for asserts: http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/
btw, happy new year
Categories: coding