Skip to content

Commit

Permalink
refactor: macOS native bridge - add SentryConfigureScope()
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed May 4, 2022
1 parent 786b15d commit 61691df
Showing 1 changed file with 74 additions and 80 deletions.
154 changes: 74 additions & 80 deletions package-dev/Plugins/macOS/SentryNativeBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ void SentryNativeBridgeStartWithOptions(const void *options)
[SentrySDK performSelector:@selector(startWithOptions:) withObject:dictOptions];
}

void SentryConfigureScope(void (^callback)(id))
{
[SentrySDK performSelector:@selector(configureScope:) withObject:callback];
}

/*******************************************************************************/
/* The remaining code is a copy of iOS/SentryNativeBridge.m with changes to */
/* make it work with dynamically loaded classes. Mainly: */
Expand Down Expand Up @@ -98,37 +103,34 @@ void SentryNativeBridgeAddBreadcrumb(
return;
}

// declaring the (block) callback as a variable to avoid too much editor blank space on the left
void (^scopeUpdateBlock)(id) = ^void(id scope) {
id breadcrumb = [[SentryBreadcrumb alloc] init];

if (timestamp != NULL) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:NSCalendarIdentifierISO8601];
[breadcrumb
setValue:[dateFormatter dateFromString:[NSString stringWithUTF8String:timestamp]]
forKey:@"timestamp"];
}

if (message != NULL) {
[breadcrumb setValue:[NSString stringWithUTF8String:message] forKey:@"message"];
}
@try {
SentryConfigureScope(^(id scope) {
id breadcrumb = [[SentryBreadcrumb alloc] init];

if (timestamp != NULL) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:NSCalendarIdentifierISO8601];
[breadcrumb setValue:[dateFormatter
dateFromString:[NSString stringWithUTF8String:timestamp]]
forKey:@"timestamp"];
}

if (type != NULL) {
[breadcrumb setValue:[NSString stringWithUTF8String:type] forKey:@"type"];
}
if (message != NULL) {
[breadcrumb setValue:[NSString stringWithUTF8String:message] forKey:@"message"];
}

if (category != NULL) {
[breadcrumb setValue:[NSString stringWithUTF8String:category] forKey:@"category"];
}
if (type != NULL) {
[breadcrumb setValue:[NSString stringWithUTF8String:type] forKey:@"type"];
}

[breadcrumb setValue:[NSNumber numberWithInt:level] forKey:@"level"];
if (category != NULL) {
[breadcrumb setValue:[NSString stringWithUTF8String:category] forKey:@"category"];
}

[scope performSelector:@selector(addBreadcrumb:) withObject:breadcrumb];
};
[breadcrumb setValue:[NSNumber numberWithInt:level] forKey:@"level"];

@try {
[SentrySDK performSelector:@selector(configureScope:) withObject:scopeUpdateBlock];
[scope performSelector:@selector(addBreadcrumb:) withObject:breadcrumb];
});
} @catch (NSException *exception) {
NSLog(@"Sentry (bridge): failed to add breadcrumb: %@", exception.reason);
}
Expand All @@ -141,17 +143,16 @@ void SentryNativeBridgeSetExtra(const char *key, const char *value)
}

@try {
[SentrySDK performSelector:@selector(configureScope:)
withObject:^(id scope) {
if (value != NULL) {
[scope performSelector:@selector(setExtraValue:forKey:)
withObject:[NSString stringWithUTF8String:value]
withObject:[NSString stringWithUTF8String:key]];
} else {
[scope performSelector:@selector(removeExtraForKey:)
withObject:[NSString stringWithUTF8String:key]];
}
}];
SentryConfigureScope(^(id scope) {
if (value != NULL) {
[scope performSelector:@selector(setExtraValue:forKey:)
withObject:[NSString stringWithUTF8String:value]
withObject:[NSString stringWithUTF8String:key]];
} else {
[scope performSelector:@selector(removeExtraForKey:)
withObject:[NSString stringWithUTF8String:key]];
}
});
} @catch (NSException *exception) {
NSLog(@"Sentry (bridge): failed to set extra: %@", exception.reason);
}
Expand All @@ -164,17 +165,16 @@ void SentryNativeBridgeSetTag(const char *key, const char *value)
}

