debug log

来源:互联网 发布:mac可以玩守望先锋吗 编辑:程序博客网 时间:2024/06/16 01:28

    程序的后台日志能让你迅速找到错误,用c预定义函数可以方便的打印log日志。

__DATE__            返回const char*,格式为:“Mmm dd yyyy”,如May 27 2006
__FILE__              返回const char*,为当前源代码文件名的 详细路径,如/vault/git/AOI/AOIFixture/CAOIFixture.mm
__LINE__              返回int,为当前源代码中的行号的整数常量
__TIME__             返回const char*,为源文件编译时间,格式为“hh:mm:ss”,如:09:11:10;
__FUNCTION__   返回const char*,为当前所在的函数名

__func__               同__FUNCTION__,在编译器的较高版本中支持

<span style="font-size:18px;">SaveDebug(logmsg,__func__,__LINE__);在关键位置调用函数即可打印log日志,十分方便。void SaveDebug(char *msg,const char *str,int line){    //NSLog([NSString stringWithFormat:@"%s\n",msg]);    printf("%s\n",msg);    NSDate *date=[NSDate date];    NSCalendar *cal=[NSCalendar currentCalendar];    unsigned int unitflags=NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;    NSDateComponents *d=[cal components:unitflags fromDate:date];    int year=[d year];    int month=[d month];    int day=[d day];    int hour=[d hour];    int minute=[d minute];    int second=[d second];    char tmp[strlen(msg)+50];    char filepath[255];    sprintf(tmp, "%02d.%02d:%02d %s:%d %s\n",hour,minute,second,str,line,msg);    //sprintf(tmp,"%s %s %s~%d %s\n",__DATE__,__TIME__,str,line,msg);    sprintf(filepath, "/vault/AOI_HALCON/debugfile.%d.%02d.%02d.txt",year,month,day);    //sprintf(filepath,"/vault/AOI_HALCON/debugfile_%s.txt</span><span style="font-size:18px;">",__DATE__);    ofstream dbf(filepath,ios_base::out|ios_base::app);    dbf.write(tmp, strlen(tmp));    dbf.close();}</span>


0 0