欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

iOS捕獲全局異常,統(tǒng)一收集

參考博文:http://www.cnblogs.com/easonoutlook/archive/2012/12/27/2835979.html

創(chuàng)新互聯(lián)建站主營昆都侖網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,手機APP定制開發(fā),昆都侖h5微信平臺小程序開發(fā)搭建,昆都侖網(wǎng)站營銷推廣歡迎昆都侖等地區(qū)企業(yè)咨詢

開發(fā)程序的過程中不管我們已經(jīng)如何小心,總是會在不經(jīng)意間遇到程序閃退。流暢的操作被無情地Crash打斷,當程序運行Crash的時候,系統(tǒng)會把運行的最后時刻的運行信息記錄下來,存儲到一個文件中,也就是我們所說的Crash文件,當時如果是真機測試離開Xcode的時候Crash掉,我們是無法知道crash的具體位置的。現(xiàn)在做一個程序統(tǒng)一記錄crash的位置。先科普一下crash的知識:


Crash文件結構

1、Process Information(進程信息)

Incident Idnetifier崩潰報告的唯一標識符,不同的Crash
CrashReporter Key設備標識相對應的唯一鍵值(并非真正的設備的UDID,蘋果為了保護用戶隱私iOS6以后已經(jīng)無法獲取)。通常同一個設備上同一版本的App發(fā)生Crash時,該值都是一樣的。
Hardware Model代表發(fā)生Crash的設備類型,上圖中的“iPad4,4”代表iPad Air
Process代表Crash的進程名稱,通常都是我們的App的名字, []里面是當時進程的ID
Path可執(zhí)行程序在手機上的存儲位置,注意路徑時到XXX.app/XXX,XXX.app其實是作為一個Bundle的,真正的可執(zhí)行文件其實是Bundle里面的XXX,感興趣的可以自己查一下相關資料,有機會我后面也會介紹到
Identifier你的App的Indentifier,通常為“com.xxx.yyy”,xxx代表你們公司的域名,yyy代表某一個App
Version當前App的版本號,由Info.plist中的兩個字段組成,CFBundleShortVersionString and CFBundleVersion
Code Type當前App的CPU架構
Parent Process當前進程的父進程,由于iOS中App通常都是單進程的,一般父進程都是launchd

2、Basic Information

Date/TimeCrash發(fā)生的時間,可讀的字符串
OS Version系統(tǒng)版本,()內的數(shù)字代表的時Bulid號
Report VersionCrash日志的格式,目前基本上都是104,不同的version里面包含的字段可能有不同

 

 

3、Exception(非常重要)

Exception Type異常類型
Exception Subtype:異常子類型
Crashed Thread發(fā)生異常的線程號

 

 

 

4、Thread Backtrace

發(fā)生Crash的線程的Crash調用棧,從上到下分別代表調用順序,最上面的一個表示拋出異常的位置,依次往下可以看到API的調用順序。上圖的信息表明本次Crash出現(xiàn)xxxViewController的323行,出錯的函數(shù)調用為orderCountLoadFailed。

5、Thread State

Crash時發(fā)生時刻,線程的狀態(tài),通常我們根據(jù)Crash棧即可獲取到相關信息,這部分一般不用關心。

6、Binary Images

Crash時刻App加載的所有的庫,其中第一行是Crash發(fā)生時我們App可執(zhí)行文件的信息,可以看出為armv7,可執(zhí)行文件的包得uuid位c0f……cd65,解析Crash的時候dsym文件的uuid必須和這個一樣才能完成Crash的符號化解析。

廢話說了那么多,到底怎么捕獲整個系統(tǒng)的crash呢,入正題,其實也很簡單:創(chuàng)建一個工具類  ExceptionHandler.h  和 ExceptionHandler.m(oc版本),swift版本的下面再講.

ExceptionHandler.h

#import <UIKit/UIKit.h>

@interface ExceptionHandler : NSObject{

BOOL dismissed;

}

@end

void HandleException(NSException *exception);

void SignalHandler(int signal);

void InstallUncaughtExceptionHandler(void);

ExceptionHandler.m

#import "ExceptionHandler.h"

#include<libkern/OSAtomic.h>

#include<execinfo.h>

NSString *const ExceptionHandlerSignalExceptionName =@"UncaughtExceptionHandlerSignalExceptionName";

NSString *const ExceptionHandlerSignalKey =@"UncaughtExceptionHandlerSignalKey";

NSString *const ExceptionHandlerAddressesKey =@"UncaughtExceptionHandlerAddressesKey";

volatile int32_t UncaughtExceptionCount =0;

const int32_t UncaughtExceptionMaximum =10;

const NSInteger ExceptionHandlerSkipAddressCount =4;

const NSInteger ExceptionHandlerReportAddressCount =5;

@implementation ExceptionHandler

+ (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 = ExceptionHandlerSkipAddressCount;

i < ExceptionHandlerSkipAddressCount +

ExceptionHandlerReportAddressCount;

i++)

{

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

}

free(strs);

 

return backtrace;

}

