WinGui: Fix another instance of the Caliburn vs Json.net sillyness where objects...
[HandBrake.git] / macosx / HBApplication.m
blob2e07071287485c6db816e0c8fa5ab828bc521345
1 /*  HBApplication.m $
3  This file is part of the HandBrake source code.
4  Homepage: <http://handbrake.fr/>.
5  It may be used under the terms of the GNU General Public License. */
7 #import "HBApplication.h"
8 #import "HBExceptionAlertController.h"
9 #import "HBUtilities.h"
11 @implementation HBApplication
13 static void CrashMyApplication()
15     *(char *)0x08 = 1;
18 - (NSAttributedString *)_formattedExceptionBacktrace:(NSArray *)backtrace
20     NSMutableAttributedString *result = [[NSMutableAttributedString alloc] init];
21     for (__strong NSString *s in backtrace)
22     {
23         s = [s stringByAppendingString:@"\n"];
24         NSAttributedString *attrS = [[NSAttributedString alloc] initWithString:s];
25         [result appendAttributedString:attrS];
26     }
27     [result addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Monaco" size:10] range:NSMakeRange(0, result.length)];
28     return result;    
31 - (void)reportException:(NSException *)exception
33     // NSApplication simply logs the exception to the console. We want to let the user know
34     // when it happens in order to possibly prevent subsequent random crashes that are difficult to debug
35     @try
36     {
37         @autoreleasepool
38         {
39             // Create a string based on the exception
40             NSString *exceptionMessage = [NSString stringWithFormat:@"%@\nReason: %@\nUser Info: %@", exception.name, exception.reason, exception.userInfo];
41             // Always log to console for history
43             [HBUtilities writeToActivityLog:"Exception raised:\n%s", exceptionMessage.UTF8String];
44             [HBUtilities writeToActivityLog:"Backtrace:\n%s", exception.callStackSymbols.description.UTF8String];
46             HBExceptionAlertController *alertController = [[HBExceptionAlertController alloc] init];
47             alertController.exceptionMessage = exceptionMessage;
48             alertController.exceptionBacktrace = [self _formattedExceptionBacktrace:exception.callStackSymbols];
50             NSInteger result = [alertController runModal];
51             if (result == HBExceptionAlertControllerResultCrash)
52             {
53                 CrashMyApplication();
54             }
55         }
56     }
57     @catch (NSException *e)
58     {
59         // Suppress any exceptions raised in the handling
60     }    
63 @end