1/*  HBApplication.m $
2
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. */
6
7#import "HBApplication.h"
8#import "HBExceptionAlertController.h"
9
10@import HandBrakeKit;
11
12@implementation HBApplication
13
14static void CrashMyApplication()
15{
16    *(char *)0x08 = 1;
17}
18
19- (NSAttributedString *)_formattedExceptionBacktrace:(NSArray *)backtrace
20{
21    NSMutableAttributedString *result = [[NSMutableAttributedString alloc] init];
22    for (__strong NSString *s in backtrace)
23    {
24        s = [s stringByAppendingString:@"\n"];
25        NSAttributedString *attrS = [[NSAttributedString alloc] initWithString:s];
26        [result appendAttributedString:attrS];
27    }
28    [result addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Monaco" size:10] range:NSMakeRange(0, result.length)];
29    return result;
30}
31
32- (void)reportException:(NSException *)exception
33{
34    // NSApplication simply logs the exception to the console. We want to let the user know
35    // when it happens in order to possibly prevent subsequent random crashes that are difficult to debug
36    @try
37    {
38        @autoreleasepool
39        {
40            // Create a string based on the exception
41            NSString *exceptionMessage = [NSString stringWithFormat:@"%@\nReason: %@\nUser Info: %@", exception.name, exception.reason, exception.userInfo];
42            // Always log to console for history
43
44            [HBUtilities writeToActivityLog:"Exception raised:\n%s", exceptionMessage.UTF8String];
45            [HBUtilities writeToActivityLog:"Backtrace:\n%s", exception.callStackSymbols.description.UTF8String];
46
47            HBExceptionAlertController *alertController = [[HBExceptionAlertController alloc] init];
48            alertController.exceptionMessage = exceptionMessage;
49            alertController.exceptionBacktrace = [self _formattedExceptionBacktrace:exception.callStackSymbols];
50
51            NSInteger result = [alertController runModal];
52            if (result == HBExceptionAlertControllerResultCrash)
53            {
54                CrashMyApplication();
55            }
56        }
57    }
58    @catch (NSException *e)
59    {
60        // Suppress any exceptions raised in the handling
61    }
62}
63
64@end
65