在app发布到商店之后,用户的crash报告怎么才能收集回来? 根据苹果官方的文档,当设备连接上itunes后,crash报告会自动同步至pc,但是用户可能很少这么做,即使有,可能用户也找不到,用户找到了,也可能不想麻烦再传给你。这时候,我们改怎么办?
其实,我们可以自己做一个错误收集模块。不过这个收集要分成两个部分:1、NSException 2、非NSException致命错误(如提前释放内存、内存超最高限制)
第一部分,NSException的收集。采用自定义exception处理函数,替换系统处理函数来取得exception发生时的具体错误信息、堆栈状态。
自定义exception处理函数:
void exceptionHandler(NSException *exception) {
NSLog(@"Exception throw: %@", exception);
NSLog(@"Stack Trace:\r\n%@", [exception callStackSymbols]);
//NSLog(@"Stack Trace:\r\n%@", GTMStackTraceFromException(exception));
}
然后调用
NSSetUncaughtExceptionHandler(&exceptionHandler);
来替换系统得exception处理函数.
第二部分:signal的处理
自定义系统收到signal时得处理函数:
void stacktrace(int sig, siginfo_t *info, void *context)
{
NSMutableString * mstr = [[NSMutableString alloc] initWithCapacity:0];
[mstr appendString:@"Stack:\n"];
void* callstack[128];
int i, frameCount = backtrace(callstack, 128);
char** strs = backtrace_symbols(callstack, frameCount);
for (i = 0; i < frameCount; ++i) {
[mstr appendFormat:@"%s\n", strs[i]];
}
NSLog(@"Stack Trace:\r\n%@", mstr);
}
注册系统要监测的signal:
struct sigaction mySigAction;
mySigAction.sa_sigaction = stacktrace;
mySigAction.sa_flags = SA_SIGINFO;
sigemptyset(&mySigAction.sa_mask);
sigaction(SIGQUIT, &mySigAction, NULL);
sigaction(SIGILL , &mySigAction, NULL);
sigaction(SIGTRAP, &mySigAction, NULL);
sigaction(SIGABRT, &mySigAction, NULL);
sigaction(SIGEMT , &mySigAction, NULL);
sigaction(SIGFPE , &mySigAction, NULL);
sigaction(SIGBUS , &mySigAction, NULL);
sigaction(SIGSEGV, &mySigAction, NULL);
sigaction(SIGSYS , &mySigAction, NULL);
sigaction(SIGPIPE, &mySigAction, NULL);
sigaction(SIGALRM, &mySigAction, NULL);
sigaction(SIGXCPU, &mySigAction, NULL);
sigaction(SIGXFSZ, &mySigAction, NULL);
另外,还有一种办法就是将console中得错误重定向,写入文本。
1. 设置一个开关,用来清空日志文件内容,并切换输出位置;
- (BOOL)deleteLogFile {
[self finishLog];
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:[self loggingPath] error:nil];
[self startLog];
return success;
}
当我们调用上面的deleteLogFile后,就会清空之前的日志,并设置新的输出。
- (void)finishLog {
fflush(stderr);
dup2(dup(STDERR_FILENO), STDERR_FILENO);
close(dup(STDERR_FILENO));
}
- (NSString*)loggingPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
return logPath;
}
- (void)startLog {
freopen([[self loggingPath] cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}
2. 当日志取得之后,可以通过下面的方式发送邮件给开发者
- (void)sendLogByMail {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:[NSString stringWithFormat:@"%@ - Log", [self appName]]];
NSString *message = [NSString stringWithContentsOfFile:[self loggingPath] encoding:NSUTF8StringEncoding error:nil];
[picker setMessageBody:message isHTML:NO];
[self.navigationController presentModalViewController:picker animated:YES];
[picker release];
}
最后总结:
因iOS sdk中没有<Foundation/NSDebug.h>, 不能使用backtrace_symbols等函数,在网上有一个方便得工具库(google-toolbox-for-mac),其中有专门处理stacktrace的GTMStackTrace.
综合上面的代码如下:
//
// main.m
// Taonan
//
// Created by zeng songgen on 11-3-11.
// Copyright 2011 none. All rights reserved.
//
#import <UIKit/UIKit.h>
#include <signal.h>
#import "config.h"
#import "GTMStackTrace.h"
void exceptionHandler(NSException *exception) {
NSLog(@"Exception throw: %@", exception);
NSLog(@"Stack Trace:\r\n%@", [exception callStackSymbols]);
//NSLog(@"Stack Trace:\r\n%@", GTMStackTraceFromException(exception));
}
void stacktrace(int sig, siginfo_t *info, void *context)
{
/*
NSMutableString * mstr = [[NSMutableString alloc] initWithCapacity:0];
[mstr appendString:@"Stack:\n"];
void* callstack[128];
int i, frameCount = backtrace(callstack, 128);
char** strs = backtrace_symbols(callstack, frameCount);
for (i = 0; i < frameCount; ++i) {
[mstr appendFormat:@"%s\n", strs[i]];
}
NSLog(@"Stack Trace:\r\n%@", mstr);
*/
NSLog(@"Stack Trace:\r\n%@", GTMStackTrace());
}
int main(int argc, char *argv[]) {
NSSetUncaughtExceptionHandler(&exceptionHandler);
struct sigaction mySigAction;
mySigAction.sa_sigaction = stacktrace;
mySigAction.sa_flags = SA_SIGINFO;
sigemptyset(&mySigAction.sa_mask);
sigaction(SIGQUIT, &mySigAction, NULL);
sigaction(SIGILL , &mySigAction, NULL);
sigaction(SIGTRAP, &mySigAction, NULL);
sigaction(SIGABRT, &mySigAction, NULL);
sigaction(SIGEMT , &mySigAction, NULL);
sigaction(SIGFPE , &mySigAction, NULL);
sigaction(SIGBUS , &mySigAction, NULL);
sigaction(SIGSEGV, &mySigAction, NULL);
sigaction(SIGSYS , &mySigAction, NULL);
sigaction(SIGPIPE, &mySigAction, NULL);
sigaction(SIGALRM, &mySigAction, NULL);
sigaction(SIGXCPU, &mySigAction, NULL);
sigaction(SIGXFSZ, &mySigAction, NULL);
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}