知識小計:
1、使用宏對錯誤進行處理
#define CHECK(LINE, EXPECTED)
{
int rc =LINE;
if (rc != EXPECTED)
ut_abort(__FILE__,__LINE__,#LINE,rc,EXPECTED);
}
void ut_abort(char *file, int ln, char *line, int rc,int exp)
{
fprintf(stderr,"%s line %d \n '%s' :expected %d, got %d \n", file,ln,line,exp,rc);
exit(1);
}
使用
CHECK(stat("/tmp", &stat_buff), 0);
如果失敗了,則會得到寫到stderr 的消息:
source.c line 19
'stat("/tmp",&stat_buff)':expected 0 ,got -1