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

Add ability to get logging level via C API #7419

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Enhancements
* <New feature description> (PR [#????](https://github.com/realm/realm-core/pull/????))
* None.
* Added ability to get current log level via C API (PR [#7419](https://github.com/realm/realm-core/pull/7419))

### Fixed
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
Expand Down
9 changes: 8 additions & 1 deletion src/realm.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,14 @@ typedef void (*realm_log_func_t)(realm_userdata_t userdata, realm_log_level_e le
RLM_API void realm_set_log_callback(realm_log_func_t, realm_log_level_e, realm_userdata_t userdata,
realm_free_userdata_func_t userdata_free) RLM_API_NOEXCEPT;
RLM_API void realm_set_log_level(realm_log_level_e) RLM_API_NOEXCEPT;
RLM_API void realm_set_log_level_category(const char*, realm_log_level_e) RLM_API_NOEXCEPT;
/**
* Set the logging level for given category. Return the previous level.
*/
RLM_API realm_log_level_e realm_set_log_level_category(const char*, realm_log_level_e) RLM_API_NOEXCEPT;
/**
* Get the logging level for given category.
*/
RLM_API realm_log_level_e realm_get_log_level_category(const char*) RLM_API_NOEXCEPT;

/**
* Get a thread-safe reference representing the same underlying object as some
Expand Down
15 changes: 12 additions & 3 deletions src/realm/object-store/c_api/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,18 @@ RLM_API void realm_set_log_level(realm_log_level_e level) noexcept
util::LogCategory::realm.set_default_level_threshold(realm::util::LogCategory::Level(level));
}

RLM_API void realm_set_log_level_category(const char* category_name, realm_log_level_e level) noexcept
RLM_API realm_log_level_e realm_set_log_level_category(const char* category_name, realm_log_level_e level) noexcept
{
util::LogCategory::get_category(category_name)
.set_default_level_threshold(realm::util::LogCategory::Level(level));
auto& cat = util::LogCategory::get_category(category_name);
realm_log_level_e prev_level = realm_log_level_e(util::Logger::get_default_logger()->get_level_threshold(cat));
cat.set_default_level_threshold(realm::util::LogCategory::Level(level));
return prev_level;
}

RLM_API realm_log_level_e realm_get_log_level_category(const char* category_name) noexcept
{
auto& cat = util::LogCategory::get_category(category_name);
return realm_log_level_e(util::Logger::get_default_logger()->get_level_threshold(cat));
}

} // namespace realm::c_api
8 changes: 5 additions & 3 deletions test/object-store/c_api/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1608,9 +1608,11 @@ TEST_CASE("C API logging", "[c_api]") {
TestFile test_file;

LogUserData userdata;
auto log_level_old = util::LogCategory::realm.get_default_level_threshold();
auto log_level_old = realm_get_log_level_category("Realm");
realm_set_log_callback(realm_log_func, RLM_LOG_LEVEL_DEBUG, &userdata, nullptr);
realm_set_log_level_category("Realm.Storage.Object", RLM_LOG_LEVEL_OFF);
auto prev_level = realm_set_log_level_category("Realm.Storage.Object", RLM_LOG_LEVEL_OFF);
CHECK(prev_level == RLM_LOG_LEVEL_DEBUG);
CHECK(realm_get_log_level_category("Realm.Storage.Object") == RLM_LOG_LEVEL_OFF);
auto config = make_config(test_file.path.c_str(), true);
realm_t* realm = realm_open(config.get());
realm_begin_write(realm);
Expand All @@ -1637,7 +1639,7 @@ TEST_CASE("C API logging", "[c_api]") {
// Remove this logger again
realm_set_log_callback(nullptr, RLM_LOG_LEVEL_DEBUG, nullptr, nullptr);
// Restore old log level
util::LogCategory::realm.set_default_level_threshold(log_level_old);
realm_set_log_level(log_level_old);
}

TEST_CASE("C API - scheduler", "[c_api]") {
Expand Down