Assert

Started by
13 comments, last by tuxx 21 years, 9 months ago
(And pardon the shameless plug, but you could also check out my article on C++ debugging for some useful tips on when to use assertions, return values, and exceptions.)
Advertisement
Expirement with __LINE__ and __FILE__ (if your compiler supports them) to create perhaps a log file system... You wanted to know more specific where the error took place, so there you go...

Although don''t use __LINE__ and __FILE__ within the log function, use it perhaps as a parameter in your log output function... then you can always make a macro that automatically includes them.

/G
---GUI Programming Division Manager at Wildfire Gamesworking on the 0 A.D. project
Thanks. I agree, Gee, with you, so I think I''ll do my own asserts. Two types of asserts, in fact: one your standard assert with the addition of the functionality of verify. The second, pretty much a macro for if(FAILED(...()))
[email=dumass@poppet.com]dumass@poppet.com[/email]
7I wrote a TESTHR macro to cook hresults.
It's not a bad idea to assert on a HRESULT failure, but you also want to propegate the failure up the call chain. If it's a show-stopper, you could throw an exception if you wanted to (you can throw your own exception object, and put whatever you want in it, such as the line & file).

If you do make a macro that always executes it's parameter - don't call it assert, assert's de facto behavior is to only execute the parameter in a debug build.


  void __cdecl HR2String(HRESULT hr, char* szHR);#ifdef _DEBUG 	#define TESTHR(Test_) 	if(S_OK!=(hr=Test_) && (S_FALSE!=Test_)) 		{		char szError[256];		HR2String(hr, szError);		if(FAILED(hr))			{			sprintf(&szError[strlen(szError)], " 0x%x\n%s\nOn line %i in %s\n\n\0", hr, #Test_, __LINE__, __FILE__);			assert(strlen(szError)<sizeof(szError));			OutputDebugString("*****************\n");			OutputDebugString(szError);			return(hr);			}		else			{			sprintf(&szError[strlen(szError)], " 0x%x\n%s\nOn line %i in %s\n\n\0", hr, #Test_, __LINE__, __FILE__);			assert(strlen(szError)<sizeof(szError));			OutputDebugString("*****************\n");			OutputDebugString(szError);			}		}#else 	#define TESTHR(Test_) if(FAILED(hr=Test_)) return(hr); #endif //.cpp#define HR2StringCase(Case_)\ 	case Case_:		strcpy(szHR, #Case_);	break;#define HR2StringCase_(HR_, Msg_)	case HR_:		strcpy(szHR, #Msg_);	break;void __cdecl HR2String(HRESULT hr, char* szHR)	{	switch(hr)		{		HR2StringCase(S_OK)		HR2StringCase(S_FALSE)		//on & on & on ...				default:			{			if(hr>0)				{				FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, hr, 0, szHR, 80, 0);				}			else				{				strcpy(szHR, "Unknown Failure: 0x");				itoa(hr, &szHR[strlen(szHR)], 16);				strcpy(&szHR[strlen(szHR)], " (");				itoa(hr, &szHR[strlen(szHR)], 10);				strcpy(&szHR[strlen(szHR)], ")");				}			}		}  

Note that the line continuation characters have been removed because the board interprets them...

[edited by - Magmai Kai Holmlor on July 18, 2002 9:25:19 PM]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Slightly off-topic, but I recently discovered that the line continuation character also works on C++ comments. Too cool

This topic is closed to new replies.

Advertisement