From 661b803885b252a577c7a4b9699cb181ad6b127f Mon Sep 17 00:00:00 2001 From: Karl Stenerud Date: Wed, 24 Nov 2021 10:21:39 +0100 Subject: [PATCH] Add more information to window-related breadcrumbs --- .../Breadcrumbs/BSGNotificationBreadcrumbs.m | 54 +++++++++++++++---- Bugsnag/Helpers/BSGUtils.h | 8 +-- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/Bugsnag/Breadcrumbs/BSGNotificationBreadcrumbs.m b/Bugsnag/Breadcrumbs/BSGNotificationBreadcrumbs.m index 587042210..31149b6ef 100644 --- a/Bugsnag/Breadcrumbs/BSGNotificationBreadcrumbs.m +++ b/Bugsnag/Breadcrumbs/BSGNotificationBreadcrumbs.m @@ -259,7 +259,7 @@ - (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, *)) { @@ -267,38 +267,72 @@ - (void)addBreadcrumbForNotification:(NSNotification *)notification { 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]; } diff --git a/Bugsnag/Helpers/BSGUtils.h b/Bugsnag/Helpers/BSGUtils.h index 169bd63df..17021c67f 100644 --- a/Bugsnag/Helpers/BSGUtils.h +++ b/Bugsnag/Helpers/BSGUtils.h @@ -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; -}