Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom base override #513

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions Sources/KSCrashRecording/KSCrash.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ @interface KSCrash ()

@end

static NSString *gCustomBasePath = nil;
static BOOL gIsSharedInstanceCreated = NO;

static NSString* getBundleName(void)
{
Expand All @@ -74,6 +76,11 @@ @interface KSCrash ()

static NSString* getBasePath(void)
{
if(gCustomBasePath != nil)
{
return gCustomBasePath;
}

NSArray* directories = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask,
YES);
Expand Down Expand Up @@ -121,23 +128,32 @@ + (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] init];
sharedInstance = [[KSCrash alloc] initWithBasePath:getBasePath()];
gIsSharedInstanceCreated = YES;
});
return sharedInstance;
}

- (instancetype) init
{
return [self initWithBasePath:getBasePath()];
}

- (instancetype) initWithBasePath:(NSString *)basePath
- (instancetype) initWithBasePath:(NSString*) basePath
{
if((self = [super init]))
{
Expand Down
18 changes: 16 additions & 2 deletions Sources/KSCrashRecording/include/KSCrash.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,24 @@ NS_ASSUME_NONNULL_BEGIN

#pragma mark - API -

/** Init KSCrash instance with custom base path. */
- (instancetype) initWithBasePath:(NSString*) basePath;
- (instancetype) init NS_UNAVAILABLE;
+ (instancetype) new NS_UNAVAILABLE;

/**
* 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that user would try to change it after the installation. Maybe better move it to KSCrashConfiguration as a compulsory init parameter?