Skip to content

Commit

Permalink
Restore the ability to set loggers per-SyncManager (#6400)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne authored Mar 23, 2023
1 parent 7eac25f commit b8aad6b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Enhancements
* <New feature description> (PR [#????](https://github.com/realm/realm-core/pull/????))
* Expose `Results::is_valid()` in the C API, in order to prevent to use an invalid Results. (PR [#6407](https://github.com/realm/realm-core/pull/6407))
* Restore `SyncManager::set_logger_factory()` to enable setting different loggers for different SyncManagers ([PR #6400](https://github.com/realm/realm-core/pull/6400)).

### Fixed
* `SyncSession::pause()` could hold a reference to the database open after shutting down the sync session, preventing users from being able to delete the realm. ([#6372](https://github.com/realm/realm-core/issues/6372), since v13.3.0)
Expand Down
3 changes: 1 addition & 2 deletions src/realm/object-store/sync/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ App::~App() {}

void App::configure(const SyncClientConfig& sync_client_config)
{
init_logger();
auto sync_route = make_sync_route(m_app_route);
m_sync_manager->configure(shared_from_this(), sync_route, sync_client_config);
if (auto metadata = m_sync_manager->app_metadata()) {
Expand All @@ -272,7 +271,7 @@ void App::configure(const SyncClientConfig& sync_client_config)
bool App::init_logger()
{
if (!m_logger_ptr) {
m_logger_ptr = util::Logger::get_default_logger();
m_logger_ptr = m_sync_manager->get_logger();
}
return bool(m_logger_ptr);
}
Expand Down
35 changes: 34 additions & 1 deletion src/realm/object-store/sync/sync_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,36 @@ void SyncManager::reset_for_testing()
}
}

void SyncManager::set_log_level(util::Logger::Level level) noexcept
{
util::CheckedLockGuard lock(m_mutex);
m_config.log_level = level;
// Update the level threshold in the already created logger
if (m_logger_ptr) {
m_logger_ptr->set_level_threshold(level);
}
}

void SyncManager::set_logger_factory(SyncClientConfig::LoggerFactory factory)
{
util::CheckedLockGuard lock(m_mutex);
m_config.logger_factory = std::move(factory);

if (m_sync_client)
throw std::logic_error("Cannot set the logger factory after creating the sync client");

// Create a new logger using the new factory
do_make_logger();
}

void SyncManager::do_make_logger()
{
m_logger_ptr = util::Logger::get_default_logger();
if (m_config.logger_factory) {
m_logger_ptr = m_config.logger_factory(m_config.log_level);
}
else {
m_logger_ptr = util::Logger::get_default_logger();
}
}

const std::shared_ptr<util::Logger>& SyncManager::get_logger() const
Expand Down Expand Up @@ -306,6 +333,12 @@ void SyncManager::reconnect() const
}
}

util::Logger::Level SyncManager::log_level() const noexcept
{
util::CheckedLockGuard lock(m_mutex);
return m_config.log_level;
}

bool SyncManager::perform_metadata_update(util::FunctionRef<void(SyncMetadataManager&)> update_function) const
{
util::CheckedLockGuard lock(m_file_system_mutex);
Expand Down
11 changes: 11 additions & 0 deletions src/realm/object-store/sync/sync_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ struct SyncClientConfig {
MetadataMode metadata_mode = MetadataMode::Encryption;
util::Optional<std::vector<char>> custom_encryption_key;

using LoggerFactory = std::function<std::shared_ptr<util::Logger>(util::Logger::Level)>;
LoggerFactory logger_factory;
util::Logger::Level log_level = util::Logger::Level::info;
ReconnectMode reconnect_mode = ReconnectMode::normal; // For internal sync-client testing only!
bool multiplex_sessions = false;

Expand Down Expand Up @@ -121,6 +124,13 @@ class SyncManager : public std::enable_shared_from_this<SyncManager> {
// Destroys the sync manager, terminates all sessions created by it, and stops its SyncClient.
~SyncManager();

// Sets the log level for the Sync Client.
// The log level can only be set up until the point the Sync Client is
// created (when the first Session is created) or an App operation is
// performed (e.g. log in).
void set_log_level(util::Logger::Level) noexcept REQUIRES(!m_mutex);
void set_logger_factory(SyncClientConfig::LoggerFactory) REQUIRES(!m_mutex);

// Sets the application level user agent string.
// This should have the format specified here:
// https://github.com/realm/realm-sync/blob/develop/src/realm/sync/client.hpp#L126 The user agent can only be set
Expand All @@ -140,6 +150,7 @@ class SyncManager : public std::enable_shared_from_this<SyncManager> {
/// on a per-session basis.
void reconnect() const REQUIRES(!m_session_mutex);

util::Logger::Level log_level() const noexcept REQUIRES(!m_mutex);

std::vector<std::shared_ptr<SyncSession>> get_all_sessions() const REQUIRES(!m_session_mutex);
std::shared_ptr<SyncSession> get_session(std::shared_ptr<DB> db, const RealmConfig& config)
Expand Down

0 comments on commit b8aad6b

Please sign in to comment.