iOS中捕获日志与异常示例详解
前言
在平时自己调试的时候,可以直接连接电脑,直接在窗口中查看结果。但是在测试人员测试,或者灰度测试的时候,怎么才能拿到日志呢?最先想到的肯定是输出到本地文件,然后在需要的时候进行上传。
分享一段之前找到的方法,下面的代码提供了两个主要功能:
–把日志输出到文件中
–捕捉异常信息
【解析都写在注释中了】
示例代码
-(void)redirectNSLogToDocumentFolder
{
//如果已经连接Xcode调试则不输出到文件
//该函数用于检测输出(STDOUT_FILENO)是否重定向是个Linux程序方法
if(isatty(STDOUT_FILENO)){
return;
}
//判断当前是否在模拟器环境下在模拟器不保存到文件中
UIDevice*device=[UIDevicecurrentDevice];
if([[devicemodel]hasSuffix:@"Simulator"]){
return;
}
//将NSlog打印信息保存到Document目录下的Log文件夹下
NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*logDirectory=[[pathsobjectAtIndex:0]stringByAppendingPathComponent:@"Log"];
NSFileManager*fileManager=[NSFileManagerdefaultManager];
BOOLfileExists=[fileManagerfileExistsAtPath:logDirectory];
if(!fileExists){
[fileManagercreateDirectoryAtPath:logDirectorywithIntermediateDirectories:YESattributes:nilerror:nil];
}
NSDateFormatter*formatter=[[NSDateFormatteralloc]init];
[formattersetLocale:[[NSLocalealloc]initWithLocaleIdentifier:@"zh_CN"]];
[formattersetDateFormat:@"yyyy-MM-ddHH:mm:ss"];//每次启动后都保存一个新的日志文件中
NSString*dateStr=[formatterstringFromDate:[NSDatedate]];
NSString*logFilePath=[logDirectorystringByAppendingFormat:@"/%@.log",dateStr];
//将log输入到文件
freopen([logFilePathcStringUsingEncoding:NSUTF8StringEncoding],"a+",stdout);
freopen([logFilePathcStringUsingEncoding:NSUTF8StringEncoding],"a+",stderr);
//未捕获的Objective-C异常日志
NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
}
之前看的时候,对NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler)这个用法一知半解,去翻了一下源码,这个方法是在Foundation中。
api中的定义是Changesthetop-levelerrorhandler,Setsthetop-levelerror-handlingfunctionwhereyoucanperformlast-minuteloggingbeforetheprogramterminates.通过替换掉最高级别的handle方法,可以在程序终止之前可以获取到崩溃信息,并执行相应的操作,比如保存本地,或者上报。
方法调用为:
voidNSSetUncaughtExceptionHandler(NSUncaughtExceptionHandler*);
传入的是一个NSUncaughtExceptionHandler的指针。
typedefvoidNSUncaughtExceptionHandler(NSException*exception);
意思就是需要一个返回void并且参数为NSException*exception的函数指针。
你想要,那我就给你!
所以下面有个C语言的函数,你看这个写法和OC的声明也不一样。
voidUncaughtExceptionHandler(NSException*exception)
{
NSString*name=[exceptionname];
NSString*reason=[exceptionreason];
NSArray*symbols=[exceptioncallStackSymbols];//异常发生时的调用栈
NSMutableString*strSymbols=[[NSMutableStringalloc]init];//将调用栈拼成输出日志的字符串
for(NSString*iteminsymbols)
{
[strSymbolsappendString:item];
[strSymbolsappendString:@"\r\n"];
}
//将crash日志保存到Document目录下的Log文件夹下
NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*logDirectory=[[pathsobjectAtIndex:0]stringByAppendingPathComponent:@"Log"];
NSFileManager*fileManager=[NSFileManagerdefaultManager];
if(![fileManagerfileExistsAtPath:logDirectory]){
[fileManagercreateDirectoryAtPath:logDirectorywithIntermediateDirectories:YESattributes:nilerror:nil];
}
NSString*logFilePath=[logDirectorystringByAppendingPathComponent:@"UncaughtException.log"];
NSDateFormatter*formatter=[[NSDateFormatteralloc]init];
[formattersetLocale:[[NSLocalealloc]initWithLocaleIdentifier:@"zh_CN"]];
[formattersetDateFormat:@"yyyy-MM-ddHH:mm:ss"];
NSString*dateStr=[formatterstringFromDate:[NSDatedate]];
NSString*crashString=[NSStringstringWithFormat:@"<-%@->[UncaughtException]\r\nName:%@,Reason:%@\r\n[FeSymbolsStart]\r\n%@[FeSymbolsEnd]\r\n\r\n",dateStr,name,reason,strSymbols];
//把错误日志写到文件中
if(![fileManagerfileExistsAtPath:logFilePath]){
[crashStringwriteToFile:logFilePathatomically:YESencoding:NSUTF8StringEncodingerror:nil];
}else{
NSFileHandle*outFile=[NSFileHandlefileHandleForWritingAtPath:logFilePath];
[outFileseekToEndOfFile];
[outFilewriteData:[crashStringdataUsingEncoding:NSUTF8StringEncoding]];
[outFilecloseFile];
}
//把错误日志发送到邮箱
//NSString*urlStr=[NSStringstringWithFormat:@"mailto://XXXXX@126.com?subject=bug报告&body=感谢您的配合!
错误详情:
%@",crashString];
//NSURL*url=[NSURLURLWithString:[urlStrstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//[[UIApplicationsharedApplication]openURL:url];
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对各位iOS开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。