iOS日志记录和异常捕获

日志记录

iOS日志记录当前文件的堆栈、类名、函数名、行号及文件路径等信息

NSArray *array = [NSThread callStackSymbols];
NSLog(@"堆栈信息:        %@", array);
NSLog(@"当前类名:         %@", NSStringFromClass([self class]));
NSLog(@"当前函数名:       %s", __func__);
NSLog(@"当前函数和参数:    %s", __PRETTY_FUNCTION__);
NSLog(@"当前函数的行号:    %d", __LINE__);
NSLog(@"当前文件路径:      %s", __FILE__);

输出

test[21663:2092255] 堆栈信息:        (
	0   test                                0x000000010549fb71 -[ViewController viewDidLoad] + 545
	1   UIKitCore                           0x0000000108f3e43b -[UIViewController loadViewIfRequired] + 1183
	2   UIKitCore                           0x0000000108f3e868 -[UIViewController view] + 27
	3   UIKitCore                           0x0000000109576c33 -[UIWindow addRootViewControllerViewIfPossible] + 122
	4   UIKitCore                           0x0000000109577327 -[UIWindow _setHidden:forced:] + 289
	5   UIKitCore                           0x0000000109589f86 -[UIWindow makeKeyAndVisible] + 42
	6   UIKitCore                           0x0000000109539f1c -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4555
	7   UIKitCore                           0x000000010953f0c6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1617
	8   UIKitCore                           0x0000000108d846d6 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCo
test[21663:2092255] 当前类名:         ViewController
test[21663:2092255] 当前函数名:       -[ViewController viewDidLoad]
test[21663:2092255] 当前函数和参数:    -[ViewController viewDidLoad]
test[21663:2092255] 当前函数的行号:    47
test[21663:2092255] 当前文件路径:      /Users/victor/Documents/iOS/test/test/ViewController.m



异常捕获

主要是NSSetUncaughtExceptionHandler函数

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 关键函数:设置未捕获的异常处理器
    NSSetUncaughtExceptionHandler(&getException);
    return YES;
}

//获得异常的C函数
void getException(NSException *exception)
{
    NSLog(@"名字:%@",exception.name);
    NSLog(@"原因:%@",exception.reason);
    NSLog(@"用户信息:%@",exception.userInfo);
    NSLog(@"栈内存地址:%@",exception.callStackReturnAddresses);
    NSLog(@"栈描述:%@",exception.callStackSymbols);
    //每次启动的时候将,捕获的异常信息,反馈给服务器
    //获取当前设备
    UIDevice*divice=[UIDevice currentDevice];
    //1.系统版本
    NSString*systemVersion=divice.systemVersion;
    //2.app当前版本
    //先获取当前infoplist文件数据
    NSDictionary*infoDic=[[NSBundle mainBundle] infoDictionary];
    //然后从字典中取出版本号
    NSString*version=[infoDic objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"系统版本%@",version);
    //3.系统时间
    NSDate*date=[NSDate date];
    //4.设备种类
    NSString*model=divice.model;
    //将捕获的异常数据进行保存,保存到本地
    //可以在下一次启动的时候将数据发给服务器
}

输出

test[21663:2092255] 名字:NSInvalidArgumentException
test[21663:2092255] 原因:Unable to parse constraint format: 
Expected a '-' here 
H:|-(margin)1-[redView]-(margin)-[greenView(==redView)]-(margin)-| 
            ^
