Skip to content

Commit

Permalink
Add more information to window-related breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
kstenerud committed Nov 24, 2021
1 parent a0ebe99 commit 661b803
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
54 changes: 44 additions & 10 deletions Bugsnag/Breadcrumbs/BSGNotificationBreadcrumbs.m
Original file line number Diff line number Diff line change
Expand Up @@ -259,46 +259,80 @@ - (void)startListeningForStateChangeNotification:(NSNotificationName)notificatio
[self.notificationCenter addObserver:self selector:@selector(addBreadcrumbForNotification:) name:notificationName object:nil];
}

- (void)addBreadcrumbForNotification:(NSNotification *)notification {
- (BOOL)tryAddSceneNotification:(NSNotification *)notification {
#if (defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0) || \
(defined(__TVOS_13_0) && __TV_OS_VERSION_MAX_ALLOWED >= __TVOS_13_0)
if (@available(iOS 13.0, tvOS 13.0, *)) {
if ([notification.name hasPrefix:@"UIScene"] && [notification.object isKindOfClass:UISCENE]) {
UIScene *scene = notification.object;
NSMutableDictionary *metadata = [NSMutableDictionary dictionary];
metadata[@"configuration"] = scene.session.configuration.name;
metadata[@"delegateClass"] = bsg_string_from_class(scene.session.configuration.delegateClass);
metadata[@"delegateClass"] = BSGStringFromClass(scene.session.configuration.delegateClass);
metadata[@"role"] = scene.session.role;
metadata[@"sceneClass"] = bsg_string_from_class(scene.session.configuration.sceneClass);
metadata[@"sceneClass"] = BSGStringFromClass(scene.session.configuration.sceneClass);
metadata[@"title"] = scene.title.length ? scene.title : nil;
[self addBreadcrumbWithType:BSGBreadcrumbTypeState forNotificationName:notification.name metadata:metadata];
return;
return YES;
}
}
#else
(void)notification;
#endif
return NO;
}

- (BOOL)tryAddWindowNotification:(NSNotification *)notification {
#if !TARGET_OS_OSX && \
(defined(__IPHONE_2_0) || (defined(__TVOS_9_0) && __TV_OS_VERSION_MAX_ALLOWED >= __TVOS_9_0))
if ([notification.name hasPrefix:@"UIWindow"] && [notification.object isKindOfClass:UIWINDOW]) {
UIWindow *window = notification.object;
NSMutableDictionary *metadata = [NSMutableDictionary dictionary];
metadata[@"windowClass"] = bsg_string_from_class(window.class);
metadata[@"description"] = window.description;
#if !TARGET_OS_TV && (defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
if (@available(iOS 13.0, *)) {
UIWindowScene *scene = window.windowScene;
metadata[@"sceneTitle"] = scene.title;
#if defined(__IPHONE_15_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_15_0
if (@available(iOS 15.0, *)) {
metadata[@"sceneSubtitle"] = scene.subtitle;
}
#endif
}
#endif
metadata[@"viewController"] = window.rootViewController.description;
metadata[@"viewControllerTitle"] = window.rootViewController.title;
[self addBreadcrumbWithType:BSGBreadcrumbTypeState forNotificationName:notification.name metadata:metadata];
return;
return YES;
}
#endif
#if TARGET_OS_OSX
if ([notification.name hasPrefix:@"NSWindow"] && [notification.object isKindOfClass:NSWINDOW]) {
NSWindow *window = notification.object;
NSMutableDictionary *metadata = [NSMutableDictionary dictionary];
metadata[@"windowClass"] = window.className;
metadata[@"title"] = window.title;
metadata[@"description"] = window.description;
metadata[@"title"] = window.title;
#if defined(__MAC_11_0) && __MAC_OS_VERSION_MAX_ALLOWED >= __MAC_11_0
if (@available(macOS 11.0, *)) {
metadata[@"subtitle"] = window.subtitle;
}
#endif
metadata[@"representedURL"] = window.representedURL;
metadata[@"viewController"] = window.contentViewController.description;
metadata[@"viewControllerTitle"] = window.contentViewController.title;
[self addBreadcrumbWithType:BSGBreadcrumbTypeState forNotificationName:notification.name metadata:metadata];
return;
return YES;
}
#endif

return NO;
}

- (void)addBreadcrumbForNotification:(NSNotification *)notification {
if ([self tryAddSceneNotification:notification]) {
return;
}
if ([self tryAddWindowNotification:notification]) {
return;
}
[self addBreadcrumbWithType:BSGBreadcrumbTypeState forNotificationName:notification.name];
}

Expand Down
8 changes: 4 additions & 4 deletions Bugsnag/Helpers/BSGUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ NSString *_Nullable BSGStringFromDeviceOrientation(UIDeviceOrientation orientati
API_AVAILABLE(ios(11.0), tvos(11.0))
NSString *_Nullable BSGStringFromThermalState(NSProcessInfoThermalState thermalState);

static inline NSString * _Nullable BSGStringFromClass(Class _Nullable cls) {
return cls ? NSStringFromClass((Class _Nonnull)cls) : nil;
}

NS_ASSUME_NONNULL_END

__END_DECLS

static inline NSString * _Nullable bsg_string_from_class(Class _Nullable cls) {
return cls ? NSStringFromClass((Class _Nonnull)cls) : nil;
}

0 comments on commit 661b803

Please sign in to comment.