Skip to content

Commit

Permalink
Move install path to config (#536)
Browse files Browse the repository at this point in the history
* Move install path to config

* Update docs

* Explicitly provide install path to config
  • Loading branch information
bamx23 committed Aug 27, 2024
1 parent 1158d62 commit 29e574a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions Samples/Common/Sources/LibraryBridge/InstallBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? "<default>")")
KSCrash.setBasePath(basePath)
config.installPath = basePath
}

private var config: KSCrashConfiguration
Expand All @@ -74,7 +74,7 @@ public class InstallBridge: ObservableObject {

$basePath
.removeDuplicates()
.sink(receiveValue: Self.setBasePath(_:))
.sink(receiveValue: setBasePath(_:))
.store(in: &disposables)
}

Expand Down
31 changes: 5 additions & 26 deletions Sources/KSCrashRecording/KSCrash.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.");
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions Sources/KSCrashRecording/KSCrashConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ - (instancetype)init
self = [super init];
if (self) {
KSCrashCConfiguration cConfig = KSCrashCConfiguration_Default();
_installPath = nil;
_monitors = cConfig.monitors;

if (cConfig.userInfoJSON != NULL) {
Expand Down
14 changes: 0 additions & 14 deletions Sources/KSCrashRecording/include/KSCrash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions Sources/KSCrashRecording/include/KSCrashConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ typedef NS_ENUM(NSUInteger, KSCDeleteBehavior) {

@interface KSCrashConfiguration : NSObject <NSCopying>

/** 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).
*
Expand Down

0 comments on commit 29e574a

Please sign in to comment.