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

[Core] Add logging level getter to FIRConfiguration #13815

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion FirebaseCore/Sources/FIRConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#import "FirebaseCore/Sources/FIRAnalyticsConfiguration.h"

extern void FIRSetLoggerLevel(FIRLoggerLevel loggerLevel);
extern FIRLoggerLevel FIRGetLoggerLevel(void);

@interface FIRConfiguration ()
@property(nonatomic, readonly) dispatch_queue_t queue;
@end

@implementation FIRConfiguration

Expand All @@ -32,6 +37,7 @@ + (instancetype)sharedInstance {
- (instancetype)init {
self = [super init];
if (self) {
_queue = dispatch_queue_create("com.firebase.FIRConfiguration", DISPATCH_QUEUE_SERIAL);
_analyticsConfiguration = [FIRAnalyticsConfiguration sharedInstance];
}
return self;
Expand All @@ -40,7 +46,17 @@ - (instancetype)init {
- (void)setLoggerLevel:(FIRLoggerLevel)loggerLevel {
NSAssert(loggerLevel <= FIRLoggerLevelMax && loggerLevel >= FIRLoggerLevelMin,
@"Invalid logger level, %ld", (long)loggerLevel);
FIRSetLoggerLevel(loggerLevel);
dispatch_sync(self.queue, ^{
FIRSetLoggerLevel(loggerLevel);
});
}

- (FIRLoggerLevel)loggerLevel {
__block FIRLoggerLevel level;
dispatch_sync(self.queue, ^{
level = FIRGetLoggerLevel();
});
return level;
}

@end
1 change: 1 addition & 0 deletions FirebaseCore/Sources/FIRLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void FIRLoggerInitialize(void) {
}

FIRLoggerLevel FIRGetLoggerLevel(void) {
FIRLoggerInitialize();
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved
return (FIRLoggerLevel)GULGetLoggerLevel();
}

Expand Down
3 changes: 3 additions & 0 deletions FirebaseCore/Sources/Public/FirebaseCore/FIRConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ NS_SWIFT_NAME(FirebaseConfiguration)
*/
- (void)setLoggerLevel:(FIRLoggerLevel)loggerLevel;

/// Returns the logging level for internal Firebase logging.
- (FIRLoggerLevel)loggerLevel;

@end

NS_ASSUME_NONNULL_END
43 changes: 43 additions & 0 deletions FirebaseCore/Tests/Unit/FIRConfigurationTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,47 @@ - (void)testSharedInstance {
XCTAssertNotNil(config.analyticsConfiguration);
}

- (void)testGetDefaultLevel {
FIRConfiguration *config = [FIRConfiguration sharedInstance];
FIRLoggerLevel defaultLevel = [config loggerLevel];
XCTAssertEqual(defaultLevel, FIRLoggerLevelNotice);
}

- (void)testSetAndGet {
FIRConfiguration *config = [FIRConfiguration sharedInstance];
for (FIRLoggerLevel level = FIRLoggerLevelMin; level <= FIRLoggerLevelMax; level++) {
[config setLoggerLevel:level];
XCTAssertEqual([config loggerLevel], level);
}
}

- (void)testSynchronizedAccess {
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved
FIRConfiguration *config = [FIRConfiguration sharedInstance];

__block BOOL raceConditionExists = NO;

// Dispatch to a concurrent queue to simulate concurrent access.
dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0);
for (int i = 0; i < 1000000; i++) {
dispatch_async(queue, ^{
// Get the current logging level.
FIRLoggerLevel currentLevel = [config loggerLevel];

// Set a new logging level.
FIRLoggerLevel nextLevel =
(currentLevel + 1 > FIRLoggerLevelMax) ? FIRLoggerLevelMin : currentLevel + 1;
[config setLoggerLevel:nextLevel];

// Confirm that the new logging level is retrieved.
if (nextLevel != [config loggerLevel]) {
raceConditionExists = YES;
}
});
}

dispatch_sync(queue, ^{/* Drain queue before proceeding. */});

XCTAssertEqual(raceConditionExists, NO);
}

@end
Loading