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

CBL-5396: Use os_log instead of NSLog for Console Log #3323

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
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
10 changes: 0 additions & 10 deletions CouchbaseLite.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1479,10 +1479,6 @@
9388CC3321C18672005CA66D /* CBLLog+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = 93C50F0F21C068F300C7E980 /* CBLLog+Internal.h */; };
9388CC3421C18673005CA66D /* CBLLog+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = 93C50F0F21C068F300C7E980 /* CBLLog+Internal.h */; };
9388CC3521C18674005CA66D /* CBLLog+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = 93C50F0F21C068F300C7E980 /* CBLLog+Internal.h */; };
9388CC3621C186DD005CA66D /* CBLLog+Admin.h in Headers */ = {isa = PBXBuildFile; fileRef = 274D6C272011529D00DAB7DD /* CBLLog+Admin.h */; };
9388CC3721C186DE005CA66D /* CBLLog+Admin.h in Headers */ = {isa = PBXBuildFile; fileRef = 274D6C272011529D00DAB7DD /* CBLLog+Admin.h */; };
9388CC3821C186DF005CA66D /* CBLLog+Admin.h in Headers */ = {isa = PBXBuildFile; fileRef = 274D6C272011529D00DAB7DD /* CBLLog+Admin.h */; };
9388CC3921C186DF005CA66D /* CBLLog+Admin.h in Headers */ = {isa = PBXBuildFile; fileRef = 274D6C272011529D00DAB7DD /* CBLLog+Admin.h */; };
9388CC3A21C18784005CA66D /* CBLLog.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9388CBE721BF7195005CA66D /* CBLLog.mm */; };
9388CC3B21C18786005CA66D /* CBLLog.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9388CBE721BF7195005CA66D /* CBLLog.mm */; };
9388CC3C21C18787005CA66D /* CBLLog.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9388CBE721BF7195005CA66D /* CBLLog.mm */; };
Expand Down Expand Up @@ -2224,7 +2220,6 @@
270AB2BB2073EF57009A4596 /* CBLChangeNotifier.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CBLChangeNotifier.m; sourceTree = "<group>"; };
2728509C1E99CA4D009CA22F /* CBLReplicator+Internal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "CBLReplicator+Internal.h"; path = "../CBLReplicator+Internal.h"; sourceTree = "<group>"; };
27476651201912B5007B39D1 /* CBLErrors.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CBLErrors.h; sourceTree = "<group>"; };
274D6C272011529D00DAB7DD /* CBLLog+Admin.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "CBLLog+Admin.h"; sourceTree = "<group>"; };
275229C51E776BC100E630FA /* CBLReplicator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CBLReplicator.h; sourceTree = "<group>"; };
275229C61E776BC100E630FA /* CBLReplicator.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CBLReplicator.mm; sourceTree = "<group>"; };
2753AFF11EC39CA200C12E98 /* CBLHTTPLogic.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CBLHTTPLogic.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3932,7 +3927,6 @@
9388CC1021BF9E54005CA66D /* Log */ = {
isa = PBXGroup;
children = (
274D6C272011529D00DAB7DD /* CBLLog+Admin.h */,
93C50F0F21C068F300C7E980 /* CBLLog+Internal.h */,
934F4C9C1E241FB500F90659 /* CBLLog+Logging.h */,
9388CC5721C25FDE005CA66D /* CBLLog+Swift.h */,
Expand Down Expand Up @@ -4419,7 +4413,6 @@
1AAFB66C284A260A00878453 /* CBLCollectionChange.h in Headers */,
9388CC0621BF750E005CA66D /* CBLFileLogger.h in Headers */,
9381960B1EC112010032CC51 /* CBLMutableDictionary.h in Headers */,
9388CC3721C186DE005CA66D /* CBLLog+Admin.h in Headers */,
9381960D1EC112130032CC51 /* CBLArray.h in Headers */,
93CED8D420488DC400E6F0A4 /* CBLBlob+Swift.h in Headers */,
9381961F1EC11A8C0032CC51 /* CBLDictionary+Swift.h in Headers */,
Expand Down Expand Up @@ -4700,7 +4693,6 @@
40FC1C0B2B928ADC00394276 /* CBLListenerPasswordAuthenticator+Internal.h in Headers */,
9343EFF7207D611600F19A89 /* CBLQueryCollation.h in Headers */,
9343EFF8207D611600F19A89 /* CBLArrayFragment.h in Headers */,
9388CC3821C186DF005CA66D /* CBLLog+Admin.h in Headers */,
9343EFF9207D611600F19A89 /* CBLIndex.h in Headers */,
935A58D021AFAD31009A29CB /* CBLDocumentReplication+Internal.h in Headers */,
9343EFFB207D611600F19A89 /* CBLData.h in Headers */,
Expand Down Expand Up @@ -4909,7 +4901,6 @@
9343F116207D61AB00F19A89 /* CBLReplicator+Internal.h in Headers */,
9343F117207D61AB00F19A89 /* CBLC4Document.h in Headers */,
9343F118207D61AB00F19A89 /* CBLJSON.h in Headers */,
9388CC3921C186DF005CA66D /* CBLLog+Admin.h in Headers */,
9343F119207D61AB00F19A89 /* CBLCoreBridge.h in Headers */,
935A58D121AFAD31009A29CB /* CBLDocumentReplication+Internal.h in Headers */,
1A3470EC266F69280042C6BA /* CBLIndexConfiguration+Internal.h in Headers */,
Expand Down Expand Up @@ -5020,7 +5011,6 @@
9388CC0521BF750E005CA66D /* CBLFileLogger.h in Headers */,
9398D9FF1E03531A00464432 /* CouchbaseLite.h in Headers */,
1A3470C1266F3E7C0042C6BA /* CBLIndexConfiguration.h in Headers */,
9388CC3621C186DD005CA66D /* CBLLog+Admin.h in Headers */,
6932D48D2954640000D28C18 /* CBLQueryFullTextIndexExpression.h in Headers */,
937F01E11EFB269300060D64 /* CBLAuthenticator.h in Headers */,
934F4CB51E241FB500F90659 /* CBLPrefix.h in Headers */,
Expand Down
53 changes: 49 additions & 4 deletions Objective-C/CBLConsoleLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@

#import "CBLConsoleLogger.h"
#import "CBLLog+Internal.h"
#import "CBLLog+Admin.h"

@implementation CBLConsoleLogger

@synthesize level=_level, domains=_domains;

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;
[self initializeOSLogDomains];
}
return self;
}
Expand All @@ -43,9 +46,51 @@ - (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("com.couchbase.lite.ios", "Database");
velicuvlad marked this conversation as resolved.
Show resolved Hide resolved
osLogDictionary[@(kCBLLogDomainQuery)] = os_log_create("com.couchbase.lite.ios", "Query");
osLogDictionary[@(kCBLLogDomainReplicator)] = os_log_create("com.couchbase.lite.ios", "Replicator");
osLogDictionary[@(kCBLLogDomainNetwork)] = os_log_create("com.couchbase.lite.ios", "Network");

#ifdef COUCHBASE_ENTERPRISE
osLogDictionary[@(kCBLLogDomainListener)] = os_log_create("com.couchbase.lite.ios", "Listener");
#endif
}

