Skip to content

Commit

Permalink
Solved PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaafanador3 committed May 30, 2024
1 parent c6c23cd commit 1472ad6
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Realm/ObjectServerTests/RLMSyncTestCase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ - (RLMApp *)appWithId:(NSString *)appId {
RLMApp *app = [RLMApp appWithConfiguration:config];
RLMSyncManager *syncManager = app.syncManager;
syncManager.userAgent = self.name;
RLMLogger.defaultLogger.level = RLMLogLevelWarn;
[RLMLogger.defaultLogger setLevel:RLMLogLevelWarn category:RLMLogCategoryRealmSync];
return app;
}

Expand Down
55 changes: 49 additions & 6 deletions Realm/RLMLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,40 @@ typedef RLM_CLOSED_ENUM(NSUInteger, RLMLogLevel) {
RLMLogLevelAll
} NS_SWIFT_NAME(LogLevel);

/// An enum representing different levels of sync-related logging that can be configured.
typedef NS_ENUM(NSUInteger, RLMLogCategory) {
/// Top level log category for Realm, updating this category level would set all other subcategories too.
RLMLogCategoryRealm,
/// Log category for all sdk related logs.
RLMLogCategoryRealmSDK,
/// Log category for all app related logs.
RLMLogCategoryRealmApp,
/// Log category for all database related logs.
RLMLogCategoryRealmStorage,
/// Log category for all database transaction related logs.
RLMLogCategoryRealmStorageTransaction,
/// Log category for all database queries related logs.
RLMLogCategoryRealmStorageQuery,
/// Log category for all database object related logs.
RLMLogCategoryRealmStorageObject,
/// Log category for all database notification related logs.
RLMLogCategoryRealmStorageNotification,
/// Log category for all sync related logs.
RLMLogCategoryRealmSync,
/// Log category for all sync client related logs.
RLMLogCategoryRealmSyncClient,
/// Log category for all sync client session related logs.
RLMLogCategoryRealmSyncClientSession,
/// Log category for all sync client changeset related logs.
RLMLogCategoryRealmSyncClientChangeset,
/// Log category for all sync client network related logs.
RLMLogCategoryRealmSyncClientNetwork,
/// Log category for all sync client reset related logs.
RLMLogCategoryRealmSyncClientReset,
/// Log category for all sync server related logs.
RLMLogCategoryRealmSyncServer
};

/// A log callback function which can be set on RLMLogger.
///
/// The log function may be called from multiple threads simultaneously, and is
Expand All @@ -60,7 +94,7 @@ typedef void (^RLMLogFunction)(RLMLogLevel level, NSString *message);
/// The log function may be called from multiple threads simultaneously, and is
/// responsible for performing its own synchronization if any is required.
RLM_SWIFT_SENDABLE // invoked on a background thread
typedef void (^RLMLogCategoryFunction)(RLMLogLevel level, NSString *category, NSString *message) NS_REFINED_FOR_SWIFT;
typedef void (^RLMLogCategoryFunction)(RLMLogLevel level, RLMLogCategory category, NSString *message) NS_REFINED_FOR_SWIFT;
/**
`RLMLogger` is used for creating your own custom logging logic.
Expand All @@ -69,8 +103,8 @@ typedef void (^RLMLogCategoryFunction)(RLMLogLevel level, NSString *category, NS
Set this custom logger as you default logger using `setDefaultLogger`.
RLMLogger.defaultLogger = [[RLMLogger alloc] initWithLevel:RLMLogLevelDebug
category:RLMLogCategoryRealm
logFunction:^(RLMLogLevel level, NSString *category, NSString *message) {
category:RLMLogCategoryRealm
logFunction:^(RLMLogLevel level, NSString *category, NSString *message) {
NSLog(@"Realm Log - %lu, %@, %@", (unsigned long)level, category, message);
}];
Expand Down Expand Up @@ -104,7 +138,7 @@ __attribute__((deprecated("Use `initWithLevel:logFunction:` instead.")));
@param logFunction The log function which will be invoked whenever there is a log message.
*/
- (instancetype)initWithLevel:(RLMLogLevel)level
category:(NSString *)category
category:(RLMLogCategory)category
logFunction:(RLMLogCategoryFunction)logFunction;

