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 all 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
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
17 changes: 16 additions & 1 deletion src/realm.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,22 @@ 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 the actual log category names (currently 15)
@param num_values number of values in the out_values array
@param out_values pointer to an array of size num_values
@return returns the number of categories returned. If num_values is zero, it will
return the total number of categories.
*/
RLM_API size_t realm_get_category_names(size_t num_values, const char** out_values);

/**
* Get a thread-safe reference representing the same underlying object as some
Expand Down
29 changes: 26 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,32 @@ 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));
}

RLM_API size_t realm_get_category_names(size_t num_values, const char** out_values)
{
auto vec = util::LogCategory::get_category_names();
auto number_to_copy = vec.size();
if (num_values > 0) {
if (number_to_copy > num_values)
number_to_copy = num_values;
for (size_t n = 0; n < number_to_copy; n++) {
out_values[n] = vec[n];
}
}
return number_to_copy;
}

} // namespace realm::c_api
9 changes: 9 additions & 0 deletions src/realm/util/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ LogCategory& LogCategory::get_category(std::string_view name)
return *log_catagory_map.at(name); // Throws
}

std::vector<const char*> LogCategory::get_category_names()
{
std::vector<const char*> ret;
for (auto& it : log_catagory_map) {
ret.push_back(it.second->get_name().c_str());
}
return ret;
}

void LogCategory::set_default_level_threshold(Level level)
{
m_default_level.store(level);
Expand Down
1 change: 1 addition & 0 deletions src/realm/util/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class LogCategory {
// Find category from fully qualified name. Will throw if
// name does not match a category
static LogCategory& get_category(std::string_view name);
static std::vector<const char*> get_category_names();

private:
friend class Logger;
Expand Down
15 changes: 12 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,18 @@ TEST_CASE("C API logging", "[c_api]") {
TestFile test_file;

LogUserData userdata;
auto log_level_old = util::LogCategory::realm.get_default_level_threshold();
const char* category_names[20];
auto num_categories = realm_get_category_names(20, category_names);
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);
for (size_t n = 0; n < num_categories; n++) {
CHECK(realm_get_log_level_category(category_names[n]) == RLM_LOG_LEVEL_DEBUG);
}

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 +1646,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
Loading