+ (os_log_t) internalLogger {
if(!logger){
logger = os_log_create("com.couchbase.lite.ios", "Log");
velicuvlad marked this conversation as resolved.
Show resolved Hide resolved
}
return logger;
}

+ (void) logWithInternal: (NSString*)message {
logger = [self internalLogger];
os_log(logger, "%@", message);
}

@end
3 changes: 1 addition & 2 deletions Objective-C/CBLDatabase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#import "CBLIndexConfiguration+Internal.h"
#import "CBLIndexSpec.h"
#import "CBLIndex+Internal.h"
#import "CBLLog+Admin.h"
#import "CBLLog+Internal.h"
#import "CBLMisc.h"
#import "CBLQuery+Internal.h"
Expand Down Expand Up @@ -115,7 +114,7 @@ to be done in CBLInit()which is called only once when the first CBLDatabase is c
*/
+ (void) initialize {
if (self == [CBLDatabase class]) {
NSLog(@"%@", [CBLVersion userAgent]);
[CBLConsoleLogger logWithInternal:[CBLVersion userAgent]];
// Initialize logging
CBLAssertNotNil(CBLLog.sharedInstance);
}
Expand Down
35 changes: 4 additions & 31 deletions Objective-C/CBLLog.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
//

#import "CBLLog.h"
#import "CBLLog+Admin.h"
#import "CBLLog+Internal.h"
#import "CBLLog+Logging.h"
#import "CBLLog+Swift.h"
Expand All @@ -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;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it use CBLConsoleLogger's logAlways as well?

}
}
#endif
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 0 additions & 36 deletions Objective-C/Internal/CBLLog+Admin.h

This file was deleted.

5 changes: 5 additions & 0 deletions Objective-C/Internal/CBLLog+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// limitations under the License.
//

#import <os/log.h>
#import "CBLLog.h"
#import "CBLConsoleLogger.h"
#import "CBLFileLogger.h"
Expand All @@ -35,6 +36,10 @@ NS_ASSUME_NONNULL_BEGIN

- (instancetype) initWithLogLevel: (CBLLogLevel)level;

+ (os_log_t) internalLogger;

+ (void) logWithInternal: (nonnull NSString*)message;

@end

@interface CBLFileLogger ()
Expand Down
5 changes: 1 addition & 4 deletions Objective-C/Tests/Util/CBLWordEmbeddingModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ - (CBLArray*) vectorForWord: (NSString*)word collection: (NSString*)collection {
- (CBLDictionary*) predict: (CBLDictionary*)input {
NSString* inputWord = [input stringForKey: @"word"];

if (!inputWord) {
NSLog(@"No word input !!!");
return nil;
}
assert(inputWord);

CBLArray* result = [self vectorForWord: inputWord collection: @"words"];
if (!result) {
Expand Down
Loading