From e8924a3d47f50ed6ad100252075414622f6c8d89 Mon Sep 17 00:00:00 2001 From: Felix Heitmann Date: Fri, 5 Jul 2024 14:54:55 +0200 Subject: [PATCH] Attributes: Removing impact on logger The logger interface should not be bloated by additional functionality. Reducing it to a nested call to `.attrs().put()` achieves the same nesting as `.put_attribute()` but requires a smaller interface change on the logger class. --- include/spdlog/logger.h | 6 +----- src/logger.cpp | 15 +-------------- tests/test_attributes.cpp | 2 +- 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index c0627424..2df3032d 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -161,11 +161,7 @@ public: // create new logger with same sinks and configuration. std::shared_ptr clone(std::string logger_name); - void push_attribute(log_attributes::attr_map_t const &attributes); - void push_attribute(log_attributes::key_t const &key, log_attributes::value_t const &value); - void push_attribute(log_attributes const &attributes); - void remove_attribute(const log_attributes::key_t &key); - void clear_attribute(); + log_attributes &attrs(); private: std::string name_; diff --git a/src/logger.cpp b/src/logger.cpp index 4da695af..258c8700 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -82,19 +82,6 @@ void logger::flush_() noexcept { } } -void logger::push_attribute(const log_attributes::attr_map_t &attributes) { - this->attributes.attr_ctx(attributes.begin(), attributes.end()); -} -void logger::push_attribute(const log_attributes::key_t &key, const log_attributes::value_t &value) { - attributes.put(key, value); -} -void logger::push_attribute(const log_attributes &attributes) { - const auto &map = attributes.get_map(); - this->attributes.attr_ctx(map.begin(), map.end()); -} - -void logger::remove_attribute(const log_attributes::key_t &key) { attributes.remove(key); } - -void logger::clear_attribute() { attributes.clear(); } +log_attributes &logger::attrs() { return attributes; } } // namespace spdlog diff --git a/tests/test_attributes.cpp b/tests/test_attributes.cpp index 2c1f5977..fec7fa8e 100644 --- a/tests/test_attributes.cpp +++ b/tests/test_attributes.cpp @@ -11,7 +11,7 @@ TEST_CASE("Attribute test") { log_a.set_pattern("[%n] [%*]"); log_b.set_pattern("[%n] [%*]"); - log_a.push_attribute("my_key", "my_value"); + log_a.attrs().put("my_key", "my_value"); log_a.info("Hello"); log_b.info("Hello");