#pragma mark RLMLogger Default Logger API
Expand All @@ -120,15 +154,24 @@ __attribute__((deprecated("Use `initWithLevel:logFunction:` instead.")));
@param level The log level to be set for the logger.
@param category The log function which will be invoked whenever there is a log message.
*/
- (void)setLevel:(RLMLogLevel)level category:(NSString *)category NS_REFINED_FOR_SWIFT;
- (void)setLevel:(RLMLogLevel)level category:(RLMLogCategory)category NS_REFINED_FOR_SWIFT;

/**
Gets the logger's associated level for the specified category.
@param category The log category which we need the level.
@returns The log level for the specified category
*/
- (RLMLogLevel)getLevelForCategory:(NSString *)category NS_REFINED_FOR_SWIFT;
- (RLMLogLevel)getLevelForCategory:(RLMLogCategory)category NS_REFINED_FOR_SWIFT;

/**
Log a message to the supplied level.
@param logLevel The log level for the message.
@param category The log category for the message.
@param message The message to log.
*/
- (void)logWithLevel:(RLMLogLevel)logLevel category:(RLMLogCategory)category message:(NSString *)message;

@end

Expand Down
85 changes: 73 additions & 12 deletions Realm/RLMLogger.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#import <realm/util/logger.hpp>

typedef void (^RLMLoggerFunction)(RLMLogLevel level, NSString *category, NSString *message);
typedef void (^RLMLoggerFunction)(RLMLogLevel level, RLMLogCategory category, NSString *message);

using namespace realm;
using Logger = realm::util::Logger;
Expand Down Expand Up @@ -75,6 +75,57 @@ static RLMLogLevel logLevelForLevel(Level logLevel) {
REALM_UNREACHABLE(); // Unrecognized log level.
}

static NSArray<NSString *> *categories = [NSArray arrayWithObjects:
@"Realm",
@"Realm.SDK",
@"Realm.App",
@"Realm.Storage",
@"Realm.Storage.Transaction",
@"Realm.Storage.Query",
@"Realm.Storage.Object",
@"Realm.Storage.Notification",
@"Realm.Sync",
@"Realm.Sync.Client",
@"Realm.Sync.Client.Session",
@"Realm.Sync.Client.Changeset",
@"Realm.Sync.Client.Network",
@"Realm.Sync.Client.Reset",
@"Realm.Sync.Server",
nil];

static std::string categoryNameForLogCategory(RLMLogCategory logCategory) {
if (logCategory < [categories count]) {
if (auto categoryName = [categories objectAtIndex:logCategory]) {
return categoryName.UTF8String;
}
}
REALM_UNREACHABLE();
}

static RLMLogCategory logCategoryForCategoryName(std::string category) {
auto index = [categories indexOfObject:RLMStringDataToNSString(category)];
if (index != NSNotFound) {
switch (index) {
case 0: return RLMLogCategoryRealm;
case 1: return RLMLogCategoryRealmSDK;
case 2: return RLMLogCategoryRealmApp;
case 3: return RLMLogCategoryRealmStorage;
case 4: return RLMLogCategoryRealmStorageTransaction;
case 5: return RLMLogCategoryRealmStorageQuery;
case 6: return RLMLogCategoryRealmStorageObject;
case 7: return RLMLogCategoryRealmStorageNotification;
case 8: return RLMLogCategoryRealmSync;
case 9: return RLMLogCategoryRealmSyncClient;
case 10: return RLMLogCategoryRealmSyncClientSession;
case 11: return RLMLogCategoryRealmSyncClientChangeset;
case 12: return RLMLogCategoryRealmSyncClientNetwork;
case 13: return RLMLogCategoryRealmSyncClientReset;
case 14: return RLMLogCategoryRealmSyncServer;
}
}
REALM_UNREACHABLE();
}

