今天試了一次,發現很實用,
可以幫助我們Developer在沒有Debug Console的情況下建立Log Report,
可惜是BAD_EXC_ACCESS的Exception就不能捕捉到Log Report了,
不過還是有用的,所以發上來。
// Define exception handler callback function
void UncaughtExceptionHandler(NSException* exception)
{
// Get the exception detail infomation
NSArray* arr = [exception callStackSymbols];
NSString* reason = [exception reason];
NSString* name = [exception name];
NSString* exceptionStr = [NSString stringWithFormat:@"Exception Name : %@\nReason : %@\n Call Stack Symbols :\n%@", name,reason,[arr componentsJoinedByString:@"\n"]];
// Save to log file and file encoding was UTF-8
[exceptionStr writeToFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, TRUE) objectAtIndex:0] stringByAppendingPathComponent:@"exception.log"] atomically:TRUE encoding:NSUTF8StringEncoding error:nil];
// Print the exception message in debug console
NSLog(exceptionStr);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Setup the exception callback handler
NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
return TRUE;
}
void UncaughtExceptionHandler(NSException* exception)
{
// Get the exception detail infomation
NSArray* arr = [exception callStackSymbols];
NSString* reason = [exception reason];
NSString* name = [exception name];
NSString* exceptionStr = [NSString stringWithFormat:@"Exception Name : %@\nReason : %@\n Call Stack Symbols :\n%@", name,reason,[arr componentsJoinedByString:@"\n"]];
// Save to log file and file encoding was UTF-8
[exceptionStr writeToFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, TRUE) objectAtIndex:0] stringByAppendingPathComponent:@"exception.log"] atomically:TRUE encoding:NSUTF8StringEncoding error:nil];
// Print the exception message in debug console
NSLog(exceptionStr);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Setup the exception callback handler
NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
return TRUE;
}
沒有留言:
張貼留言