自定义捕获异常 crash信息日志

// 1 定义异常捕获函数
// 获得异常的C函数
void readException(NSException *exception)
{
    // 设备信息
    NSString *deviceModel = [NSString stringWithFormat:@"设备类型:%@", UIDevice.currentDevice.model];
    NSString *deviceSystem = [NSString stringWithFormat:@"设备系统:%@", UIDevice.currentDevice.systemName];
    NSString *deviceVersion = [NSString stringWithFormat:@"设备系统版本:%@", UIDevice.currentDevice.systemVersion];
    NSString *deviceName = [NSString stringWithFormat:@"设备名称:%@", UIDevice.currentDevice.name];
    NSString *batteryState = @"UIDeviceBatteryStateUnknown";
    switch (UIDevice.currentDevice.batteryState) {
        case UIDeviceBatteryStateUnknown: batteryState = @"UIDeviceBatteryStateUnknown"; break;
        case UIDeviceBatteryStateUnplugged: batteryState = @"UIDeviceBatteryStateUnplugged"; break;
        case UIDeviceBatteryStateCharging: batteryState = @"UIDeviceBatteryStateCharging"; break;
        case UIDeviceBatteryStateFull: batteryState = @"UIDeviceBatteryStateFull"; break;
        default: break;
    }
    NSString *deviceBatteryState = [NSString stringWithFormat:@"设备电池:%@", batteryState];
    NSString *deviceBattery = [NSString stringWithFormat:@"设备量:%f", UIDevice.currentDevice.batteryLevel];
    // 应用信息
    NSString *appName = [NSString stringWithFormat:@"应用名称:%@", [NSBundle.mainBundle.infoDictionary objectForKey:@"CFBundleDisplayName"]];
    NSString *appVersion = [NSString stringWithFormat:@"应用版本:%@", [NSBundle.mainBundle.infoDictionary objectForKey:@"CFBundleShortVersionString"]];
    // 异常信息
    NSString *errorName = [NSString stringWithFormat:@"异常名称:%@", exception.name];
    NSString *errorReason = [NSString stringWithFormat:@"异常原因:%@",exception.reason];
    NSString *errorUser = [NSString stringWithFormat:@"用户信息:%@",exception.userInfo];
    NSString *errorAddress = [NSString stringWithFormat:@"栈内存地址:%@",exception.callStackReturnAddresses];
    NSArray *symbols = exception.callStackSymbols;
    NSMutableString *errorSymbol = [[NSMutableString alloc] initWithString:@"异常描述:"];
    for (NSString *item in symbols) {
        [errorSymbol appendString:@"\r\n"];
        [errorSymbol appendString:item];
    }
    [errorSymbol appendString:@"\n"];
    //
    NSArray *array = @[deviceModel, deviceSystem, deviceVersion, deviceName, deviceBatteryState, deviceBattery, appName, appVersion, errorName, errorReason, errorUser, errorAddress, errorSymbol];
    NSMutableString *crashString = [[NSMutableString alloc] init];
    for (NSString *string in array) {
        [crashString appendString:string];
        [crashString appendString:@"\n"];
    }
    NSLog(@"%@", crashString);
}
// 2 使用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 	
 	// 调用异常 crash 函数
 	NSSetUncaughtExceptionHandler(&readException);
 	
    return YES;
}
// 3 效果
设备类型:iPhone
设备系统:iOS
设备系统版本:12.0
设备名称:iPhone XR
设备电池:UIDeviceBatteryStateUnknown
设备量:-1.000000
应用名称:实时日志
应用版本:1.0
异常名称:NSRangeException
异常原因:*** -[__NSFrozenArrayM objectAtIndexedSubscript:]: index 1000 beyond bounds [0 .. 9]
用户信息:(null)
栈内存地址:(0x111117283 0x1106b3735 0x1110618fc 0x111040b24 0x10fd848d6 0x115c666e3 0x115c66e74 0x115806f2c 0x11581d7bc 0x11581ebac 0x1157aa63f 0x115c4b015 0x11756fd3d 0x117574bf7 0x1174edaa6 0x117524c2a 0x11552bff9 0x115489090 0x11107ab31 0x11107a3a3 0x111074a4f 0x111074221 0x1196fb1dd 0x115532115 0x10fd87bd0 0x11323a551 0x1)
异常描述:
0   CoreFoundation                      0x000000011111729b __exceptionPreprocess + 331
1   libobjc.A.dylib                     0x00000001106b3735 objc_exception_throw + 48
2   CoreFoundation                      0x00000001110618fc _CFThrowFormattedException + 194
3   CoreFoundation                      0x0000000111040b24 -[__NSFrozenArrayM objectAtIndexedSubscript:] + 116
4   HAMLogOutputWindowDemo              0x000000010fd848d6 -[ViewController1 viewWillAppear:] + 278
5   UIKitCore                           0x0000000115c666e3 -[UIViewController _setViewAppearState:isAnimating:] + 690
6   UIKitCore                           0x0000000115c66e74 -[UIViewController __viewWillAppear:] + 131
7   UIKitCore                           0x0000000115806f2c -[UINavigationController _startCustomTransition:] + 1128
8   UIKitCore                           0x000000011581d7bc -[UINavigationController _startDeferredTransitionIfNeeded:] + 741
9   UIKitCore                           0x000000011581ebac -[UINavigationController __viewWillLayoutSubviews] + 150
10  UIKitCore                           0x00000001157aa63f -[UILayoutContainerView layoutSubviews] + 217
11  UIKitCore                           0x0000000115c4b015 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1441
12  QuartzCore                          0x000000011756fd3d -[CALayer layoutSublayers] + 175
13  QuartzCore                          0x0000000117574bf7 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395
14  QuartzCore                          0x00000001174edaa6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 342
15  QuartzCore                          0x0000000117524c2a _ZN2CA11Transaction6commitEv + 576
16  UIKitCore                           0x000000011552bff9 _UIApplicationFlushRunLoopCATransactionIfTooLate + 165
17  UIKitCore                           0x0000000115489090 __handleEventQueueInternal + 6874
18  CoreFoundation                      0x000000011107ab31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
19  CoreFoundation                      0x000000011107a3a3 __CFRunLoopDoSources0 + 243
20  CoreFoundation                      0x0000000111074a4f __CFRunLoopRun + 1263
21  CoreFoundation                      0x0000000111074221 CFRunLoopRunSpecific + 625
22  GraphicsServices                    0x00000001196fb1dd GSEventRunModal + 62
23  UIKitCore                           0x0000000115532115 UIApplicationMain + 140
24  HAMLogOutputWindowDemo              0x000000010fd87bd0 main + 112
25  libdyld.dylib                       0x000000011323a551 start + 1
26  ???                                 0x0000000000000001 0x0 + 1
// 4 对比IDE 打印显示信息
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSFrozenArrayM objectAtIndexedSubscript:]: index 1000 beyond bounds [0 .. 9]'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000011111729b __exceptionPreprocess + 331
	1   libobjc.A.dylib                     0x00000001106b3735 objc_exception_throw + 48
	2   CoreFoundation                      0x00000001110618fc _CFThrowFormattedException + 194
	3   CoreFoundation                      0x0000000111040b24 -[__NSFrozenArrayM objectAtIndexedSubscript:] + 116
	4   HAMLogOutputWindowDemo              0x000000010fd848d6 -[ViewController1 viewWillAppear:] + 278
	5   UIKitCore                           0x0000000115c666e3 -[UIViewController _setViewAppearState:isAnimating:] + 690
	6   UIKitCore                           0x0000000115c66e74 -[UIViewController __viewWillAppear:] + 131
	7   UIKitCore                           0x0000000115806f2c -[UINavigationController _startCustomTransition:] + 1128
	8   UIKitCore                           0x000000011581d7bc -[UINavigationController _startDeferredTransitionIfNeeded:] + 741
	9   UIKitCore                           0x000000011581ebac -[UINavigationController __viewWillLayoutSubviews] + 150
	10  UIKitCore                           0x00000001157aa63f -[UILayoutContainerView layoutSubviews] + 217
	11  UIKitCore                           0x0000000115c4b015 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1441
	12  QuartzCore                          0x000000011756fd3d -[CALayer layoutSublayers] + 175
	13  QuartzCore                          0x0000000117574bf7 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395
	14  QuartzCore                          0x00000001174edaa6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 342
	15  QuartzCore                          0x0000000117524c2a _ZN2CA11Transaction6commitEv + 576
	16  UIKitCore                           0x000000011552bff9 _UIApplicationFlushRunLoopCATransactionIfTooLate + 165
	17  UIKitCore                           0x0000000115489090 __handleEventQueueInternal + 6874
	18  CoreFoundation                      0x000000011107ab31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	19  CoreFoundation                      0x000000011107a3a3 __CFRunLoopDoSources0 + 243
	20  CoreFoundation                      0x0000000111074a4f __CFRunLoopRun + 1263
	21  CoreFoundation                      0x0000000111074221 CFRunLoopRunSpecific + 625
	22  GraphicsServices                    0x00000001196fb1dd GSEventRunModal + 62
	23  UIKitCore                           0x0000000115532115 UIApplicationMain + 140
	24  HAMLogOutputWindowDemo              0x000000010fd87bd0 main + 112
	25  libdyld.dylib                       0x000000011323a551 start + 1
	26  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

发布了804 篇原创文章 · 获赞 135 · 访问量 142万+

猜你喜欢

转载自blog.csdn.net/potato512/article/details/103414817