ios崩溃日志收集_iOS Swift Crash日志收集

本文介绍了如何在Swift项目中实现iOS崩溃日志的收集。通过在AppDelegate中设置异常处理,结合Objective-C的HandleCrash类,捕获并记录SIGABRT、SIGILL、SIGSEGV等信号产生的崩溃信息,保存到本地文件error1.log。
摘要由CSDN通过智能技术生成

尝试使用Swift进行日志收集-----------失败,最后还是调用OC实现

AppDelegate中

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

// Override point for customization after application launch.

CatchCrash.share().installUncaughtExceptionHandler()

return true

}

CatchCrash.swift

import UIKit

private let catchCrash = CatchCrash()

private var UncaughtExceptionCount: Int32 = 0

private var UncaughtExceptionMaximum: Int32 = 10

class CatchCrash: NSObject {

private let UncaughtExceptionMaximum: Int32 = 10

private let SignalHandler: Darwin.sig_t? = { (signal) in

let exceptionInfo = HandleCrash.crash(signal)

try! exceptionInfo?.write(toFile: NSHomeDirectory() + "/Documents/error1.log", atomically: true, encoding: .utf8)

}

class func share() -> CatchCrash {

return catchCrash

}

func installUncaughtExceptionHandler() {

NSSetUncaughtExceptionHandler { (exception) in

// 异常的堆栈信息

let stackArray = exception.callStackSymbols

// 出现异常的原因

let reason = exception.reason

// 异常名称

let name = exception.name

let exceptionInfo = "Eqqqqqxception reason:\(String(describing: reason))\n"

+ "Exception name:\(name)\n"

+ "Exception stack:\(stackArray.description)"

print(exceptionInfo)

var tmpArr = stackArray

tmpArr.insert(reason!, at: 0)

//保存到本地

try! exceptionInfo.write(toFile: NSHomeDirectory() + "/Documents/error1.log", atomically: true, encoding: .utf8)

abort()

}

signal(SIGABRT, SignalHandler)

signal(SIGILL, SignalHandler)

signal(SIGSEGV, SignalHandler)

signal(SIGFPE, SignalHandler)

signal(SIGBUS, SignalHandler)

signal(SIGPIPE, SignalHandler)

}

}

CatchCrash-Bridging-Header.h 中进行桥接

#import "HandleCrash.h"

HandleCrash.h

#import

@interface HandleCrash : NSObject

+ (NSString *)crash:(int)signal;

@end

HandleCrash.m

#import "HandleCrash.h"

#import

#include

NSString * const UncaughtExceptionHandlerSignalExceptionName = @"UncaughtExceptionHandlerSignalExceptionName";

NSString * const UncaughtExceptionHandlerSignalKey = @"UncaughtExceptionHandlerSignalKey";

NSString * const UncaughtExceptionHandlerAddressesKey = @"UncaughtExceptionHandlerAddressesKey";

atomic_int UncaughtExceptionCount = 0;

const int32_t UncaughtExceptionMaximum = 10;

const NSInteger UncaughtExceptionHandlerSkipAddressCount = 4;

const NSInteger UncaughtExceptionHandlerReportAddressCount = 5;

@implementation HandleCrash

+ (NSString *)crash:(int)signal {

int exceptionCount = atomic_fetch_add_explicit(&UncaughtExceptionCount, 1, memory_order_relaxed);

if (exceptionCount > UncaughtExceptionMaximum) {

return @"";

}

NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithInt:signal]

forKey:UncaughtExceptionHandlerSignalKey];

NSArray *callStack = [self backtrace];

[userInfo setObject:callStack

forKey:UncaughtExceptionHandlerAddressesKey];

NSException *exception = [NSException

exceptionWithName: UncaughtExceptionHandlerSignalExceptionName reason: [NSString stringWithFormat:

NSLocalizedString(@"Signal %d was raised.", nil),

signal] userInfo: userInfo];

return [NSString stringWithFormat:@"%@, %@, %@", exception.name, exception.reason, exception.userInfo];

}

+ (NSArray *)backtrace

{

void* callstack[128];

int frames = backtrace(callstack, 128);

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

int i;

NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames];

for (

i = UncaughtExceptionHandlerSkipAddressCount;

i < UncaughtExceptionHandlerSkipAddressCount +

UncaughtExceptionHandlerReportAddressCount;

i++)

{

[backtrace addObject:[NSString stringWithUTF8String:strs[i]]];

}

free(strs);

return backtrace;

}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值