iOS中捕獲日志與異常示例詳解
前言
在平時(shí)自己調(diào)試的時(shí)候,可以直接連接電腦,直接在窗口中查看結(jié)果。但是在測(cè)試人員測(cè)試,或者灰度測(cè)試的時(shí)候,怎么才能拿到日志呢?最先想到的肯定是輸出到本地文件,然后在需要的時(shí)候進(jìn)行上傳。
分享一段之前找到的方法,下面的代碼提供了兩個(gè)主要功能:
– 把日志輸出到文件中
– 捕捉異常信息
【解析都寫(xiě)在注釋中了】
示例代碼
- (void)redirectNSLogToDocumentFolder { //如果已經(jīng)連接X(jué)code調(diào)試則不輸出到文件 //該函數(shù)用于檢測(cè)輸出 (STDOUT_FILENO) 是否重定向 是個(gè) Linux 程序方法 if(isatty(STDOUT_FILENO)) { return; } // 判斷 當(dāng)前是否在 模擬器環(huán)境 下 在模擬器不保存到文件中 UIDevice *device = [UIDevice currentDevice]; if([[device model] hasSuffix:@"Simulator"]){ return; } //將NSlog打印信息保存到Document目錄下的Log文件夾下 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *logDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Log"]; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL fileExists = [fileManager fileExistsAtPath:logDirectory]; if (!fileExists) { [fileManager createDirectoryAtPath:logDirectory withIntermediateDirectories:YES attributes:nil error:nil]; } NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //每次啟動(dòng)后都保存一個(gè)新的日志文件中 NSString *dateStr = [formatter stringFromDate:[NSDate date]]; NSString *logFilePath = [logDirectory stringByAppendingFormat:@"/%@.log",dateStr]; // 將log輸入到文件 freopen([logFilePath cStringUsingEncoding:NSUTF8StringEncoding], "a+", stdout); freopen([logFilePath cStringUsingEncoding:NSUTF8StringEncoding], "a+", stderr); //未捕獲的Objective-C異常日志 NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler); }
之前看的時(shí)候,對(duì) NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler)
這個(gè)用法一知半解,去翻了一下源碼,這個(gè)方法是在 Foundation 中。
api 中的定義是Changes the top-level error handler ,Sets the top-level error-handling function where you can perform last-minute logging before the program terminates. 通過(guò)替換掉最高級(jí)別的 handle 方法,可以在程序終止之前可以獲取到崩潰信息,并執(zhí)行相應(yīng)的操作,比如保存本地,或者上報(bào)。
方法調(diào)用為:
void NSSetUncaughtExceptionHandler(NSUncaughtExceptionHandler *);
傳入的是一個(gè) NSUncaughtExceptionHandler 的指針。
typedef void NSUncaughtExceptionHandler(NSException *exception);
意思就是需要一個(gè) 返回 void 并且參數(shù)為 NSException *exception
的函數(shù)指針。
你想要,那我就給你!
所以下面有個(gè) C 語(yǔ)言的函數(shù),你看這個(gè)寫(xiě)法和 OC 的聲明也不一樣。
void UncaughtExceptionHandler(NSException* exception) { NSString* name = [ exception name ]; NSString* reason = [ exception reason ]; NSArray* symbols = [ exception callStackSymbols ]; // 異常發(fā)生時(shí)的調(diào)用棧 NSMutableString* strSymbols = [ [ NSMutableString alloc ] init ]; //將調(diào)用棧拼成輸出日志的字符串 for ( NSString* item in symbols ) { [ strSymbols appendString: item ]; [ strSymbols appendString: @"\r\n" ]; } //將crash日志保存到Document目錄下的Log文件夾下 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *logDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Log"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:logDirectory]) { [fileManager createDirectoryAtPath:logDirectory withIntermediateDirectories:YES attributes:nil error:nil]; } NSString *logFilePath = [logDirectory stringByAppendingPathComponent:@"UncaughtException.log"]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *dateStr = [formatter stringFromDate:[NSDate date]]; NSString *crashString = [NSString stringWithFormat:@"<- %@ ->[ Uncaught Exception ]\r\nName: %@, Reason: %@\r\n[ Fe Symbols Start ]\r\n%@[ Fe Symbols End ]\r\n\r\n", dateStr, name, reason, strSymbols]; //把錯(cuò)誤日志寫(xiě)到文件中 if (![fileManager fileExistsAtPath:logFilePath]) { [crashString writeToFile:logFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil]; }else{ NSFileHandle *outFile = [NSFileHandle fileHandleForWritingAtPath:logFilePath]; [outFile seekToEndOfFile]; [outFile writeData:[crashString dataUsingEncoding:NSUTF8StringEncoding]]; [outFile closeFile]; } //把錯(cuò)誤日志發(fā)送到郵箱 // NSString *urlStr = [NSString stringWithFormat:@"mailto://XXXXX@126.com?subject=bug報(bào)告&body=感謝您的配合!<br><br><br>錯(cuò)誤詳情:<br>%@",crashString ]; // NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; // [[UIApplication sharedApplication] openURL:url]; }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位iOS開(kāi)發(fā)者們能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS 10 和Xcode8 一起 創(chuàng)建 Siri 功能步驟詳解(OC寫(xiě)的 )
這篇文章主要介紹了iOS 10 和Xcode8 一起 創(chuàng)建 Siri 功能(OC寫(xiě)的 ),本文分步驟給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-12-12iOS使用AFN進(jìn)行單圖和多圖上傳的實(shí)例代碼
本篇文章中主要介紹了iOS使用AFN進(jìn)行單圖和多圖上傳的實(shí)例代碼,整理出單張和多張圖片上傳的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04IOS 使用NSAssert()和NSParameterAssert調(diào)試程序
這篇文章主要介紹了IOS 使用NSAssert()和NSParameterAssert調(diào)試程序的相關(guān)資料,需要的朋友可以參考下2017-06-06詳解iOS 滾動(dòng)視圖的復(fù)用問(wèn)題解決方案
本篇文章主要介紹iOS 滾動(dòng)視圖的復(fù)用問(wèn)題解決方案,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12iOS實(shí)現(xiàn)對(duì)不同分辨率設(shè)備的字號(hào)大小適配方法
下面小編就為大家分享一篇iOS實(shí)現(xiàn)對(duì)不同分辨率設(shè)備的字號(hào)大小適配方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01iOS 生成plist文件,在項(xiàng)目中代碼創(chuàng)建plist的實(shí)例
下面小編就為大家分享一篇iOS 生成plist文件,在項(xiàng)目中代碼創(chuàng)建plist的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02iOS開(kāi)發(fā)自定義頁(yè)腳和頁(yè)眉技巧詳解
這篇文章主要為大家介紹了iOS開(kāi)發(fā)自定義頁(yè)腳和頁(yè)眉的技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07