test[21663:2092255] 用户信息:(null)
test[21663:2092255] 栈内存地址:(0x1067d26e3 0x105d76ac5 0x1059d38e0 0x1059d3a5c 0x1059d337f 0x10549fe0d 0x108f3e43b 0x108f3e868 0x109576c33 0x109577327 0x109589f86 0x109539f1c 0x10953f0c6 0x108d846d6 0x108d8cfce 0x108d842ec 0x108d84c48 0x108d82fba 0x108d82c71 0x108d879b6 0x108d88610 0x108d8771d 0x108d8c6d0 0x10953d9a8 0x1090f4dfa 0x111d6e125 0x111d77ed6 0x111d77700 0x10803cdb5 0x1080402ba 0x111da9146 0x111da8dfe 0x111da9393 0x106739be1 0x106739463 0x106733b1f 0x106733302 0x10ecc22fe 0x109540ba2 0x1054a00c0 0x1080b1541)
test[21663:2092255] 栈描述:(
	0   CoreFoundation                      0x00000001067d26fb __exceptionPreprocess + 331
	1   libobjc.A.dylib                     0x0000000105d76ac5 objc_exception_throw + 48
	2   Foundation                          0x00000001059d38e0 -[NSLayoutConstraintParser metricForKey:] + 0
	3   Foundation                          0x00000001059d3a5c -[NSLayoutConstraintParser parse] + 322
	4   Foundation                          0x00000001059d337f +[NSLayoutConstraintParser constraintsWithVisualFormat:options:metrics:views:] + 91
	5   test                                0x000000010549fe0d -[ViewController viewDidLoad] + 1213
	6   UIKitCore                           0x0000000108f3e43b -[UIViewController loadViewIfRequired] + 1183
	7   UIKitCore                           0x0000000108f3e868 -[UIViewController view] + 27
	8   UIKitCore                           0x0000000109576c33 -[UIWindow addRootViewControllerViewIfPossible] + 122
	9   UIKitCore                           0x0000000109577327 -[UIWindow _setHid
test[21663:2092255] 系统版本1.0
test[21663:2092255] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 
Expected a '-' here 
H:|-(margin)1-[redView]-(margin)-[greenView(==redView)]-(margin)-| 
            ^'
*** First throw call stack:
(
	0   CoreFoundation                      0x00000001067d26fb __exceptionPreprocess + 331
	1   libobjc.A.dylib                     0x0000000105d76ac5 objc_exception_throw + 48
	2   Foundation                          0x00000001059d38e0 -[NSLayoutConstraintParser metricForKey:] + 0
	3   Foundation                          0x00000001059d3a5c -[NSLayoutConstraintParser parse] + 322
	4   Foundation                          0x00000001059d337f +[NSLayoutConstraintParser constraintsWithVisualFormat:options:metrics:views:] + 91
	5   test                                0x000000010549fe0d -[ViewController viewDidLoad] + 1213
	6   UIKitCore                           0x0000000108f3e43b -[UIViewController loadViewIfRequired] + 1183
	7   UIKitCore                           0x0000000108f3e868 -[UIViewController view] + 27
	8   UIKitCore                           0x0000000109576c33 -[UIWindow addRootViewControllerViewIfPossible] + 122
	9   UIKitCore                           0x0000000109577327 -[UIWindow _setHidden:forced:] + 289
	10  UIKitCore                           0x0000000109589f86 -[UIWindow makeKeyAndVisible] + 42
	11  UIKitCore                           0x0000000109539f1c -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4555
	12  UIKitCore                           0x000000010953f0c6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1617
	13  UIKitCore                           0x0000000108d846d6 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 904
	14  UIKitCore                           0x0000000108d8cfce +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
	15  UIKitCore                           0x0000000108d842ec -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236
	16  UIKitCore                           0x0000000108d84c48 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 1091
	17  UIKitCore                           0x0000000108d82fba __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 782
	18  UIKitCore                           0x0000000108d82c71 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
	19  UIKitCore                           0x0000000108d879b6 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 576
	20  UIKitCore                           0x0000000108d88610 _performActionsWithDelayForTransitionContext + 100
	21  UIKitCore                           0x0000000108d8771d -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
	22  UIKitCore                           0x0000000108d8c6d0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
	23  UIKitCore                           0x000000010953d9a8 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 514
	24  UIKitCore                           0x00000001090f4dfa -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361
	25  FrontBoardServices                  0x0000000111d6e125 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 448
	26  FrontBoardServices                  0x0000000111d77ed6 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 283
	27  FrontBoardServices                  0x0000000111d77700 __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 53
	28  libdispatch.dylib                   0x000000010803cdb5 _dispatch_client_callout + 8
	29  libdispatch.dylib                   0x00000001080402ba _dispatch_block_invoke_direct + 300
	30  FrontBoardServices                  0x0000000111da9146 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30
	31  FrontBoardServices                  0x0000000111da8dfe -[FBSSerialQueue _performNext] + 451
	32  FrontBoardServices                  0x0000000111da9393 -[FBSSerialQueue _performNextFromRunLoopSource] + 42
	33  CoreFoundation                      0x0000000106739be1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	34  CoreFoundation                      0x0000000106739463 __CFRunLoopDoSources0 + 243
	35  CoreFoundation                      0x0000000106733b1f __CFRunLoopRun + 1231
	36  CoreFoundation                      0x0000000106733302 CFRunLoopRunSpecific + 626
	37  GraphicsServices                    0x000000010ecc22fe GSEventRunModal + 65
	38  UIKitCore                           0x0000000109540ba2 UIApplicationMain + 140
	39  test                                0x00000001054a00c0 main + 112
	40  libdyld.dylib                       0x00000001080b1541 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

第三方分析工具友盟、Bugly、Instabug、

iOS应用崩溃后会产生dSYM符号集文件,我们需要使用xcode自带的symbolicatecrash工具才能解析该崩溃文件

参考

https://blog.csdn.net/u013538542/article/details/51225550

https://www.jianshu.com/p/77660e626874

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值