- (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex

{

if (anIndex ==0)

{

dismissed =YES;

}

}

- (void)validateAndSaveCriticalApplicationData

{

}

- (void)handleException:(NSException *)exception

{

[self validateAndSaveCriticalApplicationData];

UIAlertView *alert =

[[UIAlertView alloc]

initWithTitle:NSLocalizedString(@"Unhandled exception",nil)

message:[NSString stringWithFormat:NSLocalizedString(

@"You can try to continue but the application may be unstable.\n\n"

@"Debug details follow:\n%@\n%@",nil),

[exception reason],

[[exception userInfo] objectForKey:ExceptionHandlerAddressesKey]]

delegate:self

cancelButtonTitle:NSLocalizedString(@"Quit",nil)

otherButtonTitles:NSLocalizedString(@"Continue",nil),nil];

[alert show];

    

    NSUserDefaults*defaults = [NSUserDefaults standardUserDefaults];

    

CFRunLoopRef runLoop = CFRunLoopGetCurrent();

CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop);

while (!dismissed)

{

for (NSString *modein (NSArray *)allModes)

{

CFRunLoopRunInMode((CFStringRef)mode,0.001,false);

}

}

CFRelease(allModes);

NSSetUncaughtExceptionHandler(NULL);

signal(SIGABRT, SIG_DFL);

signal(SIGILL, SIG_DFL);

signal(SIGSEGV, SIG_DFL);

signal(SIGFPE, SIG_DFL);

signal(SIGBUS, SIG_DFL);

signal(SIGPIPE, SIG_DFL);

if ([[exception name] isEqual:ExceptionHandlerSignalExceptionName])

{

kill(getpid(), [[[exception userInfo] objectForKey:ExceptionHandlerSignalKey] intValue]);

}

else

{

[exception raise];

}

}

@end

void HandleException(NSException *exception)

{

int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCount);

if (exceptionCount > UncaughtExceptionMaximum)

{

return;

}

NSArray *callStack = [ExceptionHandler backtrace];

NSMutableDictionary *userInfo =

[NSMutableDictionary dictionaryWithDictionary:[exception userInfo]];

[userInfo

setObject:callStack

forKey:ExceptionHandlerAddressesKey];

[[[ExceptionHandler alloc] init]

performSelectorOnMainThread:@selector(handleException:)

withObject:

[NSException

exceptionWithName:[exception name]

reason:[exception reason]

userInfo:userInfo]

waitUntilDone:YES];

}

void SignalHandler(int signal)

{

int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCount);

if (exceptionCount > UncaughtExceptionMaximum)

{

return;

}

NSMutableDictionary *userInfo =

[NSMutableDictionary

dictionaryWithObject:[NSNumber numberWithInt:signal]

forKey:ExceptionHandlerSignalKey];

NSArray *callStack = [ExceptionHandler backtrace];

[userInfo

setObject:callStack

forKey:ExceptionHandlerAddressesKey];

[[[ExceptionHandler alloc] init]

performSelectorOnMainThread:@selector(handleException:)

withObject:

[NSException

exceptionWithName:ExceptionHandlerSignalExceptionName

reason:

[NSString stringWithFormat:

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

signal]

userInfo:

[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:signal] forKey:ExceptionHandlerSignalKey]]

waitUntilDone:YES];

}

void InstallUncaughtExceptionHandler(void)

{

NSSetUncaughtExceptionHandler(&HandleException);

signal(SIGABRT, SignalHandler);

signal(SIGILL, SignalHandler);

signal(SIGSEGV, SignalHandler);

signal(SIGFPE, SignalHandler);

signal(SIGBUS, SignalHandler);

signal(SIGPIPE, SignalHandler);

}

使用方式:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    

    InstallUncaughtExceptionHandler();

   returnYES;

}

大功告成

swift版本:

func application(application:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) ->Bool {

          

       //輸出bug信息

        print("系統(tǒng)bug日志記錄---------------------\(NSUserDefaults.standardUserDefaults().valueForKey(ERROR_MESSAGE))")

        NSSetUncaughtExceptionHandler { exceptionin

           var message = exception.callStackSymbols

            message.removeAll()

            message.append("錯誤理由:\(exception.reason!)")

            message.append("錯誤詳細信息:\(exception.callStackSymbols)")

            NSUserDefaults.standardUserDefaults().setObject(message, forKey: ERROR_MESSAGE)

            NSUserDefaults.standardUserDefaults().synchronize()

            print("系統(tǒng)bug日志記錄---------------------\(message)")

        }

       returntrue

    }

    

swift比oc簡單很多,直接在didFinishLaunchingWithOptions加入上面的代碼就可以。

測試代碼:

 NSMutableArray *array = [NSMutableArray array];

    [array addObject:@"1"];

    [array addObject:@"2"];

    

    NSLog(@"%@",array[9]);

效果圖  數(shù)組越界:

iOS 捕獲全局異常,統(tǒng)一收集








網(wǎng)頁題目:iOS捕獲全局異常,統(tǒng)一收集
本文URL:http://chinadenli.net/article8/pgjjop.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器微信小程序手機網(wǎng)站建設定制網(wǎng)站靜態(tài)網(wǎng)站軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化