Skip to content

Commit

Permalink
Use OSLog instead of asl as GULLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
ls-todd-lunter committed May 20, 2024
1 parent 56577f6 commit 98f8243
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 39 deletions.
55 changes: 24 additions & 31 deletions GoogleUtilities/Logger/GULLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#import "GoogleUtilities/Logger/Public/GoogleUtilities/GULLogger.h"

#include <asl.h>
#import <OSLog/OSLog.h>

#import "GoogleUtilities/Environment/Public/GoogleUtilities/GULAppEnvironmentUtil.h"
#import "GoogleUtilities/Logger/Public/GoogleUtilities/GULLoggerLevel.h"
Expand All @@ -24,7 +24,7 @@

static dispatch_once_t sGULLoggerOnceToken;

static aslclient sGULLoggerClient;
static os_log_t sLogObject;

static dispatch_queue_t sGULClientQueue;

Expand All @@ -45,29 +45,10 @@

void GULLoggerInitializeASL(void) {
dispatch_once(&sGULLoggerOnceToken, ^{
NSInteger majorOSVersion = [[GULAppEnvironmentUtil systemVersion] integerValue];
uint32_t aslOptions = ASL_OPT_STDERR;
#if TARGET_OS_SIMULATOR
// The iOS 11 simulator doesn't need the ASL_OPT_STDERR flag.
if (majorOSVersion >= 11) {
aslOptions = 0;
}
#else
// Devices running iOS 10 or higher don't need the ASL_OPT_STDERR flag.
if (majorOSVersion >= 10) {
aslOptions = 0;
}
#endif // TARGET_OS_SIMULATOR

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations" // asl is deprecated
// Initialize the ASL client handle.
sGULLoggerClient = asl_open(NULL, kGULLoggerASLClientFacilityName, aslOptions);
// Initialize the OSLog custom object
sLogObject = os_log_create(kGULLoggerASLClientFacilityName, "GULLogger");
sGULLoggerMaximumLevel = GULLoggerLevelNotice;

// Set the filter used by system/device log. Initialize in default mode.
asl_set_filter(sGULLoggerClient, ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE));

sGULClientQueue = dispatch_queue_create("GULLoggingClientQueue", DISPATCH_QUEUE_SERIAL);
dispatch_set_target_queue(sGULClientQueue,
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0));
Expand All @@ -80,7 +61,6 @@ void GULLoggerInitializeASL(void) {
}

void GULLoggerEnableSTDERR(void) {
asl_add_log_file(sGULLoggerClient, STDERR_FILENO);
}

void GULLoggerForceDebug(void) {
Expand Down Expand Up @@ -108,9 +88,6 @@ GULLoggerLevel GULGetLoggerLevel(void) {
}

sGULLoggerMaximumLevel = loggerLevel;
dispatch_async(sGULClientQueue, ^{
asl_set_filter(sGULLoggerClient, ASL_FILTER_MASK_UPTO(loggerLevel));
});
}

/**
Expand All @@ -131,8 +108,8 @@ void GULResetLogger(void) {
sGULLoggerMaximumLevel = GULLoggerLevelNotice;
}

aslclient getGULLoggerClient(void) {
return sGULLoggerClient;
void* getGULLoggerClient(void) {
return nil;
}

dispatch_queue_t getGULClientQueue(void) {
Expand All @@ -148,6 +125,23 @@ void GULLoggerRegisterVersion(NSString *version) {
sVersion = version;
}

os_log_type_t convertLoggerLevel(GULLoggerLevel level) {
switch (level) {
case GULLoggerLevelDebug:
return OS_LOG_TYPE_DEBUG;
case GULLoggerLevelInfo:
return OS_LOG_TYPE_INFO;
case GULLoggerLevelNotice:
return OS_LOG_TYPE_DEFAULT;
case GULLoggerLevelWarning:
return OS_LOG_TYPE_DEFAULT;
case GULLoggerLevelError:
return OS_LOG_TYPE_ERROR;
default:
return OS_LOG_TYPE_DEFAULT;
}
}

void GULLogBasic(GULLoggerLevel level,
GULLoggerService service,
BOOL forceLog,
Expand All @@ -174,10 +168,9 @@ void GULLogBasic(GULLoggerLevel level,
}
logMsg = [NSString stringWithFormat:@"%@ - %@[%@] %@", sVersion, service, messageCode, logMsg];
dispatch_async(sGULClientQueue, ^{
asl_log(sGULLoggerClient, NULL, (int)level, "%s", logMsg.UTF8String);
os_log_with_type(sLogObject, convertLoggerLevel(level), "%@", logMsg);
});
}
#pragma clang diagnostic pop

/**
* Generates the logging functions using macros.
Expand Down
16 changes: 8 additions & 8 deletions GoogleUtilities/Tests/Unit/Logger/GULLoggerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@

#import "GoogleUtilities/Logger/Public/GoogleUtilities/GULLogger.h"

#import <asl.h>
#import <OSLog/OSLog.h>

extern const char *kGULLoggerASLClientFacilityName;

extern void GULResetLogger(void);

extern aslclient getGULLoggerClient(void);

extern dispatch_queue_t getGULClientQueue(void);

extern BOOL getGULLoggerDebugMode(void);

extern os_log_type_t convertLoggerLevel(GULLoggerLevel);

static NSString *const kMessageCode = @"I-COR000001";

@interface GULLoggerTest : XCTestCase
Expand Down Expand Up @@ -110,11 +110,11 @@ - (void)testLoggerInterface {
// them in GULLoggerLevel.h since we cannot include <asl.h> (see b/34976089 for more details).
// This test ensures the constants match.
- (void)testGULLoggerLevelValues {
XCTAssertEqual(GULLoggerLevelError, ASL_LEVEL_ERR);
XCTAssertEqual(GULLoggerLevelWarning, ASL_LEVEL_WARNING);
XCTAssertEqual(GULLoggerLevelNotice, ASL_LEVEL_NOTICE);
XCTAssertEqual(GULLoggerLevelInfo, ASL_LEVEL_INFO);
XCTAssertEqual(GULLoggerLevelDebug, ASL_LEVEL_DEBUG);
XCTAssertEqual(convertLoggerLevel(GULLoggerLevelError), OS_LOG_TYPE_ERROR);
XCTAssertEqual(convertLoggerLevel(GULLoggerLevelWarning), OS_LOG_TYPE_DEFAULT);
XCTAssertEqual(convertLoggerLevel(GULLoggerLevelNotice), OS_LOG_TYPE_DEFAULT);
XCTAssertEqual(convertLoggerLevel(GULLoggerLevelInfo), OS_LOG_TYPE_INFO);
XCTAssertEqual(convertLoggerLevel(GULLoggerLevelDebug), OS_LOG_TYPE_DEBUG);
}

- (void)testGULGetLoggerLevel {
Expand Down

0 comments on commit 98f8243

Please sign in to comment.