2013-05-31 3 views
0

Мы можем хранить обычные журналы в файле, но когда приложение падает, то как сохранить причину сбоя в файле. Поэтому мы можем узнать причину. Как мы можем хранить журналы сбоев в приложении iOS.Как мы можем хранить журналы сбоев в приложении iOS

+0

Рассмотрите возможность использования службы аварии репортер как Crashlytics или Crittercism. – Mar0ux

ответ

1

в 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; 

} 
+0

Обратите внимание, что это будет работать только для исключений Obj-C. Не EXC_BAD_ACCESS и другие подобные сбои. – Mar0ux

Смежные вопросы