diff --git a/Samples/Common/Sources/IntegrationTestsHelper/InstallConfig.swift b/Samples/Common/Sources/IntegrationTestsHelper/InstallConfig.swift index 64e79f94..164b4b11 100644 --- a/Samples/Common/Sources/IntegrationTestsHelper/InstallConfig.swift +++ b/Samples/Common/Sources/IntegrationTestsHelper/InstallConfig.swift @@ -38,8 +38,8 @@ public struct InstallConfig: Codable { extension InstallConfig { func install() throws { - KSCrash.setBasePath(installPath) let config = KSCrashConfiguration() + config.installPath = installPath if let isCxaThrowEnabled { config.enableSwapCxaThrow = isCxaThrowEnabled } diff --git a/Samples/Common/Sources/LibraryBridge/InstallBridge.swift b/Samples/Common/Sources/LibraryBridge/InstallBridge.swift index ec818315..06defa08 100644 --- a/Samples/Common/Sources/LibraryBridge/InstallBridge.swift +++ b/Samples/Common/Sources/LibraryBridge/InstallBridge.swift @@ -56,10 +56,10 @@ public class InstallBridge: ObservableObject { private static let logger = Logger(label: "InstallBridge") - private static func setBasePath(_ value: BasePath) { + private func setBasePath(_ value: BasePath) { let basePath = value.basePaths.first.flatMap { $0 + "/KSCrash" } Self.logger.info("Setting KSCrash base path to: \(basePath ?? "")") - KSCrash.setBasePath(basePath) + config.installPath = basePath } private var config: KSCrashConfiguration @@ -74,7 +74,7 @@ public class InstallBridge: ObservableObject { $basePath .removeDuplicates() - .sink(receiveValue: Self.setBasePath(_:)) + .sink(receiveValue: setBasePath(_:)) .store(in: &disposables) } diff --git a/Sources/KSCrashRecording/KSCrash.m b/Sources/KSCrashRecording/KSCrash.m index 51427540..9420c9dc 100644 --- a/Sources/KSCrashRecording/KSCrash.m +++ b/Sources/KSCrashRecording/KSCrash.m @@ -55,12 +55,10 @@ @interface KSCrash () @property(nonatomic, readwrite, copy) NSString *bundleName; -@property(nonatomic, readwrite, copy) NSString *basePath; @property(nonatomic, strong) KSCrashConfiguration *configuration; @end -static NSString *gCustomBasePath = nil; static BOOL gIsSharedInstanceCreated = NO; static NSString *getBundleName(void) @@ -72,12 +70,8 @@ @interface KSCrash () return bundleName; } -static NSString *getBasePath(void) +static NSString *getDefaultInstallPath(void) { - if (gCustomBasePath != nil) { - return gCustomBasePath; - } - NSArray *directories = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); if ([directories count] == 0) { KSLOG_ERROR(@"Could not locate cache directory path."); @@ -110,38 +104,22 @@ + (void)initialize } } -+ (void)setBasePath:(NSString *)basePath; -{ - if (basePath == gCustomBasePath || [basePath isEqualToString:gCustomBasePath]) { - return; - } - if (gIsSharedInstanceCreated) { - KSLOG_WARN(@"A shared instance of KSCrash is already created. Can't change the base path to: %@", basePath); - } - gCustomBasePath = [basePath copy]; -} - + (instancetype)sharedInstance { static KSCrash *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - sharedInstance = [[KSCrash alloc] initWithBasePath:getBasePath()]; + sharedInstance = [[KSCrash alloc] init]; gIsSharedInstanceCreated = YES; }); return sharedInstance; } -- (instancetype)initWithBasePath:(NSString *)basePath +- (instancetype)init { - if (basePath == nil) { - KSLOG_ERROR(@"Failed to initialize crash handler. Crash reporting disabled."); - return nil; - } if ((self = [super init])) { _bundleName = getBundleName(); - _basePath = [basePath copy]; } return self; } @@ -243,8 +221,9 @@ - (NSDictionary *)systemInfo - (BOOL)installWithConfiguration:(KSCrashConfiguration *)configuration error:(NSError **)error { self.configuration = [configuration copy] ?: [KSCrashConfiguration new]; + NSString *installPath = configuration.installPath ?: getDefaultInstallPath(); KSCrashInstallErrorCode result = - kscrash_install(self.bundleName.UTF8String, self.basePath.UTF8String, [self.configuration toCConfiguration]); + kscrash_install(self.bundleName.UTF8String, installPath.UTF8String, [self.configuration toCConfiguration]); if (result != KSCrashInstallErrorNone) { if (error != NULL) { diff --git a/Sources/KSCrashRecording/KSCrashConfiguration.m b/Sources/KSCrashRecording/KSCrashConfiguration.m index 7c554bea..04a555d2 100644 --- a/Sources/KSCrashRecording/KSCrashConfiguration.m +++ b/Sources/KSCrashRecording/KSCrashConfiguration.m @@ -35,6 +35,7 @@ - (instancetype)init self = [super init]; if (self) { KSCrashCConfiguration cConfig = KSCrashCConfiguration_Default(); + _installPath = nil; _monitors = cConfig.monitors; if (cConfig.userInfoJSON != NULL) { diff --git a/Sources/KSCrashRecording/include/KSCrash.h b/Sources/KSCrashRecording/include/KSCrash.h index b26701bb..ca625fb2 100644 --- a/Sources/KSCrashRecording/include/KSCrash.h +++ b/Sources/KSCrashRecording/include/KSCrash.h @@ -110,23 +110,9 @@ NS_ASSUME_NONNULL_BEGIN + (instancetype)new NS_UNAVAILABLE; /** Get the singleton instance of the crash reporter. - * - * @note To specify a custom base direcory for KSCrash use `setBasePath:` method. */ @property(class, readonly) KSCrash *sharedInstance NS_SWIFT_NAME(shared); -/** - * Specifies a custom base path for KSCrash installation. - * By default a "KSCrash" directory inside the default cache directory is used. - * - * @param basePath An absolute path to directory in which KSCrash stores the data. - * If `nil` the default directory is used. - * - * @note This method SHOULD be called before any use of `sharedInstance` method. - * Any call of this method after that is ignored. - */ -+ (void)setBasePath:(nullable NSString *)basePath; - /** Install the crash reporter. * The reporter will record crashes, but will not send any crash reports unless a sink is set. * diff --git a/Sources/KSCrashRecording/include/KSCrashConfiguration.h b/Sources/KSCrashRecording/include/KSCrashConfiguration.h index 60a099ab..b280b21a 100644 --- a/Sources/KSCrashRecording/include/KSCrashConfiguration.h +++ b/Sources/KSCrashRecording/include/KSCrashConfiguration.h @@ -38,6 +38,14 @@ typedef NS_ENUM(NSUInteger, KSCDeleteBehavior) { @interface KSCrashConfiguration : NSObject +/** Specifies a custom base path for KSCrash installation. + * If `nil` the default directory is used:. + * The default directory is "KSCrash" inside the default cache directory. + * + * **Default**: `nil` + */ +@property(nonatomic, copy, nullable) NSString *installPath; + /** The crash types that will be handled. * Some crash types may not be enabled depending on circumstances (e.g., running in a debugger). *