-
Notifications
You must be signed in to change notification settings - Fork 301
CBL-5396: Use os_log instead of NSLog for Console Log #3323
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
base: master
Are you sure you want to change the base?
Changes from 7 commits
ed9ecfb
9a03db9
a906642
6895ecd
c139662
4e50ac7
90a540a
e7defe8
96a48a5
04526ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,17 +19,21 @@ | |
|
|
||
| #import "CBLConsoleLogger.h" | ||
| #import "CBLLog+Internal.h" | ||
| #import "CBLLog+Admin.h" | ||
|
|
||
| @implementation CBLConsoleLogger | ||
|
|
||
| @synthesize level=_level, domains=_domains; | ||
| @synthesize level=_level, domains=_domains, sysID=_sysID; | ||
|
|
||
| static NSMutableDictionary<NSNumber *, os_log_t>* osLogDictionary; | ||
| static os_log_t logger; | ||
|
|
||
| - (instancetype) initWithLogLevel: (CBLLogLevel)level { | ||
| self = [super init]; | ||
| if (self) { | ||
| _level = level; | ||
| _domains = kCBLLogDomainAll; | ||
| _sysID = [[NSBundle mainBundle] bundleIdentifier]; | ||
| [self initializeOSLogDomains]; | ||
| } | ||
| return self; | ||
| } | ||
|
|
@@ -43,9 +47,52 @@ - (void) logWithLevel: (CBLLogLevel)level domain: (CBLLogDomain)domain message: | |
| if (self.level > level || (self.domains & domain) == 0) | ||
| return; | ||
|
|
||
| NSString* levelName = CBLLog_GetLevelName(level); | ||
| NSString* domainName = CBLLog_GetDomainName(domain); | ||
| NSLog(@"CouchbaseLite %@ %@: %@", domainName, levelName, message); | ||
| os_log_t osLogDomain = osLogDictionary[@(domain)]; | ||
| os_log_type_t osLogType = osLogTypeForLevel(level); | ||
| os_log_with_type(osLogDomain, osLogType, "%@", message); | ||
| } | ||
|
|
||
| static os_log_type_t osLogTypeForLevel(CBLLogLevel level) { | ||
| switch (level) { | ||
| case kCBLLogLevelDebug: | ||
| return OS_LOG_TYPE_DEBUG; | ||
| case kCBLLogLevelVerbose: | ||
| return OS_LOG_TYPE_INFO; // Map verbose to info | ||
| case kCBLLogLevelInfo: | ||
| return OS_LOG_TYPE_INFO; | ||
| case kCBLLogLevelWarning: | ||
| return OS_LOG_TYPE_ERROR; // Map warning to error | ||
| case kCBLLogLevelError: | ||
| return OS_LOG_TYPE_ERROR; | ||
| default: | ||
| return OS_LOG_TYPE_DEFAULT; // Default log type | ||
| } | ||
| } | ||
|
|
||
| - (void) initializeOSLogDomains { | ||
| osLogDictionary = [NSMutableDictionary dictionary]; | ||
|
|
||
| osLogDictionary[@(kCBLLogDomainDatabase)] = os_log_create([self.sysID UTF8String], "Database"); | ||
| osLogDictionary[@(kCBLLogDomainQuery)] = os_log_create([self.sysID UTF8String], "Query"); | ||
| osLogDictionary[@(kCBLLogDomainReplicator)] = os_log_create([self.sysID UTF8String], "Replicator"); | ||
| osLogDictionary[@(kCBLLogDomainNetwork)] = os_log_create([self.sysID UTF8String], "Network"); | ||
|
|
||
| #ifdef COUCHBASE_ENTERPRISE | ||
| osLogDictionary[@(kCBLLogDomainListener)] = os_log_create([self.sysID UTF8String], "Listener"); | ||
| #endif | ||
| } | ||
|
|
||
| + (os_log_t) internalLogger { | ||
| static dispatch_once_t onceToken; | ||
| dispatch_once(&onceToken, ^{ | ||
| logger = os_log_create("com.couchbase.lite.ios", "Internal"); | ||
|
||
| }); | ||
| return logger; | ||
| } | ||
|
|
||
| + (void) logWithInternal: (NSString*)message { | ||
| logger = [self internalLogger]; | ||
| os_log(logger, "%@", message); | ||
| } | ||
|
|
||
| @end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,6 @@ | |
| // | ||
|
|
||
| #import "CBLLog.h" | ||
| #import "CBLLog+Admin.h" | ||
| #import "CBLLog+Internal.h" | ||
| #import "CBLLog+Logging.h" | ||
| #import "CBLLog+Swift.h" | ||
|
|
@@ -44,6 +43,7 @@ - (instancetype) initWithLevel: (CBLLogLevel)level logger: (CBLCustomLoggerBlock | |
|
|
||
| @implementation CBLLog { | ||
| CBLLogLevel _callbackLogLevel; | ||
| os_log_t oslogger; | ||
| } | ||
|
|
||
| @synthesize console=_console, file=_file, custom=_custom; | ||
|
|
@@ -159,7 +159,8 @@ - (instancetype) initWithDefault { | |
| callbackLogLevel = string2level(userLogLevel); | ||
| } | ||
| if (callbackLogLevel != kC4LogWarning) { | ||
| NSLog(@"CouchbaseLite minimum log level is %s", kLevelNames[callbackLogLevel]); | ||
| NSString* message = [NSString stringWithFormat: @"CouchbaseLite minimum log level is %s", kLevelNames[callbackLogLevel]]; | ||
| [CBLConsoleLogger logWithInternal: message]; | ||
| } | ||
| #endif | ||
|
|
||
|
|
@@ -190,7 +191,7 @@ - (instancetype) initWithDefault { | |
| C4LogDomain domain = c4log_getDomain(domainName, true); | ||
| C4LogLevel level = string2level(defaults[key]); | ||
| c4log_setLevel(domain, level); | ||
| NSLog(@"CouchbaseLite logging to %s domain at level %s", domainName, kLevelNames[level]); | ||
| os_log(oslogger, "CouchbaseLite logging to %s domain at level %s", domainName, kLevelNames[level]); | ||
|
||
| } | ||
| } | ||
| #endif | ||
|
|
@@ -283,34 +284,6 @@ void cblLog(C4LogDomain domain, C4LogLevel level, NSString *msg, ...) { | |
| sendToCallbackLogger(domain, level, nsmsg); | ||
| } | ||
|
|
||
| NSString* CBLLog_GetLevelName(CBLLogLevel level) { | ||
| return [NSString stringWithUTF8String: kLevelNames[level]]; | ||
| } | ||
|
|
||
| NSString* CBLLog_GetDomainName(CBLLogDomain domain) { | ||
| switch (domain) { | ||
| case kCBLLogDomainDatabase: | ||
| return @"Database"; | ||
| break; | ||
| case kCBLLogDomainQuery: | ||
| return @"Query"; | ||
| break; | ||
| case kCBLLogDomainReplicator: | ||
| return @"Replicator"; | ||
| break; | ||
| case kCBLLogDomainNetwork: | ||
| return @"Network"; | ||
| break; | ||
| #ifdef COUCHBASE_ENTERPRISE | ||
| case kCBLLogDomainListener: | ||
| return @"Listener"; | ||
| break; | ||
| #endif | ||
| default: | ||
| return @"Database"; | ||
| } | ||
| } | ||
|
|
||
| @implementation CBLCustomLogger { | ||
| CBLLogLevel _level; | ||
| CBLCustomLoggerBlock _logger; | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is right as it think it will return the bundle identifier of the app not the framework. What is the value when you run the test? If this returns bundle id of the app, you may try to get the bundle id from one of the classes such as CBLConsoleLogger itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right, will fix