From d6b709f294fa7b47c643f39740536c4c61936032 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Wed, 6 Sep 2023 17:03:58 -0400 Subject: [PATCH] Set default log level in `load` instead of getter (#23) --- AppCheckCore/Sources/Core/GACAppCheckLogger.m | 12 +++--- .../Tests/Unit/Core/GACAppCheckLoggerTests.m | 40 +++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 AppCheckCore/Tests/Unit/Core/GACAppCheckLoggerTests.m diff --git a/AppCheckCore/Sources/Core/GACAppCheckLogger.m b/AppCheckCore/Sources/Core/GACAppCheckLogger.m index ce3d4b8..6243a14 100644 --- a/AppCheckCore/Sources/Core/GACAppCheckLogger.m +++ b/AppCheckCore/Sources/Core/GACAppCheckLogger.m @@ -27,14 +27,12 @@ @implementation GACAppCheckLogger // Note: Declared as volatile to make getting and setting atomic. static volatile GACAppCheckLogLevel _logLevel; ++ (void)load { + // Set the default log level (warning). + _logLevel = GACAppCheckLogLevelWarning; +} + + (GACAppCheckLogLevel)logLevel { - static dispatch_once_t once; - dispatch_once(&once, ^{ - if (!_logLevel) { - // Set the default log level (warning) if not yet set. - _logLevel = GACAppCheckLogLevelWarning; - } - }); return _logLevel; } diff --git a/AppCheckCore/Tests/Unit/Core/GACAppCheckLoggerTests.m b/AppCheckCore/Tests/Unit/Core/GACAppCheckLoggerTests.m new file mode 100644 index 0000000..18cff08 --- /dev/null +++ b/AppCheckCore/Tests/Unit/Core/GACAppCheckLoggerTests.m @@ -0,0 +1,40 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@import XCTest; + +#import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckLogger.h" + +@interface GACAppCheckLoggerTests : XCTestCase +@end + +@implementation GACAppCheckLoggerTests + +- (void)testDefaultLogLevel { + GACAppCheckLogLevel defaultLogLevel = GACAppCheckLogger.logLevel; + + XCTAssertEqual(defaultLogLevel, GACAppCheckLogLevelWarning); +} + +- (void)testSetLogLevel { + GACAppCheckLogLevel expectedLogLevel = GACAppCheckLogLevelDebug; + + GACAppCheckLogger.logLevel = expectedLogLevel; + + XCTAssertEqual(GACAppCheckLogger.logLevel, expectedLogLevel); +} + +@end