怎样用代码获取 crash 相关信息

如题所述

提供一段代码,能捕获大部分crash消息,不过有些还是没办法:
先将console中的crash信息写入文本,再在下次启动程序时,调用借口将crash信息传回服务器。实例代码如下:

- (void)redirectTNLogToDocumentFolder{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =@"TaoLan.log";
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSString *contents = [[NSString alloc] initWithContentsOfFile:logFilePath encoding:NSUTF8StringEncoding error:nil];
NSRange range1 = [contents rangeOfString:@"Terminating app due to uncaught exception"];
NSRange range2 = [contents rangeOfString:@"message sent to deallocated instance"];

if (range1.length > 0 || range2.length > 0) {
NSString *contents1 = [contents stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%c",'\n'] withString:@"<br />"];
if (contents1 && [contents1 length] > 10) {
NSString * bundleVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSString * displayName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
NSString *contents2 = [[NSString alloc] initWithFormat:@"BUG监听报告:<br />手机型号: %@ , 版本: %@ <br />程序名称: %@, 版本:%@<br /> 用户: %d<br /> %@", [[UIDevice currentDevice] systemName], [[UIDevice currentDevice] systemVersion], displayName, bundleVersion, taonanApi.currentUserId, contents1];
[taonanApi updatelog:contents2];
[contents2 release];
}
}
[contents release];
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"w",stderr);

}
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网