From 4cff4192db2c7e0a78d0942260571458dc205165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Mat=C4=9Bj?= Date: Thu, 4 Apr 2024 12:05:23 +0200 Subject: [PATCH] Add pImpl to `libdnf5::LogRouter` --- include/libdnf5/logger/log_router.hpp | 19 ++++++++----- libdnf5/logger/log_router.cpp | 40 ++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/include/libdnf5/logger/log_router.hpp b/include/libdnf5/logger/log_router.hpp index a622b6885..a81f3c2ab 100644 --- a/include/libdnf5/logger/log_router.hpp +++ b/include/libdnf5/logger/log_router.hpp @@ -22,6 +22,8 @@ along with libdnf. If not, see . #include "logger.hpp" +#include "libdnf5/common/impl_ptr.hpp" + #include #include @@ -33,26 +35,28 @@ namespace libdnf5 { class LogRouter : public Logger { public: /// Constructs a new LogRouter instance with an empty set of destination loggers. - LogRouter() = default; + LogRouter(); + + ~LogRouter(); /// Constructs a new LogRouter instance and sets the destination loggers. - LogRouter(std::vector> && loggers) : loggers(std::move(loggers)) {} + LogRouter(std::vector> && loggers); /// Moves (registers) the "logger" into the log router. It gets next free index number. - void add_logger(std::unique_ptr && logger) { loggers.push_back(std::move(logger)); } + void add_logger(std::unique_ptr && logger); /// Returns pointer to the logger at the "index" position. - Logger * get_logger(size_t index) { return loggers.at(index).get(); } + Logger * get_logger(size_t index); /// Removes logger at the "index" position from LogRouter. /// The array of the loggers is squeezed. Index of the loggers behind removed logger is decreased by one. std::unique_ptr release_logger(size_t index); /// Swaps the logger at the "index" position with another "logger". - void swap_logger(std::unique_ptr & logger, size_t index) { loggers.at(index).swap(logger); } + void swap_logger(std::unique_ptr & logger, size_t index); /// Returns number of loggers registered in LogRouter. - size_t get_loggers_count() const noexcept { return loggers.size(); } + size_t get_loggers_count() const noexcept; void log_line(Level level, const std::string & message) noexcept override; @@ -63,7 +67,8 @@ class LogRouter : public Logger { const std::string & message) noexcept override; private: - std::vector> loggers; + class Impl; + ImplPtr p_impl; }; } // namespace libdnf5 diff --git a/libdnf5/logger/log_router.cpp b/libdnf5/logger/log_router.cpp index ea57cee89..10ea17a6f 100644 --- a/libdnf5/logger/log_router.cpp +++ b/libdnf5/logger/log_router.cpp @@ -21,16 +21,48 @@ along with libdnf. If not, see . namespace libdnf5 { +class LogRouter::Impl { +public: + Impl(){}; + Impl(std::vector> && loggers) : loggers(std::move(loggers)) {} + +private: + friend LogRouter; + std::vector> loggers; +}; + +LogRouter::LogRouter() : p_impl(new Impl()){}; + +LogRouter::~LogRouter() = default; + +LogRouter::LogRouter(std::vector> && loggers) : p_impl(new Impl(std::move(loggers))) {} + +void LogRouter::add_logger(std::unique_ptr && logger) { + p_impl->loggers.push_back(std::move(logger)); +} + +Logger * LogRouter::get_logger(size_t index) { + return p_impl->loggers.at(index).get(); +} + +void LogRouter::swap_logger(std::unique_ptr & logger, size_t index) { + p_impl->loggers.at(index).swap(logger); +} + +size_t LogRouter::get_loggers_count() const noexcept { + return p_impl->loggers.size(); +} + std::unique_ptr LogRouter::release_logger(size_t index) { - auto ret = std::move(loggers.at(index)); - loggers.erase(loggers.begin() + static_cast(index)); + auto ret = std::move(p_impl->loggers.at(index)); + p_impl->loggers.erase(p_impl->loggers.begin() + static_cast(index)); return ret; } void LogRouter::log_line(Level level, const std::string & message) noexcept { auto now = std::chrono::system_clock::now(); auto pid = getpid(); - for (auto & logger : loggers) { + for (auto & logger : p_impl->loggers) { logger->write(now, pid, level, message); } } @@ -41,7 +73,7 @@ void LogRouter::write( pid_t pid, Level level, const std::string & message) noexcept { - for (auto & logger : loggers) { + for (auto & logger : p_impl->loggers) { logger->write(time, pid, level, message); } }