我们如何在iOS应用程序中存储崩溃日志 [英] How can we store crash logs in iOS application

查看:423
本文介绍了我们如何在iOS应用程序中存储崩溃日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以将正常日志存储到文件中,但是当应用程序崩溃时,如何将崩溃原因存储在文件中.这样我们就可以知道原因. 我们如何在iOS应用程序中存储崩溃日志.

We can store normal logs to file but when application crashes then how to store the cause of crash in file.So we can able to know the cause. How can we store crash logs in iOS application.

推荐答案

在main.m文件中

 int main(int argc, char *argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//    int retVal = UIApplicationMain(argc, argv, nil, nil);
//    [pool release];
//    return retVal;

    int retVal;
    @try
    {
        retVal =  UIApplicationMain(argc, argv, nil, NSStringFromClass([SRSPoulinsAppAppDelegate class]));

    }
    @catch (NSException *exception)
    {
        NSLog(@"CRASH: %@", exception);
       // NSLog(@"Stack Trace: %@", [exception callStackSymbols]);


        NSString *BugFileName = @"BugTracker.txt";

        // Check if the SQL database has already been saved to the users phone, if not then copy it over
        BOOL success;

          NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:BugFileName];

        // Check if the database has already been created in the users filesystem

        success = [fileManager fileExistsAtPath:writableDBPath];

        // If the database already exists then return without doing anything
        if(success)
        {
            NSString *error = [NSString stringWithFormat:@"%@",exception ];
            NSString *errorDesc = [error stringByAppendingString:[NSString stringWithFormat:@"\n%@",[exception callStackSymbols]]];

            NSFileHandle *fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:writableDBPath];
            [fileHandler seekToEndOfFile];
            [fileHandler writeData:[errorDesc dataUsingEncoding:NSUTF8StringEncoding]];
            [fileHandler closeFile];
        }
        else
        {

          writableDBPath = [documentsDirectory stringByAppendingPathComponent:BugFileName];

            //create file if it doesn't exist
            if(![[NSFileManager defaultManager] fileExistsAtPath:writableDBPath])
            {
                [[NSFileManager defaultManager] createFileAtPath:writableDBPath contents:nil attributes:nil];
            }
            //append text to file (you'll probably want to add a newline every write)

            NSString *error = [NSString stringWithFormat:@"%@",exception ];
            NSString *errorDesc = [error stringByAppendingString:[NSString stringWithFormat:@"\n%@",[exception callStackSymbols]]];


            NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:writableDBPath];
            [file seekToEndOfFile];
            [file writeData:[errorDesc dataUsingEncoding:NSUTF8StringEncoding]];
            [file closeFile];

        }

       // [request setDidFinishSelector:@selector(requestFinished:)];
        //[request setDidFailSelector:@selector(requestFailed:)];





    }
    @finally
    {

    }

    return retVal;

}

这篇关于我们如何在iOS应用程序中存储崩溃日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