断言是一个用于发现程序错误的强大工具, 只可惜在手机开发中,打真机后断言由于无法定位而失去其强大的作用, 为方便调试特写了一份可用于真机,模拟器上的断言工具
代码如下:
/*
* assert_debug.h
*
* Created on: 2010-5-19
* Author: Huangdingwu
*
* 功能:
* 同时适用于真机与模拟器的断言debug工具
*
* 需要在mmp中加入:
*
* SYSTEMINCLUDE /epoc32/include/libc
SYSTEMINCLUDE /epoc32/include/libc/sys
LIBRARY estlib.lib
使用方法:
在需要使用断言的地方,请用 ASSERT_EX(X) 替换 ASSERT(x)
需要清除断言产生的内容时用:
ASSERT_Ex_CLEAR();
*/
#ifndef ASSERT_DEBUG_H_
#define ASSERT_DEBUG_H_
#include <libc/stdio.h>
#include <libc/string.h>
#define _DEBUG_OPEN_ 1
const char* const cLOG_FILE_PATH = "c://assert_log.txt";
void _hdw_PrintLog_(const char* aCodeFileName,const char* aCodeStr,int aLineNumber)
{
FILE* pFile = fopen(cLOG_FILE_PATH,"a+");
if(pFile != NULL)
{
fwrite("[filename:]",strlen("[filename:]"),1,pFile);
fwrite(aCodeFileName,strlen(aCodeFileName),1,pFile);
fwrite("/r",strlen("/r"),1,pFile);
fwrite("[code:]",strlen("[code:]"),1,pFile);
fwrite(aCodeStr,strlen(aCodeStr),1,pFile);
fwrite("/r",strlen("/r"),1,pFile);
char szNumber[32];
sprintf(szNumber,"[Line:]%05d/r/n",aLineNumber);
fwrite(szNumber,strlen(szNumber),1,pFile);
fclose(pFile);
}
}
void _hdw_Clear_()
{
FILE* pFile = fopen(cLOG_FILE_PATH,"w+");
fclose(pFile);
}
#ifdef _DEBUG_OPEN_
#define ASSERT_EX(x) /
if(!(x))/
{/
_hdw_PrintLog_(__FILE__,#x,__LINE__);/
User::Invariant();/
}/
#else
#define ASSERT_EX(x)
#endif
#define ASSERT_Ex_CLEAR() _hdw_Clear_()
#endif /* ASSERT_DEBUG_H_ */
使用的时候只要include头文件,并在需要使用断言的地方,请用 ASSERT_EX(X) 替换 ASSERT(x)
mmp里加上相关支持, 这样程序只要一报断言错误就可以去打开 c://assert_log.txt ,找到程序的错误了
在6110c上测试通过