Skip to content

Commit

Permalink
[App Check] Replace gGACAppCheckLogLevel with class property (#11572)
Browse files Browse the repository at this point in the history
Replaced the global variable gGACAppCheckLogLevel with a class property on a new GACAppCheckLogger class.
  • Loading branch information
andrewheard committed Jul 20, 2023
1 parent 41c3bfd commit 24427dd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
17 changes: 15 additions & 2 deletions AppCheck/Sources/Core/GACAppCheckLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@

#pragma mark - Public

volatile NSInteger gGACAppCheckLogLevel = GACAppCheckLogLevelError;
@implementation GACAppCheckLogger

// Note: Declared as volatile to make getting and setting atomic.
static volatile GACAppCheckLogLevel _logLevel;

+ (GACAppCheckLogLevel)logLevel {
return _logLevel;
}

+ (void)setLogLevel:(GACAppCheckLogLevel)logLevel {
_logLevel = logLevel;
}

@end

#pragma mark - Helpers

Expand Down Expand Up @@ -59,7 +72,7 @@
void GACAppCheckLog(GACAppCheckMessageCode code, GACAppCheckLogLevel logLevel, NSString *message) {
// Don't log anything in not debug builds.
#if !NDEBUG
if (logLevel >= gGACAppCheckLogLevel) {
if (logLevel >= GACAppCheckLogger.logLevel) {
NSLog(@"<%@> [AppCheckCore][%@] %@", GACAppCheckLoggerLevelEnumToString(logLevel),
GACAppCheckMessageCodeEnumToString(code), message);
}
Expand Down
21 changes: 13 additions & 8 deletions AppCheck/Sources/Public/AppCheck/GACAppCheckLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@

#import "GACAppCheckErrors.h"

/// The current logging level.
///
/// Messages with levels equal to or higher priority than `GACAppCheckLogLevel` will be printed,
/// where Fault > Error > Warning > Info > Debug.
///
/// Note: Declared as volatile to make getting and setting atomic.
FOUNDATION_EXPORT volatile NSInteger gGACAppCheckLogLevel;

/// Constants that specify the level of logging to perform in App Check Core.
typedef NS_ENUM(NSInteger, GACAppCheckLogLevel) {
/// The debug log level; equivalent to `OS_LOG_TYPE_DEBUG`.
Expand All @@ -37,3 +29,16 @@ typedef NS_ENUM(NSInteger, GACAppCheckLogLevel) {
/// The fault log level; equivalent to `OS_LOG_TYPE_FAULT`.
GACAppCheckLogLevelFault = 5
} NS_SWIFT_NAME(AppCheckCoreLogLevel);

NS_SWIFT_NAME(AppCheckCoreLogger)
@interface GACAppCheckLogger : NSObject

/// The current logging level.
///
/// Messages with levels equal to or higher priority than `logLevel` will be printed, where
/// Fault > Error > Warning > Info > Debug.
@property(class, atomic, assign) GACAppCheckLogLevel logLevel;

- (instancetype)init NS_UNAVAILABLE;

@end
5 changes: 5 additions & 0 deletions AppCheck/Tests/Unit/Swift/AppCheckAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ final class AppCheckAPITests {
}
}
#endif // !os(watchOS)

// MARK: - AppCheckCoreLogger

// Set the log level for App Check Core
AppCheckCoreLogger.logLevel = .debug
}
}

Expand Down

0 comments on commit 24427dd

Please sign in to comment.