如何记录并分析CRASH日志方法一

打开main.m 添加修改以下代码

void exceptionHandler(NSException *exception)


{
    
    id delegate = [[UIApplication sharedApplication] delegate];
    
    if([delegate respondsToSelector:@selector(terminateWithException:)])
        
    {
        
        [delegate performSelector:@selector(terminateWithException:) withObject:exception];
        
    }
    
}
void signalHandler(int sig, siginfo_t *info, void *context)

{
    
    // signal exception
    
    NSException *e = [NSException exceptionWithName:@"signal exception"
                      
                                             reason:[NSString stringWithFormat:@"signal %d", sig]
                      
                                           userInfo:nil];
    
    exceptionHandler(e);
    
}



int main(int argc, char *argv[])
{
    // For uncaultexception
    
    NSSetUncaughtExceptionHandler(&exceptionHandler);
    
    // For signals
    
    struct sigaction sigAction;
    
    sigAction.sa_sigaction = signalHandler;
    
    sigAction.sa_flags = SA_SIGINFO;
    
    sigemptyset(&sigAction.sa_mask);
    
    sigaction(SIGQUIT, &sigAction, NULL);
    
    sigaction(SIGILL, &sigAction, NULL);
    
    sigaction(SIGTRAP, &sigAction, NULL);
    
    sigaction(SIGABRT, &sigAction, NULL);
    
    sigaction(SIGEMT, &sigAction, NULL);
    
    sigaction(SIGFPE, &sigAction, NULL);
    
    sigaction(SIGBUS, &sigAction, NULL);
    
    sigaction(SIGSEGV, &sigAction, NULL);
    
    sigaction(SIGSYS, &sigAction, NULL);
    
    sigaction(SIGPIPE, &sigAction, NULL);
    
    sigaction(SIGALRM, &sigAction, NULL);
    
    sigaction(SIGXCPU, &sigAction, NULL);
    
    sigaction(SIGXFSZ, &sigAction, NULL);
    
    @autoreleasepool {
        

        
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}


然后再打开AppDelegate.m

增加函数

-(void)terminateWithException:(NSException*)e

{
    
    static BOOL deadway = NO;
    
    if(deadway)
        
        return;
    
    deadway = YES;
    
/ /  NSLog(@"Exception --- %@", e); E就是错误信息了,建议写到一个文件里

  
    
    [self applicationWillTerminate:[UIApplication sharedApplication]];
    
    deadway = NO;
    
}


你可能感兴趣的:(exception,struct,null,Class,Crash,Signal)