Skip to content

Commit

Permalink
CBL-6456 : Implement Swift LogSink API
Browse files Browse the repository at this point in the history
* Implemented Swift LogSink API.

* Fixed using out-of-scope CBLStringBytes when setting directory and header to C4 API for CBLFileLogSink ; Added an option to CBLStringBytes to disable inline storage.

* Ran the script to generate public symbol for the logging API.

* Renamed domain property to domains in CBLConsoleLogSink and CBLCustomLogSink.

* Changed maxKepthFiles to NSInteger and maxFileSize to long long in CBLFileLogSink.

* Updated and completed missing API docs in Objective-C LogSink API.
  • Loading branch information
pasin committed Jan 30, 2025
1 parent b68ef78 commit a12ee08
Show file tree
Hide file tree
Showing 41 changed files with 1,109 additions and 385 deletions.
90 changes: 64 additions & 26 deletions CouchbaseLite.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Objective-C/CBLConsoleLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ - (CBLLogDomain) domains {
}

- (void) updateConsoleLogSink {
CBLConsoleLogSink* sink =[[CBLConsoleLogSink alloc] initWithLevel: _level domain: _domains];
CBLConsoleLogSink* sink = [[CBLConsoleLogSink alloc] initWithLevel: _level domains: _domains];
sink.version = kCBLLogAPIOld;
CBLLogSinks.console = sink;

Expand Down
2 changes: 1 addition & 1 deletion Objective-C/CBLDefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extern const NSInteger kCBLDefaultLogFileMaxRotateCount;
extern const BOOL kCBLDefaultFileLogSinkUsePlaintext;

/** [524288] 512 KiB for the size of a log file */
extern const uint64_t kCBLDefaultFileLogSinkMaxSize;
extern const long long kCBLDefaultFileLogSinkMaxSize;

/** [2] 2 files preserved during each log rotation */
extern const NSInteger kCBLDefaultFileLogSinkMaxKeptFiles;
Expand Down
2 changes: 1 addition & 1 deletion Objective-C/CBLDefaults.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

const BOOL kCBLDefaultFileLogSinkUsePlaintext = NO;

const uint64_t kCBLDefaultFileLogSinkMaxSize = 524288;
const long long kCBLDefaultFileLogSinkMaxSize = 524288;

const NSInteger kCBLDefaultFileLogSinkMaxKeptFiles = 2;

Expand Down
4 changes: 2 additions & 2 deletions Objective-C/CBLLog.mm
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ - (instancetype) initWithLogger: (id<CBLLogger>)logger {
return self;
}

- (void) writeLogWithLevel: (CBLLogLevel)level domain: (CBLLogDomain)domain message: (NSString*)message {
[_logger logWithLevel: level domain: domain message: message];
- (void) writeLogWithLevel: (CBLLogLevel)level domain: (CBLLogDomain)domains message: (NSString*)message {
[_logger logWithLevel: level domain: domains message: message];
}

@end
7 changes: 7 additions & 0 deletions Objective-C/Exports/Generated/CBL.exp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
.objc_class_name_CBLCollectionConfiguration
.objc_class_name_CBLConflict
.objc_class_name_CBLConflictResolver
.objc_class_name_CBLConsoleLogSink
.objc_class_name_CBLConsoleLogger
.objc_class_name_CBLCustomLogSink
.objc_class_name_CBLDatabase
.objc_class_name_CBLDatabaseChange
.objc_class_name_CBLDatabaseConfiguration
Expand All @@ -19,6 +21,7 @@
.objc_class_name_CBLDocumentChange
.objc_class_name_CBLDocumentFragment
.objc_class_name_CBLDocumentReplication
.objc_class_name_CBLFileLogSink
.objc_class_name_CBLFileLogger
.objc_class_name_CBLFragment
.objc_class_name_CBLFullTextIndex
Expand All @@ -29,6 +32,7 @@
.objc_class_name_CBLIndexConfiguration
.objc_class_name_CBLLog
.objc_class_name_CBLLogFileConfiguration
.objc_class_name_CBLLogSinks
.objc_class_name_CBLMutableArray
.objc_class_name_CBLMutableDictionary
.objc_class_name_CBLMutableDocument
Expand Down Expand Up @@ -75,8 +79,11 @@ _kCBLDefaultCollectionName
_kCBLDefaultDatabaseFullSync
_kCBLDefaultDatabaseMmapEnabled
_kCBLDefaultFileLogSinkMaxKeptFiles
_kCBLDefaultFileLogSinkMaxKeptFiles
_kCBLDefaultFileLogSinkMaxSize
_kCBLDefaultFileLogSinkMaxSize
_kCBLDefaultFileLogSinkUsePlaintext
_kCBLDefaultFileLogSinkUsePlaintext
_kCBLDefaultFullTextIndexIgnoreAccents
_kCBLDefaultLogFileMaxRotateCount
_kCBLDefaultLogFileMaxSize
Expand Down
7 changes: 7 additions & 0 deletions Objective-C/Exports/Generated/CBL_EE.exp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
.objc_class_name_CBLCollectionConfiguration
.objc_class_name_CBLConflict
.objc_class_name_CBLConflictResolver
.objc_class_name_CBLConsoleLogSink
.objc_class_name_CBLConsoleLogger
.objc_class_name_CBLCoreMLPredictiveModel
.objc_class_name_CBLCustomLogSink
.objc_class_name_CBLDatabase
.objc_class_name_CBLDatabaseChange
.objc_class_name_CBLDatabaseConfiguration
Expand All @@ -24,6 +26,7 @@
.objc_class_name_CBLDocumentReplication
.objc_class_name_CBLEncryptionKey
.objc_class_name_CBLExtension
.objc_class_name_CBLFileLogSink
.objc_class_name_CBLFileLogger
.objc_class_name_CBLFragment
.objc_class_name_CBLFullTextIndex
Expand All @@ -37,6 +40,7 @@
.objc_class_name_CBLListenerPasswordAuthenticator
.objc_class_name_CBLLog
.objc_class_name_CBLLogFileConfiguration
.objc_class_name_CBLLogSinks
.objc_class_name_CBLMessage
.objc_class_name_CBLMessageEndpoint
.objc_class_name_CBLMessageEndpointListener
Expand Down Expand Up @@ -113,8 +117,11 @@ _kCBLDefaultCollectionName
_kCBLDefaultDatabaseFullSync
_kCBLDefaultDatabaseMmapEnabled
_kCBLDefaultFileLogSinkMaxKeptFiles
_kCBLDefaultFileLogSinkMaxKeptFiles
_kCBLDefaultFileLogSinkMaxSize
_kCBLDefaultFileLogSinkMaxSize
_kCBLDefaultFileLogSinkUsePlaintext
_kCBLDefaultFileLogSinkUsePlaintext
_kCBLDefaultFullTextIndexIgnoreAccents
_kCBLDefaultListenerDisableTls
_kCBLDefaultListenerEnableDeltaSync
Expand Down
2 changes: 0 additions & 2 deletions Objective-C/Internal/CBLLogSinks+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ typedef NS_ENUM(NSUInteger, CBLLogAPI) {

+ (void) writeCBLLog: (C4LogDomain)domain level: (C4LogLevel)level message: (NSString*)message;

+ (void) resetApiVersion;

+ (void) checkLogApiVersion: (id<CBLLogApiSource>) source;

@end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// CustomLogger.h
// CBLLogSinks+Reset.h
// CouchbaseLite
//
// Copyright (c) 2019 Couchbase, Inc All rights reserved.
// Copyright (c) 2025 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,21 +15,11 @@
// 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 <Foundation/Foundation.h>
#import <CouchbaseLite/CBLLogTypes.h>

NS_ASSUME_NONNULL_BEGIN

@interface CustomLogger : NSObject <CBLLogSinkProtocol>
#import "CBLLogSinks.h"

@property (nonatomic, readonly) NSArray* lines;
@interface CBLLogSinks ()

- (void) reset;

- (BOOL) containsString: (NSString *)string;
+ (void) resetApiVersion;

@end

NS_ASSUME_NONNULL_END
25 changes: 17 additions & 8 deletions Objective-C/Internal/CBLStringBytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,31 @@

/** A slice holding the data of an NSString. If possible, it points the slice into the data of the
NSString, requiring no copying. Otherwise it copies the characters into a small internal
buffer, or into a temporary heap block.
NOTE: Since the slice may point directly into the NSString, if the string is mutable do not
mutate it while the stringBytes object is in scope! (Releasing the string is OK, as
stringBytes retains it.) */
stack based buffer if the inline storage is enabled, or into a temporary heap block.
NOTE:
- Since the slice may point directly into the NSString, if the string is mutable do not
mutate it while the stringBytes object is in scope! (Releasing the string is OK, as
stringBytes retains it.)
- Dissable the inline storage If the CBLStringBytes is used outside the scope that is created as
the inline storage (_local) is a stack base storage.
*/
struct CBLStringBytes {
CBLStringBytes(NSString* =nil);
CBLStringBytes(NSString* str = nil, bool useLocalBuffer = true)
: _useLocalBuffer(useLocalBuffer)
{
*this = str;
}

void operator= (NSString*);

operator C4Slice() const {return bytes;}
operator fleece::slice() const {return bytes;}
operator C4Slice() const { return bytes; }

operator fleece::slice() const { return bytes; }

fleece::slice bytes;

private:
__strong id _storage {nullptr}; // keeps string alive, if `buf` points into it
char _local[64];
bool _useLocalBuffer;
};
6 changes: 1 addition & 5 deletions Objective-C/Internal/CBLStringBytes.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

using namespace fleece;

CBLStringBytes::CBLStringBytes(__unsafe_unretained NSString* str) {
*this = str;
}

void CBLStringBytes::operator= (__unsafe_unretained NSString* str) {
if (!str) {
bytes = nullslice;
Expand All @@ -42,7 +38,7 @@

NSUInteger byteCount;
NSUInteger length = str.length;
if (length <= sizeof(_local)) {
if (_useLocalBuffer && length <= sizeof(_local)) {
// Next try to copy the UTF-8 into a smallish stack-based buffer:
NSRange remaining;
BOOL ok = [str getBytes: _local maxLength: sizeof(_local) usedLength: &byteCount
Expand Down
24 changes: 17 additions & 7 deletions Objective-C/LogSink/CBLConsoleLogSink.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// CBLConsoleLogSink.h
// CouchbaseLite
//
// Copyright (c) 2024 Couchbase, Inc All rights reserved.
// Copyright (c) 2025 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -22,19 +22,29 @@

NS_ASSUME_NONNULL_BEGIN

/** A log sink that writes log messages to the console. */
@interface CBLConsoleLogSink : NSObject

/** The minimum log level of the log messages to be logged. The default log level for
console logger is warning. */
/** The minimum log level of the log messages to be logged. The default log level is warning. */
@property (nonatomic, readonly) CBLLogLevel level;

/** The set of log domains of the log messages to be logged. By default, the log
messages of all domains will be logged. */
@property (nonatomic, readonly) CBLLogDomain domain;
/** The set of log domains of the log messages to be logged. The default is all domains. */
@property (nonatomic, readonly) CBLLogDomain domains;

/**
Initializes a console log sink with a specified log level.
@param level The minimum log level.
*/
- (instancetype) initWithLevel: (CBLLogLevel)level;

- (instancetype) initWithLevel: (CBLLogLevel)level domain: (CBLLogDomain)domain;
/**
Initializes a console log sink with a specified log level and log domains.
@param level The minimum log level.
@param domains The set of log domains.
*/
- (instancetype) initWithLevel: (CBLLogLevel)level domains: (CBLLogDomain)domains;

/** Not available */
- (instancetype) init NS_UNAVAILABLE;
Expand Down
12 changes: 6 additions & 6 deletions Objective-C/LogSink/CBLConsoleLogSink.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// CBLConsoleLogSink.m
// CouchbaseLite
//
// Copyright (c) 2024 Couchbase, Inc All rights reserved.
// Copyright (c) 2025 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,24 +24,24 @@

@implementation CBLConsoleLogSink

@synthesize level=_level, domain=_domain, version=_version;
@synthesize level=_level, domains=_domains, version=_version;

- (instancetype) initWithLevel: (CBLLogLevel)level {
return [self initWithLevel: level domain: kCBLLogDomainAll];
return [self initWithLevel: level domains: kCBLLogDomainAll];
}

- (instancetype) initWithLevel: (CBLLogLevel)level domain: (CBLLogDomain)domain {
- (instancetype) initWithLevel: (CBLLogLevel)level domains: (CBLLogDomain)domains {
self = [super init];
if (self) {
_level = level;
_domain = domain;
_domains = domains;
_version = kCBLLogAPINew;
}
return self;
}

- (void) writeLogWithLevel: (CBLLogLevel)level domain: (CBLLogDomain)domain message: (NSString*)message {
if (level < self.level || (self.domain & domain) == 0) {
if (level < _level || (_domains & domain) == 0) {
return;
}

Expand Down
38 changes: 33 additions & 5 deletions Objective-C/LogSink/CBLCustomLogSink.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// CBLCustomLogSink.h
// CouchbaseLite
//
// Copyright (c) 2024 Couchbase, Inc All rights reserved.
// Copyright (c) 2025 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -22,17 +22,45 @@

NS_ASSUME_NONNULL_BEGIN

/** Protocol for custom log sinks to handle log messages. */
@protocol CBLLogSinkProtocol <NSObject>

/** Writes a log message with the given level, domain, and content. */
- (void) writeLogWithLevel: (CBLLogLevel)level domain: (CBLLogDomain)domain message: (NSString*)message;

@end

/** A log sink that writes log messages to a custom log sink implementation. */
@interface CBLCustomLogSink : NSObject

/** The minimum log level of the log messages to be logged. The default log level for console logger is warning. */
/** The minimum log level of the log messages to be logged. The default log level is warning. */
@property (nonatomic, readonly) CBLLogLevel level;

/** The set of log domains of the log messages to be logged. By default, the log messages of all domains will be logged. */
@property (nonatomic, readonly) CBLLogDomain domain;
/** The set of log domains of the log messages to be logged. The default is all domains. */
@property (nonatomic, readonly) CBLLogDomain domains;

/** The custom log sink implementation that receives the log messages. */
@property (nonatomic, readonly) id<CBLLogSinkProtocol> logSink;

- (instancetype) initWithLevel: (CBLLogLevel) level logSink: (id<CBLLogSinkProtocol>) logSink;
/**
Initializes a custom log sink with a specified log level and custom log sink implementation.
@param level The minimum log level.
@param logSink The custom log sink implementation.
*/
- (instancetype) initWithLevel: (CBLLogLevel)level
logSink: (id<CBLLogSinkProtocol>)logSink;

/**
Initializes a custom log sink with a specified log level, log domains, and custom log sink implementation.
@param level The minimum log level.
@param domains The set of log domains.
@param logSink The custom log sink implementation.
*/
- (instancetype) initWithLevel: (CBLLogLevel)level
domains: (CBLLogDomain)domains
logSink: (id<CBLLogSinkProtocol>)logSink;

@end

Expand Down
Loading

0 comments on commit a12ee08

Please # to comment.