Last active
January 29, 2018 14:11
-
-
Save alfwatt/5ddfaf68e530a5a69e3d to your computer and use it in GitHub Desktop.
+ (NSString*) exceptionReport:(NSException*) exception
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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