Skip to content

Instantly share code, notes, and snippets.

@alfwatt
Last active January 29, 2018 14:11
Show Gist options
  • Save alfwatt/5ddfaf68e530a5a69e3d to your computer and use it in GitHub Desktop.
Save alfwatt/5ddfaf68e530a5a69e3d to your computer and use it in GitHub Desktop.
+ (NSString*) exceptionReport:(NSException*) exception
#import <execinfo.h>
#import <ExceptionHandling/ExceptionHandling.h>
+ (NSString*) exceptionReport:(NSException*) exception
{
NSMutableString* report = [NSMutableString new];
NSMutableArray *addresses = [NSMutableArray new];
NSString *stackTrace = [[exception userInfo] objectForKey:NSStackTraceKey];
NSScanner *scanner = [NSScanner scannerWithString:stackTrace];
NSString *token;
[report appendString:[NSString stringWithFormat:@"%@ %@\n\n%@\n\n", exception.name, exception.reason, exception.userInfo]];
while ([scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]
intoString:&token]) {
[addresses addObject:token];
}
NSUInteger numFrames = [addresses count];
if (numFrames > 0) {
void **frames = (void **)malloc(sizeof(void *) * numFrames);
NSUInteger i, parsedFrames;
for (i = 0, parsedFrames = 0; i < numFrames; i++) {
NSString *address = [addresses objectAtIndex:i];
NSScanner* addressScanner = [NSScanner scannerWithString:address];
if (![addressScanner scanHexLongLong:(unsigned long long *)&frames[parsedFrames]]) {
NSLog(@"%@ failed to parse frame address '%@'", [self className], address);
break;
}
parsedFrames++;
}
if (parsedFrames > 0) {
char **frameStrings = backtrace_symbols(frames, (int)parsedFrames);
if (frameStrings) {
for (unsigned i = 0; i < numFrames && frameStrings[i] ; i++) {
[report appendString:[NSString stringWithUTF8String:(char *)frameStrings[i]]];
[report appendString:@"\n"];
}
free(frameStrings);
}
}
free(frames);
}
return report;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment