如何写一个crash收集sdk

微博以前的crash策略是log批量上传。。。话不多说,上代码,iOS crash 主要分为linux信号异常中断与普通异常捕获。。。

- (void)registerHandlerWithAppId:(NSString *)appId {

  _appId = appId;

  // 1 linux错误信号捕获

  signal(SIGHUP, SignalHandler);

  signal(SIGINT, SignalHandler);

  signal(SIGQUIT, SignalHandler);


  signal(SIGABRT, SignalHandler);

  signal(SIGILL, SignalHandler);

  signal(SIGSEGV, SignalHandler);

  signal(SIGFPE, SignalHandler);

  signal(SIGBUS, SignalHandler);

  signal(SIGPIPE, SignalHandler);

  // 2 objective-c未捕获异常的捕获

  NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);

}


#pragma mark - handlers

void SignalHandler(int code) {

  NSMutableString *mstr = [[NSMutableString alloc] init];

  [mstr appendString:@"Stack:\n"];

  void *callstack[128];

  int i, frames = backtrace(callstack, 128);

  char **strs = backtrace_symbols(callstack, frames);

  for (i = 0; i < frames; ++i) {

    [mstr appendFormat:@"%s\n", strs[i]];

  }

  CrashObject *crashObj = [[CrashObject alloc] init];

  crashObj.name = @"signal caused crash";

  crashObj.appId = instance.appId;

  NSString *content =

      [NSString stringWithFormat:@"%@---call stack:%@", @"signal", mstr];

  crashObj.content = [instance removeSpaceAndNewline:content];

  NSDictionary *sysDic = [instance getSysDic];

  crashObj.versionCode = [sysDic objectForKey:@"CFBundleShortVersionString"];

  crashObj.systemVersion = [[UIDevice currentDevice] systemVersion];

  crashObj.model = [[UIDevice currentDevice] model];

  crashObj.crashId = [instance getunixTimestamp];

  crashObj.timeStap = [instance getCurrentTimes];

  [instance appendNewCrash:crashObj];

}

void UncaughtExceptionHandler(NSException *exception) {


  NSArray *arr = [exception callStackSymbols]; //得到当前调用栈信息

  NSString *reason = [exception reason]; //非常重要,就是崩溃的原因

  NSString *name = [exception name];     //异常类型

  CrashObject *crashObj = [[CrashObject alloc] init];

  crashObj.name = name;

  crashObj.appId = instance.appId;

  NSString *content =

      [NSString stringWithFormat:@"%@---call stack:%@", reason,

                                 [instance jsonStringFromArray:arr]];

  crashObj.content = content;


  NSDictionary *sysDic = [instance getSysDic];

  crashObj.versionCode = [sysDic objectForKey:@"CFBundleShortVersionString"];

  crashObj.systemVersion = [[UIDevice currentDevice] systemVersion];

  crashObj.model = [[UIDevice currentDevice] model];

  crashObj.crashId = [instance getunixTimestamp];

  crashObj.timeStap = [instance getCurrentTimes];

  [instance appendNewCrash:crashObj];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值