Skip to content

Commit

Permalink
impr: Add extra logs for UIViewControllerSwizzling (#4511)
Browse files Browse the repository at this point in the history
Add more log messages for potential troubleshooting for swizzling
UIViewControllers.
  • Loading branch information
philipphofmann authored Nov 6, 2024
1 parent 06d3040 commit f31a3ef
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Improvements

- Add extra logs for UIViewControllerSwizzling (#4511)

## 8.40.0

## Feature
Expand Down
10 changes: 5 additions & 5 deletions Sources/Sentry/SentrySubClassFinder.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ - (instancetype)initWithDispatchQueue:(SentryDispatchQueueWrapper *)dispatchQueu
- (void)actOnSubclassesOfViewControllerInImage:(NSString *)imageName block:(void (^)(Class))block;
{
[self.dispatchQueue dispatchAsyncWithBlock:^{
SENTRY_LOG_DEBUG(@"ActOnSubclassesOfViewControllerInImage: %@", imageName);

Class viewControllerClass = [UIViewController class];
if (viewControllerClass == nil) {
SENTRY_LOG_DEBUG(@"UIViewController class not found.");
Expand Down Expand Up @@ -85,11 +87,9 @@ - (void)actOnSubclassesOfViewControllerInImage:(NSString *)imageName block:(void
block(NSClassFromString(className));
}

[SentryLog
logWithMessage:[NSString stringWithFormat:@"The following UIViewControllers will "
@"generate automatic transactions: %@",
[classesToSwizzle componentsJoinedByString:@", "]]
andLevel:kSentryLevelDebug];
SENTRY_LOG_DEBUG(
@"The following UIViewControllers will generate automatic transactions: %@",
[classesToSwizzle componentsJoinedByString:@", "]);
}];
}];
}
Expand Down
13 changes: 11 additions & 2 deletions Sources/Sentry/SentryUIViewControllerSwizzling.m
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,23 @@ - (void)swizzleRootViewControllerAndDescendant:(UIViewController *)rootViewContr
NSArray<UIViewController *> *allViewControllers =
[SentryViewController descendantsOfViewController:rootViewController];

SENTRY_LOG_DEBUG(@"Found %lu descendants for RootViewController %@", allViewControllers.count,
rootViewController.description);

for (UIViewController *viewController in allViewControllers) {
Class viewControllerClass = [viewController class];
if (viewControllerClass != nil) {
SENTRY_LOG_DEBUG(@"Calling swizzleRootViewController.");
SENTRY_LOG_DEBUG(
@"Calling swizzleRootViewController for %@", viewController.description);
[self swizzleViewControllerSubClass:viewControllerClass];

// We can't get the image name with the app delegate class for some apps. Therefore, we
// use the rootViewController and its subclasses as a fallback. The following method
// ensures we don't swizzle ViewControllers of UIKit.
[self swizzleUIViewControllersOfClassesInImageOf:viewControllerClass];
} else {
SENTRY_LOG_WARN(@"ViewControllerClass was nil for UIViewController: %@",
viewController.description);
}
}
}
Expand All @@ -318,8 +325,10 @@ - (void)swizzleUIViewController

- (void)swizzleViewControllerSubClass:(Class)class
{
if (![self shouldSwizzleViewController:class])
if (![self shouldSwizzleViewController:class]) {
SENTRY_LOG_DEBUG(@"Skipping swizzling of class: %@", class);
return;
}

// This are the five main functions related to UI creation in a view controller.
// We are swizzling it to track anything that happens inside one of this functions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class SentrySwizzleClassNameExclude: NSObject {
static func shouldExcludeClass(className: String, swizzleClassNameExcludes: Set<String>) -> Bool {
for exclude in swizzleClassNameExcludes {
if className.contains(exclude) {
SentryLog.debug("Excluding class \(className) from swizzling cause it matches the exclude pattern: \(exclude).")
return true
}
}
Expand Down

0 comments on commit f31a3ef

Please sign in to comment.