Thanks for all the inputs guys .. I have included your solutions in one that would solve my problem. Here is what I did. Of course, I did not compile the code, this is half-baked code. but I will iron it soon as soon as I implement it in my code.
NSLog to file Like NSLog to file LOG2FILE
#if TARGET_IPHONE_SIMULATOR == 0 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"]; freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr); #endif
Catch a crash and write them also to a file
First, create a function that will handle the error and print it to the console (as well as everything you want to do with it):
void uncaughtExceptionHandler(NSException *exception) { NSLog(@"CRASH: %@", exception); NSLog(@"Stack Trace: %@", [exception callStackSymbols]);
Then add an exception handler to your application delegate:
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions: (NSDictionary*)launchOptions { NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
Set the variable in info.plist named Crashed and then read / write it this way
- (void)readPlist { NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"]; NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:localizedPath]; NSString *crashed; crashed = [plistDict objectForKey:@"Crashed"]; } - (void)writeToPlist { NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; [plistDict setValue:@"YES" forKey:@"Crashed"]; [plistDict writeToFile:filePath atomically: YES]; }
After starting the application, read info.plist and ask the user to send crash logs.
{ MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init]; mailComposer.mailComposeDelegate = self;[mailComposer setSubject:@"Crash Log"]; // Set up recipients NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"]; [mailComposer setToRecipients:toRecipients]; // Attach the Crash Log.. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"]; NSData *myData = [NSData dataWithContentsOfFile:logPath]; [mailComposer addAttachmentData:myData mimeType:@"Text/XML" fileName:@"Console.log"]; // Fill out the email body text NSString *emailBody = @"Crash Log"; [mailComposer setMessageBody:emailBody isHTML:NO]; [self presentModalViewController:mailComposer animated:YES]; }
Mobilewits Nov 23 '11 at 5:18 2011-11-23 05:18
source share