struct CocoaLogger : public Logger {
void do_log(const LogCategory& category, Level level, const std::string& message) override {
NSLog(@"%@:%@ %@", levelPrefix(level), RLMStringDataToNSString(category.get_name()), RLMStringDataToNSString(message));
Expand All @@ -87,7 +138,7 @@ void do_log(const LogCategory& category, Level level, const std::string& message
void do_log(const LogCategory& category, Level level, const std::string& message) override {
@autoreleasepool {
if (function) {
function(logLevelForLevel(level), RLMStringDataToNSString(category.get_name()), RLMStringDataToNSString(message));
function(logLevelForLevel(level), logCategoryForCategoryName(category.get_name()), RLMStringDataToNSString(message));
}
}
}
Expand Down Expand Up @@ -126,7 +177,7 @@ - (instancetype)initWithLevel:(RLMLogLevel)level
if (self = [super init]) {
auto logger = std::make_shared<CustomLogger>();
logger->set_level_threshold(levelForLogLevel(level));
auto block = [logFunction](RLMLogLevel level, NSString *, NSString *message) {
auto block = [logFunction](RLMLogLevel level, RLMLogCategory, NSString *message) {
logFunction(level, message);
};
logger->function = block;
Expand All @@ -136,11 +187,11 @@ - (instancetype)initWithLevel:(RLMLogLevel)level
}

- (instancetype)initWithLevel:(RLMLogLevel)level
category:(NSString *)category
category:(RLMLogCategory)category
logFunction:(RLMLogCategoryFunction)logFunction {
if (self = [super init]) {
auto logger = std::make_shared<CustomLogger>();
logger->set_level_threshold(LogCategory::get_category(category.UTF8String), levelForLogLevel(level));
logger->set_level_threshold(categoryNameForLogCategory(category), levelForLogLevel(level));
logger->function = logFunction;
self->_logger = logger;
}
Expand All @@ -157,22 +208,28 @@ - (void)logWithLevel:(RLMLogLevel)logLevel message:(NSString *)message, ... {
}
}

- (void)logWithLevel:(RLMLogLevel)logLevel category:(NSString *)category message:(NSString *)message {
- (void)logWithLevel:(RLMLogLevel)logLevel category:(RLMLogCategory)category message:(NSString *)message {
auto level = levelForLogLevel(logLevel);
if (_logger->would_log(level)) {
_logger->log(LogCategory::get_category(categoryNameForLogCategory(category)), levelForLogLevel(logLevel), message.UTF8String);
}
}

- (void)logWithLevel:(RLMLogLevel)logLevel categoryName:(NSString *)categoryName message:(NSString *)message {
auto level = levelForLogLevel(logLevel);
if (_logger->would_log(level)) {
_logger->log(level, "%1", message.UTF8String);
_logger->log(LogCategory::get_category(category.UTF8String), levelForLogLevel(logLevel), message.UTF8String);
_logger->log(LogCategory::get_category(categoryName.UTF8String), levelForLogLevel(logLevel), message.UTF8String);
}
}

- (void)setLevel:(RLMLogLevel)level category:(NSString *)category {
- (void)setLevel:(RLMLogLevel)level category:(RLMLogCategory)category {
RLMLogger *defaultLogger = [RLMLogger defaultLogger];
defaultLogger->_logger->set_level_threshold(LogCategory::get_category(category.UTF8String), levelForLogLevel(level));
defaultLogger->_logger->set_level_threshold(categoryNameForLogCategory(category), levelForLogLevel(level));
}

- (RLMLogLevel)getLevelForCategory:(NSString *)category {
- (RLMLogLevel)getLevelForCategory:(RLMLogCategory)category {
RLMLogger *defaultLogger = [RLMLogger defaultLogger];
return logLevelForLevel(defaultLogger->_logger->get_level_threshold(LogCategory::get_category(category.UTF8String)));
return logLevelForLevel(defaultLogger->_logger->get_level_threshold(categoryNameForLogCategory(category)));
}

#pragma mark Testing
Expand All @@ -187,6 +244,10 @@ - (RLMLogLevel)getLevelForCategory:(NSString *)category {
return a;
}

+ (RLMLogCategory)categoryFromString:(NSString *)string {
return logCategoryForCategoryName(string.UTF8String);
}

#pragma mark Global Logger Setter

+ (instancetype)defaultLogger {
Expand Down
51 changes: 51 additions & 0 deletions Realm/RLMLogger_Private copy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2023 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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 <Realm/RLMLogger.h>
#import <Realm/RLMConstants.h>

RLM_HEADER_AUDIT_BEGIN(nullability)

@interface RLMLogger()

/**
Log a message to the supplied level.
@param logLevel The log level for the message.
@param message The message to log.
*/
- (void)logWithLevel:(RLMLogLevel)logLevel message:(NSString *)message, ... NS_SWIFT_UNAVAILABLE("");

/**
Log a message to the supplied level.
@param logLevel The log level for the message.
@param category The log category for the message.
@param message The message to log.
*/
- (void)logWithLevel:(RLMLogLevel)logLevel category:(NSString *)category message:(NSString *)message;

/**
Gets all the categories from Core. This is to be used for testing purposes only.
*/
+ (NSArray<NSString *> *)getAllCategories;

+ (RLMLogCategory)categoryFromString:(NSString *)string;
@end

RLM_HEADER_AUDIT_END(nullability)
11 changes: 9 additions & 2 deletions Realm/RLMLogger_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,22 @@ RLM_HEADER_AUDIT_BEGIN(nullability)
Log a message to the supplied level.
@param logLevel The log level for the message.
@param category The log category for the message.
@param category The log category name for the message.

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | tvos_15.4 | Test - tvOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | swiftui_15.4 | Test - iOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | watchos-swift_15.4 | Build - macOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | ios-swift-evolution_15.4 | Test - iOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | catalyst-swift_15.4 | Test - macOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | swiftui-sync_15.4 | Test - macOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | osx-swift-evolution_15.4 | Test - macOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | watchos_15.4 | Build - macOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | catalyst_15.4 | Test - macOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | ios-swift_15.4 | Test - iOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | ios_15.1 | Test - iOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | osx-swift_15.3 | Test - macOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | osx-swift_15.4 | Test - macOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | osx_15.4 | Test - macOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | ios_15.4 | Test - iOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | tvos-swift-evolution_15.4 | Test - tvOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | osx_15.3 | Test - macOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | ios-static_15.4 | Test - iOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | tvos-swift_15.4 | Test - tvOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration

Check notice on line 38 in Realm/RLMLogger_Private.h

View check run for this annotation

Xcode Cloud / RealmSwift | osx_15.2 | Test - macOS

Realm/RLMLogger_Private.h#L38

Parameter 'category' not found in the function declaration
@param message The message to log.
*/
- (void)logWithLevel:(RLMLogLevel)logLevel category:(NSString *)category message:(NSString *)message;
- (void)logWithLevel:(RLMLogLevel)logLevel categoryName:(NSString *)categoryName message:(NSString *)message;

#pragma mark Testing

/**
Gets all the categories from Core. This is to be used for testing purposes only.
*/
+ (NSArray<NSString *> *)getAllCategories;

/**
Returns a `RLMLogCategory` from a string.
*/
+ (RLMLogCategory)categoryFromString:(NSString *)string;
@end

RLM_HEADER_AUDIT_END(nullability)
Loading

0 comments on commit 1472ad6

Please sign in to comment.