From 9228cae08e4f4246ae4efb434be513ea6ba8fe5a Mon Sep 17 00:00:00 2001 From: Nikolay Volosatov Date: Sun, 11 Aug 2024 21:18:21 +0100 Subject: [PATCH 1/3] Move install path to config --- .../InstallConfig.swift | 2 +- .../Sources/LibraryBridge/InstallBridge.swift | 6 ++-- Sources/KSCrashRecording/KSCrash.m | 31 +++---------------- Sources/KSCrashRecording/include/KSCrash.h | 12 ------- .../include/KSCrashConfiguration.h | 2 ++ 5 files changed, 11 insertions(+), 42 deletions(-) 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 fe719dbe..86d3c2f2 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/include/KSCrash.h b/Sources/KSCrashRecording/include/KSCrash.h index b26701bb..93855e75 100644 --- a/Sources/KSCrashRecording/include/KSCrash.h +++ b/Sources/KSCrashRecording/include/KSCrash.h @@ -115,18 +115,6 @@ NS_ASSUME_NONNULL_BEGIN */ @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 a61785dc..cb5ba51f 100644 --- a/Sources/KSCrashRecording/include/KSCrashConfiguration.h +++ b/Sources/KSCrashRecording/include/KSCrashConfiguration.h @@ -38,6 +38,8 @@ typedef NS_ENUM(NSUInteger, KSCDeleteBehavior) { @interface KSCrashConfiguration : NSObject +@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). * From 5535529f625766f16bccdfb503c4351b816c4c92 Mon Sep 17 00:00:00 2001 From: Nikolay Volosatov Date: Fri, 16 Aug 2024 22:08:32 +0100 Subject: [PATCH 2/3] Update docs --- Sources/KSCrashRecording/include/KSCrash.h | 2 -- Sources/KSCrashRecording/include/KSCrashConfiguration.h | 6 ++++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Sources/KSCrashRecording/include/KSCrash.h b/Sources/KSCrashRecording/include/KSCrash.h index 93855e75..ca625fb2 100644 --- a/Sources/KSCrashRecording/include/KSCrash.h +++ b/Sources/KSCrashRecording/include/KSCrash.h @@ -110,8 +110,6 @@ 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); diff --git a/Sources/KSCrashRecording/include/KSCrashConfiguration.h b/Sources/KSCrashRecording/include/KSCrashConfiguration.h index cb5ba51f..e731ec35 100644 --- a/Sources/KSCrashRecording/include/KSCrashConfiguration.h +++ b/Sources/KSCrashRecording/include/KSCrashConfiguration.h @@ -38,6 +38,12 @@ 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. From ce7d59dfa30bd1d6ee5cc5fe953999f5c9f25222 Mon Sep 17 00:00:00 2001 From: Nikolay Volosatov Date: Fri, 16 Aug 2024 22:08:50 +0100 Subject: [PATCH 3/3] Explicitly provide install path to config --- Sources/KSCrashRecording/KSCrashConfiguration.m | 1 + 1 file changed, 1 insertion(+) 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) {