一、assert基础
- 备注
1)断言若为错,程序直接core down
2)在release版本中自动隐藏,在debug版本中显示出来
二、嵌入信息
- 效果,发生错误会打印
prand: /home/lighthouse/core_code/srand_num/assert1.cpp:17:
void VectorNormalize(int32_t, int32_t):
Assertion `src != 0 && "VectorNormalize: src vector pointer is NULL"' failed.
Aborted (core dumped)
- 代码
#include <assert.h>
void VectorNormalize( int32_t src, int32_t dst )
{
float length;
assert( src != 0 && "VectorNormalize: src vector pointer is NULL" );
assert( dst != 0 && "VectorNormalize: dst vector pointer is NULL" );
}
三、嵌入更多信息
- 由于条件是保证语句是非0的话,用下面的代码
- 效果
prand:
/home/lighthouse/core_code/srand_num/assert1.cpp:17:
void VectorNormalize(int32_t, int32_t): Assertion `!"VectorNormalize: The code should never get here"' failed.
Aborted (core dumped)
- 代码
void VectorNormalize( int32_t src, int32_t dst )
{
float length;
assert( !"VectorNormalize: The code should never get here" );
assert( dst != 0 && "VectorNormalize: dst vector pointer is NULL" );
}
四、让其变得更好用
- 效果
[lighthouse@VM-4-14-centos build]$ ./prand
prand: /home/lighthouse/core_code/srand_num/main.cpp:15:
int main(int, char**): Assertion `src != 0 && "VectorNormalize: src vector pointer is NULL"' failed.
Aborted (core dumped)
- 代码
#define Assert(a,b) assert( a && b )
assert( src != 0, "VectorNormalize: src vector pointer is NULL" );
assert( 0, "VectorNormalize: The code should never get here" );
五、可增加总是忽略选项
-
作用
忽略某个函数的assert,我觉得没啥用,但是学到了 -
效果
-
代码
#include <assert.h>
#include <stdint.h>
#include <math.h>
#define NULL (0)
#define redis \
extern bool CustomAssertFunction( bool, char*, int, char* );
#define Assert( exp, description ) \
if( CustomAssertFunction( (int)(exp), description, __LINE__, __FILE__ ) ) \
{ _asm { int 3 } } //this will cause the debugger to break here on PC's
bool CustomAssertFunction( bool, char*, int, char* );
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Prandgen.h"
#include "assert1.h"
using namespace std;
int main (int argc, char * argv[])
{
int32_t src = 0;
redis(true,"VectorNormalize: src vector pointer is NULL",4,"VectorNormalize: src vector pointer is NULL");
printf("3\n");
return 0;
}
- 额外补充
#if defined( _DEBUG )
extern bool CustomAssertFunction( bool, char*, int, char*, bool* );
#define Assert( exp, description ) \
{ static bool ignoreAlways = false; \
if( !ignoreAlways ) { \
if( CustomAssertFunction( (int)(exp), description, \
__LINE__, __FILE__, &ignoreAlways ) ) ) \
{ _asm { int 3 } } \
} \
}
#else
#define Assert( exp, description )
#endif
六、提供堆栈信息便于调试
#define ERR_F_F(__condition__) \
do \
{ \
ERR("LOG_PROCESS_ERROR ("#__condition__") in function %s()",__FUNCTION__);\
} while (0);
#define ANGLE_ASSERT(sendtence) \
do \
{ \
ERR_F_F(sendtence) \
ERR("stack trace info:%s ",stack_trace_info());\
assert(sendtence); \
} while (0)
#define assert_retnone(sendtence) \
do \
{ \
if(sendtence) break; \
ANGLE_ASSERT(sendtence); \
return; \
} while (0)
#define assert_retnone(sendtence,val) \
do \
{ \
if(sendtence) break; \
ANGLE_ASSERT(sendtence); \
return (val); \
} while (0)