当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS中捕获日志与异常示例详解

iOS中捕获日志与异常示例详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

养鱼场水质变黑臭,爱丫爱丫吉他谱,2人转小黄飞

前言

在平时自己调试的时候,可以直接连接电脑,直接在窗口中查看结果。但是在测试人员测试,或者灰度测试的时候,怎么才能拿到日志呢?最先想到的肯定是输出到本地文件,然后在需要的时候进行上传。

分享一段之前找到的方法,下面的代码提供了两个主要功能:

     – 把日志输出到文件中

     – 捕捉异常信息

【解析都写在注释中了】

示例代码

- (void)redirectnslogtodocumentfolder
{
//如果已经连接xcode调试则不输出到文件
//该函数用于检测输出 (stdout_fileno) 是否重定向 是个 linux 程序方法
if(isatty(stdout_fileno)) {
return;
}

// 判断 当前是否在 模拟器环境 下 在模拟器不保存到文件中
uidevice *device = [uidevice currentdevice];
if([[device model] hassuffix:@"simulator"]){
return;
}

//将nslog打印信息保存到document目录下的log文件夹下
nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);
nsstring *logdirectory = [[paths objectatindex:0] stringbyappendingpathcomponent:@"log"];

nsfilemanager *filemanager = [nsfilemanager defaultmanager];
bool fileexists = [filemanager fileexistsatpath:logdirectory];
if (!fileexists) {
[filemanager createdirectoryatpath:logdirectory withintermediatedirectories:yes attributes:nil error:nil];
}

nsdateformatter *formatter = [[nsdateformatter alloc] init];
[formatter setlocale:[[nslocale alloc] initwithlocaleidentifier:@"zh_cn"]];
[formatter setdateformat:@"yyyy-mm-dd hh:mm:ss"]; //每次启动后都保存一个新的日志文件中
nsstring *datestr = [formatter stringfromdate:[nsdate date]];
nsstring *logfilepath = [logdirectory stringbyappendingformat:@"/%@.log",datestr];

// 将log输入到文件
freopen([logfilepath cstringusingencoding:nsutf8stringencoding], "a+", stdout);
freopen([logfilepath cstringusingencoding:nsutf8stringencoding], "a+", stderr);

//未捕获的objective-c异常日志
nssetuncaughtexceptionhandler (&uncaughtexceptionhandler);
}

之前看的时候,对 nssetuncaughtexceptionhandler(&uncaughtexceptionhandler) 这个用法一知半解,去翻了一下源码,这个方法是在 foundation 中。

api 中的定义是changes the top-level error handler ,sets the top-level error-handling function where you can perform last-minute logging before the program terminates. 通过替换掉最高级别的 handle 方法,可以在程序终止之前可以获取到崩溃信息,并执行相应的操作,比如保存本地,或者上报。

方法调用为:

void nssetuncaughtexceptionhandler(nsuncaughtexceptionhandler *);

传入的是一个 nsuncaughtexceptionhandler 的指针。

typedef void nsuncaughtexceptionhandler(nsexception *exception);

意思就是需要一个 返回 void 并且参数为 nsexception *exception 的函数指针。

你想要,那我就给你!

所以下面有个 c 语言的函数,你看这个写法和 oc 的声明也不一样。

void uncaughtexceptionhandler(nsexception* exception)
{
nsstring* name = [ exception name ];
nsstring* reason = [ exception reason ];
nsarray* symbols = [ exception callstacksymbols ]; // 异常发生时的调用栈
nsmutablestring* strsymbols = [ [ nsmutablestring alloc ] init ]; //将调用栈拼成输出日志的字符串
for ( nsstring* item in symbols )
{
[ strsymbols appendstring: item ];
[ strsymbols appendstring: @"\r\n" ];
}

//将crash日志保存到document目录下的log文件夹下
nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);
nsstring *logdirectory = [[paths objectatindex:0] stringbyappendingpathcomponent:@"log"];

nsfilemanager *filemanager = [nsfilemanager defaultmanager];
if (![filemanager fileexistsatpath:logdirectory]) {
[filemanager createdirectoryatpath:logdirectory withintermediatedirectories:yes attributes:nil error:nil];
}

nsstring *logfilepath = [logdirectory stringbyappendingpathcomponent:@"uncaughtexception.log"];
nsdateformatter *formatter = [[nsdateformatter alloc] init];
[formatter setlocale:[[nslocale alloc] initwithlocaleidentifier:@"zh_cn"]];
[formatter setdateformat:@"yyyy-mm-dd hh:mm:ss"];
nsstring *datestr = [formatter stringfromdate:[nsdate date]];

nsstring *crashstring = [nsstring stringwithformat:@"<- %@ ->[ uncaught exception ]\r\nname: %@, reason: %@\r\n[ fe symbols start ]\r\n%@[ fe symbols end ]\r\n\r\n", datestr, name, reason, strsymbols];
//把错误日志写到文件中
if (![filemanager fileexistsatpath:logfilepath]) {
[crashstring writetofile:logfilepath atomically:yes encoding:nsutf8stringencoding error:nil];
}else{
nsfilehandle *outfile = [nsfilehandle filehandleforwritingatpath:logfilepath];
[outfile seektoendoffile];
[outfile writedata:[crashstring datausingencoding:nsutf8stringencoding]];
[outfile closefile];
}

//把错误日志发送到邮箱
// nsstring *urlstr = [nsstring stringwithformat:@"mailto://xxxxx@126.com?subject=bug报告&body=感谢您的配合!<br><br><br>错误详情:<br>%@",crashstring ];
// nsurl *url = [nsurl urlwithstring:[urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];
// [[uiapplication sharedapplication] openurl:url];
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位ios开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网