Skip to content
Browse files

improve thread dispatching in log exception method

  • Loading branch information...
1 parent a744ffb commit d43b2f4a00ea11df7b67802ddd315a09258751c8 @calebd calebd committed
Showing with 48 additions and 55 deletions.
  1. +48 −55 Airbrake/notifier/ABNotifier.m
View
103 Airbrake/notifier/ABNotifier.m
@@ -206,65 +206,58 @@ + (NSString *)APIKey {
#pragma mark - write data
+ (void)logException:(NSException *)exception parameters:(NSDictionary *)parameters {
- // declare block that will perform logging
- void (^block) (void) = ^{
-
- // get file handle
- NSString *name = [[NSProcessInfo processInfo] globallyUniqueString];
- NSString *path = [self pathForNewNoticeWithName:name];
- int fd = ABNotifierOpenNewNoticeFile([path UTF8String], ABNotifierExceptionNoticeType);
-
- // write stuff
- if (fd > -1) {
- @try {
-
- // create parameters
- NSMutableDictionary *exceptionParameters = [NSMutableDictionary dictionary];
- if ([parameters count]) { [exceptionParameters addEntriesFromDictionary:parameters]; }
- [exceptionParameters setValue:ABNotifierResidentMemoryUsage() forKey:@"Resident Memory Size"];
- [exceptionParameters setValue:ABNotifierVirtualMemoryUsage() forKey:@"Virtual Memory Size"];
-
- // write exception
- NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
- [exception name], ABNotifierExceptionNameKey,
- [exception reason], ABNotifierExceptionReasonKey,
- [exception callStackSymbols], ABNotifierCallStackKey,
- exceptionParameters, ABNotifierExceptionParametersKey,
+ // force all activity onto main thread
+ if (![NSThread isMainThread]) {
+ dispatch_sync(dispatch_get_main_queue(), ^{
+ [self logException:exception parameters:parameters];
+ });
+ return;
+ }
+
+ // get file handle
+ NSString *name = [[NSProcessInfo processInfo] globallyUniqueString];
+ NSString *path = [self pathForNewNoticeWithName:name];
+ int fd = ABNotifierOpenNewNoticeFile([path UTF8String], ABNotifierExceptionNoticeType);
+
+ // write stuff
+ if (fd > -1) {
+ @try {
+
+ // create parameters
+ NSMutableDictionary *exceptionParameters = [NSMutableDictionary dictionary];
+ if ([parameters count]) { [exceptionParameters addEntriesFromDictionary:parameters]; }
+ [exceptionParameters setValue:ABNotifierResidentMemoryUsage() forKey:@"Resident Memory Size"];
+ [exceptionParameters setValue:ABNotifierVirtualMemoryUsage() forKey:@"Virtual Memory Size"];
+
+ // write exception
+ NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
+ [exception name], ABNotifierExceptionNameKey,
+ [exception reason], ABNotifierExceptionReasonKey,
+ [exception callStackSymbols], ABNotifierCallStackKey,
+ exceptionParameters, ABNotifierExceptionParametersKey,
#if TARGET_OS_IPHONE
- ABNotifierCurrentViewController(), ABNotifierControllerKey,
+ ABNotifierCurrentViewController(), ABNotifierControllerKey,
#endif
- nil];
- NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
- unsigned long length = [data length];
- write(fd, &length, sizeof(unsigned long));
- write(fd, [data bytes], length);
-
- // delegate
- id<ABNotifierDelegate> delegate = [self delegate];
- if ([delegate respondsToSelector:@selector(notifierDidLogException:)]) {
- [delegate notifierDidLogException:exception];
- }
-
- }
- @catch (NSException *exception) {
- ABLog(@"Exception encountered while logging exception");
- ABLog(@"%@", exception);
- }
- @finally {
- close(fd);
+ nil];
+ NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
+ unsigned long length = [data length];
+ write(fd, &length, sizeof(unsigned long));
+ write(fd, [data bytes], length);
+
+ // delegate
+ id<ABNotifierDelegate> delegate = [self delegate];
+ if ([delegate respondsToSelector:@selector(notifierDidLogException:)]) {
+ [delegate notifierDidLogException:exception];
}
+
+ }
+ @catch (NSException *exception) {
+ ABLog(@"Exception encountered while logging exception");
+ ABLog(@"%@", exception);
+ }
+ @finally {
+ close(fd);
}
-
- };
-
- // run the task if we are on the main thread
- if ([NSThread isMainThread]) {
- block();
- }
-
- // dispatch to main thread and wait
- else {
- dispatch_sync(dispatch_get_main_queue(), block);
}
}

0 comments on commit d43b2f4

Please sign in to comment.
Something went wrong with that request. Please try again.