Logging full NSException stack traces to your server
*UPDATE* I am using Crashlytics instead now and it’s awesome, go check it out!
Recently in You Doodle I discovered a couple of nasty crashes happening to users by looking in my Flurry Analytics error logs. Unfortunately flurry decides to truncate errors so you don’t get much useful out of it.
I decided I would write a logger to my own server and pass the full exception details. Here’s how I did it:
In my AppDelegate didFinishLaunching, I call this:
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
At the top of my AppDelegate.m file I have this function:
static void uncaughtExceptionHandler(NSException* exception) { #ifdef APP_STORE NSArray* backtrace = [exception callStackSymbols]; NSString* platform = [[UIDevice currentDevice] getHardwareName]; NSString* version = [[UIDevice currentDevice] systemVersion]; NSString* message = [NSString stringWithFormat:@"Device: %@. OS: %@. Backtrace:\n%@", platform, version, backtrace]; DeviceLogRequest* req = [[DeviceLogRequest alloc] initWithText:message]; [req makeRequestWithBlock:nil]; [FlurryAnalytics logError:@"Uncaught" message:message exception:exception]; // give everything 2 seconds to fire off [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0f]]; #endif }
The logging to my server happens in DeviceLogRequest. Basically it does some magic and sends an http post request to a secure end point where the error details, including the full stack trace, are logged into my database for troubleshooting. You’ll still need to de-symbolicate crash addresses manually, but at least you will have the crash log!
Hopefully someone else finds this useful too!