Assertion C - Packaging assert

C code often error handling. Call assert, the system comes assert output unity. So we need to own a packing assert, to the output you want more information about our

 

#include <stdio.h>
//#undef  _EXAM_ASSERT_TEST_    //禁用
#define  _EXAM_ASSERT_TEST_   //启用
#ifdef _EXAM_ASSERT_TEST_     //启用断言测试
	void assert_report( const char * file_name, const char * function_name, unsigned int line_no )
	{
		printf( "\n[EXAM]Error Report file_name: %s, function_name: %s, line %u\n", 
			 file_name, function_name, line_no );
		abort();
	}
	#define  ASSERT_REPORT( condition )       \
	do{       \
	if ( condition )       \
	NULL;        \
	else         \
	assert_report( __FILE__, __func__, __LINE__ ); \
	}while(0)
#else // 禁用断言测试 
	#define ASSERT_REPORT( condition )  NULL 
#endif /* end of ASSERT */

do {} while (0); statement can effectively prevent the impact ASSERT_REPORT macro call for the logical structure itself. The following code

#ifdef _EXAM_ASSERT_TEST_     //启用断言测试
	void assert_report( const char * file_name, const char * function_name, unsigned int line_no )
	{
		printf( "\n[EXAM]Error Report file_name: %s, function_name: %s, line %u\n", 
			 file_name, function_name, line_no );
		abort();
	}
	#define  ASSERT_REPORT( condition )       \
	if ( condition )       \
	NULL;        \
	else         \
	assert_report( __FILE__, __func__, __LINE__ );
#else // 禁用断言测试 
	#define ASSERT_REPORT( condition )  NULL 
#endif /* end of ASSERT */


#define printf_define()\
	printf("one line!\n");\
	printf("two line!\n");


int main(int argc, char *argv[])
{
	int i = 0;
	
	if(1 == i)
		printf_define();
		
		|
		拆开宏带入
		|
		
	if(1 == i)
		printf("one line!\n");
	printf("two line!\n");
	
	
}

To see what the problem? The ASSERT_REPORT into itself logical structure to chant destroyed. To execute only the printf ( "TWO Line \ n-!");
, {} The while DI (0); ensure complete piece of code is executed to.

 

Reference bigwigs experience

Published 22 original articles · won praise 9 · views 8823

Guess you like

Origin blog.csdn.net/ljm_c_bok/article/details/88783695