/
NuException.m
201 lines (155 loc) · 4.25 KB
/
NuException.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// NuException.m
// Nu
//
// Created by Tim Burks on 4/24/16.
//
//
#import "NuException.h"
#pragma mark - NuException.m
#define kFilenameTopLevel @"<TopLevel>"
@implementation NSException (NuStackTrace)
- (NSString*)dump
{
NSMutableString* dump = [NSMutableString stringWithString:@""];
// Print the system stack trace (10.6 only)
if ([self respondsToSelector:@selector(callStackSymbols)])
{
[dump appendString:@"\nSystem stack trace:\n"];
NSArray* callStackSymbols = [self callStackSymbols];
NSUInteger count = [callStackSymbols count];
for (int i = 0; i < count; i++)
{
[dump appendString:[callStackSymbols objectAtIndex:i]];
[dump appendString:@"\n"];
}
}
return dump;
}
@end
static void Nu_defaultExceptionHandler(NSException* e)
{
[e dump];
}
static BOOL NuException_verboseExceptionReporting = NO;
@interface NuException ()
{
NSMutableArray* stackTrace;
}
@end
@implementation NuException
+ (void)setDefaultExceptionHandler
{
NSSetUncaughtExceptionHandler(*Nu_defaultExceptionHandler);
#ifdef IMPORT_EXCEPTION_HANDLING_FRAMEWORK
[[NSExceptionHandler defaultExceptionHandler]
setExceptionHandlingMask:(NSHandleUncaughtExceptionMask
| NSHandleUncaughtSystemExceptionMask
| NSHandleUncaughtRuntimeErrorMask
| NSHandleTopLevelExceptionMask
| NSHandleOtherExceptionMask)];
#endif
}
+ (void)setVerbose:(BOOL)flag
{
NuException_verboseExceptionReporting = flag;
}
- (void) dealloc
{
if (stackTrace)
{
[stackTrace removeAllObjects];
[stackTrace release];
}
[super dealloc];
}
- (id)initWithName:(NSString *)name reason:(NSString *)reason userInfo:(NSDictionary *)userInfo
{
self = [super initWithName:name reason:reason userInfo:userInfo];
stackTrace = [[NSMutableArray alloc] init];
return self;
}
- (NSArray*)stackTrace
{
return stackTrace;
}
- (NuException *)addFunction:(NSString *)function lineNumber:(int)line
{
return [self addFunction:function lineNumber:line filename:kFilenameTopLevel];
}
- (NuException *)addFunction:(NSString *)function lineNumber:(int)line filename:(NSString *)filename
{
NuTraceInfo* traceInfo = [[[NuTraceInfo alloc] initWithFunction:function
lineNumber:line
filename:filename]
autorelease];
[stackTrace addObject:traceInfo];
return self;
}
- (NSString *)stringValue
{
return [self reason];
}
- (NSString*)dumpExcludingTopLevelCount:(NSUInteger)topLevelCount
{
NSMutableString* dump = [NSMutableString stringWithString:@"Nu uncaught exception: "];
[dump appendString:[NSString stringWithFormat:@"%@: %@\n", [self name], [self reason]]];
NSUInteger count = [stackTrace count] - topLevelCount;
for (int i = 0; i < count; i++)
{
NuTraceInfo* trace = [stackTrace objectAtIndex:i];
NSString* traceString = [NSString stringWithFormat:@" from %@:%d: in %@\n",
[trace filename],
[trace lineNumber],
[trace function]];
[dump appendString:traceString];
}
if (NuException_verboseExceptionReporting)
{
[dump appendString:[super dump]];
}
return dump;
}
- (NSString*)dump
{
return [self dumpExcludingTopLevelCount:0];
}
@end
@interface NuTraceInfo ()
{
NSString* filename;
int lineNumber;
NSString* function;
}
@end
@implementation NuTraceInfo
- (id)initWithFunction:(NSString *)aFunction lineNumber:(int)aLine filename:(NSString *)aFilename
{
self = [super init];
if (self)
{
filename = [aFilename retain];
lineNumber = aLine;
function = [aFunction retain];
}
return self;
}
- (void)dealloc
{
[filename release];
[function release];
[super dealloc];
}
- (NSString *)filename
{
return filename;
}
- (int)lineNumber
{
return lineNumber;
}
- (NSString *)function
{
return function;
}
@end