@try {
[SentrySDK performSelector:@selector(configureScope:)
withObject:^(id scope) {
if (value != NULL) {
[scope performSelector:@selector(setTagValue:forKey:)
withObject:[NSString stringWithUTF8String:value]
withObject:[NSString stringWithUTF8String:key]];
} else {
[scope performSelector:@selector(removeTagForKey:)
withObject:[NSString stringWithUTF8String:key]];
}
}];
SentryConfigureScope(^(id scope) {
if (value != NULL) {
[scope performSelector:@selector(setTagValue:forKey:)
withObject:[NSString stringWithUTF8String:value]
withObject:[NSString stringWithUTF8String:key]];
} else {
[scope performSelector:@selector(removeTagForKey:)
withObject:[NSString stringWithUTF8String:key]];
}
});
} @catch (NSException *exception) {
NSLog(@"Sentry (bridge): failed to set tag: %@", exception.reason);
}
Expand All @@ -187,11 +187,10 @@ void SentryNativeBridgeUnsetTag(const char *key)
}

@try {
[SentrySDK performSelector:@selector(configureScope:)
withObject:^(id scope) {
[scope performSelector:@selector(removeTagForKey:)
withObject:[NSString stringWithUTF8String:key]];
}];
SentryConfigureScope(^(id scope) {
[scope performSelector:@selector(removeTagForKey:)
withObject:[NSString stringWithUTF8String:key]];
});
} @catch (NSException *exception) {
NSLog(@"Sentry (bridge): failed to unset tag: %@", exception.reason);
}
Expand All @@ -205,31 +204,27 @@ void SentryNativeBridgeSetUser(
}

@try {
[SentrySDK
performSelector:@selector(configureScope:)
withObject:^(id scope) {
id user = [[SentryUser alloc] init];

if (email != NULL) {
[user setValue:[NSString stringWithUTF8String:email] forKey:@"email"];
}

if (userId != NULL) {
[user setValue:[NSString stringWithUTF8String:userId] forKey:@"userId"];
}

if (ipAddress != NULL) {
[user setValue:[NSString stringWithUTF8String:ipAddress]
forKey:@"ipAddress"];
}

if (username != NULL) {
[user setValue:[NSString stringWithUTF8String:username]
forKey:@"username"];
}

[scope setUser:user];
}];
SentryConfigureScope(^(id scope) {
id user = [[SentryUser alloc] init];

if (email != NULL) {
[user setValue:[NSString stringWithUTF8String:email] forKey:@"email"];
}

if (userId != NULL) {
[user setValue:[NSString stringWithUTF8String:userId] forKey:@"userId"];
}

if (ipAddress != NULL) {
[user setValue:[NSString stringWithUTF8String:ipAddress] forKey:@"ipAddress"];
}

if (username != NULL) {
[user setValue:[NSString stringWithUTF8String:username] forKey:@"username"];
}

[scope setUser:user];
});
} @catch (NSException *exception) {
NSLog(@"Sentry (bridge): failed to set user: %@", exception.reason);
}
Expand All @@ -238,8 +233,7 @@ void SentryNativeBridgeSetUser(
void SentryNativeBridgeUnsetUser()
{
@try {
[SentrySDK performSelector:@selector(configureScope:)
withObject:^(id scope) { [scope setUser:nil]; }];
SentryConfigureScope(^(id scope) { [scope setUser:nil]; });
} @catch (NSException *exception) {
NSLog(@"Sentry (bridge): failed to unset user: %@", exception.reason);
}
Expand Down

0 comments on commit 61691df

Please sign in to comment.