diff --git a/RollbarNotifier/Sources/RollbarNotifier/DTOs/RollbarConfig.m b/RollbarNotifier/Sources/RollbarNotifier/DTOs/RollbarConfig.m index c9c60d24..3bbe26fc 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/DTOs/RollbarConfig.m +++ b/RollbarNotifier/Sources/RollbarNotifier/DTOs/RollbarConfig.m @@ -45,6 +45,7 @@ static NSString *const DFK_PERSON = @"person"; static NSString *const DFK_NOTIFIER = @"notifier"; static NSString *const DFK_TELEMETRY = @"telemetry"; +static NSString *const DFK_CRASH_REPORTING_ENABLED = @"crashReportingEnabled"; static NSString *const DFK_CUSTOM = @"custom"; #pragma mark - class implementation @@ -128,6 +129,7 @@ - (instancetype)initWithAccessToken:(nullable NSString *)token DFK_HTTPS_PROXY : [RollbarProxy new].jsonFriendlyData, DFK_DATA_SCRUBBER : [RollbarScrubbingOptions new].jsonFriendlyData, DFK_TELEMETRY : [RollbarTelemetryOptions new].jsonFriendlyData, + DFK_CRASH_REPORTING_ENABLED : [NSNumber numberWithBool:YES], DFK_NOTIFIER : [[RollbarModule alloc] initWithName:NOTIFIER_NAME version:NOTIFIER_VERSION] .jsonFriendlyData, @@ -152,6 +154,7 @@ - (instancetype)init { DFK_HTTPS_PROXY : [RollbarProxy new].jsonFriendlyData, DFK_DATA_SCRUBBER : [RollbarScrubbingOptions new].jsonFriendlyData, DFK_TELEMETRY : [RollbarTelemetryOptions new].jsonFriendlyData, + DFK_CRASH_REPORTING_ENABLED : [NSNumber numberWithBool:YES], DFK_NOTIFIER : [[RollbarModule alloc] initWithName:NOTIFIER_NAME version:NOTIFIER_VERSION] .jsonFriendlyData, @@ -233,6 +236,13 @@ - (RollbarTelemetryOptions *)telemetry { return [[RollbarTelemetryOptions alloc] initWithDictionary:data]; } +#pragma mark - Crash reporting + +- (BOOL)isCrashReportingEnabled { + NSNumber *result = [self safelyGetNumberByKey:DFK_CRASH_REPORTING_ENABLED]; + return result ? [result boolValue] : YES; +} + #pragma mark - Custom data - (NSDictionary *)customData { @@ -303,6 +313,7 @@ - (instancetype)init { @dynamic checkIgnoreRollbarData; @dynamic modifyRollbarData; +@dynamic isCrashReportingEnabled; - (void)setCheckIgnoreRollbarData: (BOOL (^)(RollbarData *_Nonnull))checkIgnoreRollbarData { @@ -427,6 +438,13 @@ - (void)setTelemetry:(RollbarTelemetryOptions *)value { [self setDataTransferObject:[value mutableCopy] forKey:DFK_TELEMETRY]; } +#pragma mark - Crash reporting + +- (void)setIsCrashReportingEnabled:(BOOL)value { + [self setNumber:[NSNumber numberWithBool:value] + forKey:DFK_CRASH_REPORTING_ENABLED]; +} + #pragma mark - Convenience Methods - (void)setPersonId:(nonnull NSString *)personId diff --git a/RollbarNotifier/Sources/RollbarNotifier/RollbarInfrastructure.m b/RollbarNotifier/Sources/RollbarNotifier/RollbarInfrastructure.m index 364f9ec0..56cb8d8c 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/RollbarInfrastructure.m +++ b/RollbarNotifier/Sources/RollbarNotifier/RollbarInfrastructure.m @@ -13,7 +13,7 @@ @interface RollbarInfrastructure () @property (readwrite, strong) id logger; @property (readwrite, strong) RollbarConfig *configuration; -@property (readwrite, strong) RollbarCrashCollector *collector; +@property (readwrite, strong, nullable) RollbarCrashCollector *collector; @end @implementation RollbarInfrastructure @@ -46,9 +46,13 @@ - (nonnull instancetype)configureWith:(nonnull RollbarConfig *)config { [[RollbarTelemetry sharedInstance] configureWithOptions:config.telemetry]; - self.collector = [[RollbarCrashCollector alloc] init]; - [self.collector install]; - [self.collector sendAllReports]; + if (config.isCrashReportingEnabled) { + self.collector = [[RollbarCrashCollector alloc] init]; + [self.collector install]; + [self.collector sendAllReports]; + } else { + self.collector = nil; + } // Create RollbarThread and begin processing persisted occurrences if ([[RollbarThread sharedInstance] active]) { diff --git a/RollbarNotifier/Sources/RollbarNotifier/include/RollbarConfig.h b/RollbarNotifier/Sources/RollbarNotifier/include/RollbarConfig.h index e0bd1470..5fe98261 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/include/RollbarConfig.h +++ b/RollbarNotifier/Sources/RollbarNotifier/include/RollbarConfig.h @@ -77,6 +77,9 @@ typedef RollbarData *_Nonnull(^RollbarModifyData)(RollbarData *rollbarData); /// Telemetry related settings @property (nonnull, nonatomic, readonly, strong) RollbarTelemetryOptions *telemetry; +/// Whether Rollbar should enable automatic crash reporting +@property (nonatomic, readonly) BOOL isCrashReportingEnabled; + #pragma mark - Custom data @property (nonatomic, readonly, strong) NSDictionary *customData; @@ -136,6 +139,9 @@ NS_DESIGNATED_INITIALIZER; /// Telemetry related settings @property (nonnull, nonatomic, readwrite, strong) RollbarMutableTelemetryOptions *telemetry; +/// Whether Rollbar should enable automatic crash reporting +@property (nonatomic, readwrite) BOOL isCrashReportingEnabled; + #pragma mark - Custom data @property (nonatomic, readwrite, strong) NSMutableDictionary *customData; diff --git a/RollbarNotifier/Tests/RollbarNotifierTests-ObjC/RollbarConfigTests.m b/RollbarNotifier/Tests/RollbarNotifierTests-ObjC/RollbarConfigTests.m index 96067447..6e760c66 100644 --- a/RollbarNotifier/Tests/RollbarNotifierTests-ObjC/RollbarConfigTests.m +++ b/RollbarNotifier/Tests/RollbarNotifierTests-ObjC/RollbarConfigTests.m @@ -24,6 +24,30 @@ - (void)tearDown { // Put teardown code here. This method is called after the invocation of each test method in the class. } +- (void)testCrashReportingEnabled { + + RollbarMutableConfig *mutable = [RollbarMutableConfig new]; + XCTAssertTrue(mutable.isCrashReportingEnabled); + + mutable.isCrashReportingEnabled = NO; + XCTAssertFalse(mutable.isCrashReportingEnabled); + + NSString *content = [mutable serializeToJSONString]; + XCTAssertNotNil(content); + XCTAssertTrue([content containsString:@"crashReportingEnabled"]); + + RollbarConfig *immutable = [mutable copy]; + XCTAssertNotNil(immutable); + XCTAssertFalse(immutable.isCrashReportingEnabled); + + RollbarMutableConfig *mutableCopy = [immutable mutableCopy]; + XCTAssertFalse(mutableCopy.isCrashReportingEnabled); + + RollbarConfig *configWithoutCrashReportingOption = + [[RollbarConfig alloc] initWithDictionary:@{}]; + XCTAssertTrue(configWithoutCrashReportingOption.isCrashReportingEnabled); +} + - (void)testRollbarDestination { NSString *defaultMutable = [[RollbarMutableDestination new] serializeToJSONString]; diff --git a/docs/SDK Intro.md b/docs/SDK Intro.md index aafd8001..c1fd3b6a 100644 --- a/docs/SDK Intro.md +++ b/docs/SDK Intro.md @@ -159,6 +159,25 @@ Rollbar.initWithConfiguration(config) //OR Rollbar.initWithConfiguration(config, crashCollector: crashCollector) ``` +Automatic crash reporting is enabled by default. To disable it while keeping +manual logging available, update the configuration before initializing Rollbar: + +#### Objective-C + +```Obj-C +RollbarMutableConfig *config = [RollbarMutableConfig new]; +config.isCrashReportingEnabled = NO; +[Rollbar initWithConfiguration:config]; +``` + +#### Swift + +```Swift +let config = RollbarMutableConfig() +config.isCrashReportingEnabled = false +Rollbar.initWithConfiguration(config) +``` + ### Start Logging using the Shared Notifier #### Objective-C