断言仅仅是判断一些预制的错误,对于系统开发的调试会有一定的作用。
// debug.h
#ifndef __DEBUG_H
#define __DEBUG_H
void panic_(char* filename, int line, const char* func, const char* condition);
#define PANIC(...) panic_(__FILE__, __LINE__, __func__, __VA_ARGS__)
#ifdef NDEBUG
#define ASSERT(CONDITION) ((void)0)
#else
#define ASSERT(CONDITION) if(CONDITION){}else{PANIC(#CONDITION);}
#endif
#endif
// debug.c
#include "string.h"
#include "debug.h"
#include "intr_status_op.h"
void panic_(char* filename, int line, const char* func, const char* condition) {
intr_disable();
printf_("\n#########DEBUG ERROR#########\n");
printf_("Filename = %s\n", filename);
printf_("Line = %d\n", line);
printf_("Function = %s\n", func);
printf_("Condition = %s\n", condition);
while(1);
}