diff --git a/Sources/Sentry/SentryDependencyContainer.m b/Sources/Sentry/SentryDependencyContainer.m index 1e6c3cb131..2095575cf1 100644 --- a/Sources/Sentry/SentryDependencyContainer.m +++ b/Sources/Sentry/SentryDependencyContainer.m @@ -5,6 +5,7 @@ #import "SentryDispatchQueueWrapper.h" #import "SentryDisplayLinkWrapper.h" #import "SentryExtraContextProvider.h" +#import "SentryFileIOTracker.h" #import "SentryFileManager.h" #import "SentryInternalCDefines.h" #import "SentryLog.h" @@ -186,6 +187,21 @@ - (SentryThreadInspector *)threadInspector SENTRY_DISABLE_THREAD_SANITIZER( return _threadInspector; } +- (SentryFileIOTracker *)fileIOTracker SENTRY_DISABLE_THREAD_SANITIZER( + "double-checked lock produce false alarms") +{ + if (_fileIOTracker == nil) { + @synchronized(sentryDependencyContainerLock) { + if (_fileIOTracker == nil) { + _fileIOTracker = + [[SentryFileIOTracker alloc] initWithThreadInspector:[self threadInspector] + processInfoWrapper:[self processInfoWrapper]]; + } + } + } + return _fileIOTracker; +} + - (SentryDebugImageProvider *)debugImageProvider { @synchronized(sentryDependencyContainerLock) { diff --git a/Sources/Sentry/SentryFileIOTracker.m b/Sources/Sentry/SentryFileIOTracker.m index 51af175baa..253354ba0d 100644 --- a/Sources/Sentry/SentryFileIOTracker.m +++ b/Sources/Sentry/SentryFileIOTracker.m @@ -19,8 +19,6 @@ #import "SentryThreadInspector.h" #import "SentryTracer.h" -const NSString *SENTRY_TRACKING_COUNTER_KEY = @"SENTRY_TRACKING_COUNTER_KEY"; - @interface SentryFileIOTracker () @property (nonatomic, assign) BOOL isEnabled; @@ -32,6 +30,13 @@ @interface SentryFileIOTracker () @implementation SentryFileIOTracker +NSString *const SENTRY_TRACKING_COUNTER_KEY = @"SENTRY_TRACKING_COUNTER_KEY"; + ++ (instancetype)sharedInstance +{ + return SentryDependencyContainer.sharedInstance.fileIOTracker; +} + - (instancetype)initWithThreadInspector:(SentryThreadInspector *)threadInspector processInfoWrapper:(SentryNSProcessInfoWrapper *)processInfoWrapper { diff --git a/Sources/Sentry/SentryFileIOTrackingIntegration.m b/Sources/Sentry/SentryFileIOTrackingIntegration.m index 5c650ca7ce..d634e416ff 100644 --- a/Sources/Sentry/SentryFileIOTrackingIntegration.m +++ b/Sources/Sentry/SentryFileIOTrackingIntegration.m @@ -19,9 +19,7 @@ - (BOOL)installWithOptions:(SentryOptions *)options return NO; } - self.tracker = [[SentryFileIOTracker alloc] - initWithThreadInspector:[[SentryThreadInspector alloc] initWithOptions:options] - processInfoWrapper:[SentryDependencyContainer.sharedInstance processInfoWrapper]]; + self.tracker = [[SentryDependencyContainer sharedInstance] fileIOTracker]; [self.tracker enable]; [SentryNSDataSwizzling.shared startWithOptions:options tracker:self.tracker]; diff --git a/Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h b/Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h index 433cace15e..0ee1a14506 100644 --- a/Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h +++ b/Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h @@ -22,6 +22,7 @@ @class SentrySystemWrapper; @class SentryThreadWrapper; @class SentryThreadInspector; +@class SentryFileIOTracker; @protocol SentryRandom; @protocol SentryCurrentDateProvider; @protocol SentryRateLimits; @@ -77,6 +78,7 @@ SENTRY_NO_INIT @property (nonatomic, strong) SentrySysctl *sysctlWrapper; @property (nonatomic, strong) SentryThreadInspector *threadInspector; @property (nonatomic, strong) id rateLimits; +@property (nonatomic, strong) SentryFileIOTracker *fileIOTracker; #if SENTRY_UIKIT_AVAILABLE @property (nonatomic, strong) SentryFramesTracker *framesTracker; diff --git a/Sources/Sentry/include/SentryFileIOTracker.h b/Sources/Sentry/include/SentryFileIOTracker.h index d45f437122..126a1cfbf4 100644 --- a/Sources/Sentry/include/SentryFileIOTracker.h +++ b/Sources/Sentry/include/SentryFileIOTracker.h @@ -8,6 +8,15 @@ NS_ASSUME_NONNULL_BEGIN @interface SentryFileIOTracker : NSObject SENTRY_NO_INIT +/** + * Convenience accessor to the shared instance of the tracker in the dependency container. + * + * @note Can be used from Swift without import the entire dependency container. + * + * @return The shared instance of the tracker. + */ ++ (instancetype)sharedInstance; + - (instancetype)initWithThreadInspector:(SentryThreadInspector *)threadInspector processInfoWrapper:(SentryNSProcessInfoWrapper *)processInfoWrapper; diff --git a/Sources/Sentry/include/SentryPrivate.h b/Sources/Sentry/include/SentryPrivate.h index 76be9ef069..a3dd22e7ff 100644 --- a/Sources/Sentry/include/SentryPrivate.h +++ b/Sources/Sentry/include/SentryPrivate.h @@ -12,6 +12,7 @@ #import "SentryDateUtil.h" #import "SentryDateUtils.h" #import "SentryDisplayLinkWrapper.h" +#import "SentryFileIOTracker.h" #import "SentryLevelHelper.h" #import "SentryLogC.h" #import "SentryMeta.h"