zoukankan      html  css  js  c++  java
  • iOS 保存异常日志

    //
    //  AppDelegate.m
    //  test
    //
    //  Created by Chocolate. on 14-4-16.
    //  Copyright (c) 2014年 redasen. All rights reserved.
    //
    
    #import "AppDelegate.h"
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        NSSetUncaughtExceptionHandler(handleRootException);
        return YES;
    }
                                
    - (void)applicationWillResignActive:(UIApplication *)application
    {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    - (void)applicationWillTerminate:(UIApplication *)application
    {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    static void handleRootException( NSException* exception )
    {
        NSString* name = [ exception name ];
        NSString* reason = [ exception reason ];
        NSArray* symbols = [ exception callStackSymbols ]; // 异常发生时的调用栈
        NSMutableString* strSymbols = [ [ NSMutableString alloc ] init ]; // 将调用栈拼成输出日志的字符串
        for ( NSString* item in symbols )
        {
            [ strSymbols appendString: item ];
            [ strSymbols appendString: @"
    " ];
        }
        
        
        
        // 写一个文件,记录此时此刻发生了异常。这个挺有用的哦
        NSDictionary* dict = [ NSDictionary dictionaryWithObjectsAndKeys:
                              name, @"exception_name",                // 当前日志文件名称
                              reason, @"exception_reason",    // 当前日志文件全路径
                              symbols,@"exception_stack",
                              [ NSDate date ], @"TimeStamp",                        // 异常发生的时刻
                              nil ];
        
        NSString* path = [ NSString stringWithFormat: @"%@/Documents/", NSHomeDirectory() ];
        NSString* lastExceptionLog = [ NSString stringWithFormat: @"%@LastExceptionLog.txt", path ];
        [ dict writeToFile: lastExceptionLog atomically: YES ];
    }
    
    @end
  • 相关阅读:
    XML文件的操作说明
    IIS中如何应用程序启用https协议
    sql server中的数据类型转换函数
    sql语句中的join连接(左连接、右连接、全连接、内连接)
    sql语句中日期相减的操作
    C# NameValueCollection集合
    json的两种表示结构(对象和数组).。
    ASP.NET中一般处理程序报的错误:由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值
    C#中类的实例是不能 获取到类中的静态方法和静态变量(Static)的,及原因
    《好好学Java 从零基础到项目实战》姗姗而来
  • 原文地址:https://www.cnblogs.com/lihaibo-Leao/p/3669112.html
Copyright © 2011-2022 走看看