Skip to content

Commit

Permalink
Add pImpl to libdnf5::LogRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
kontura authored and jan-kolarik committed Apr 4, 2024
1 parent 10e67ea commit 4cff419
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
19 changes: 12 additions & 7 deletions include/libdnf5/logger/log_router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "logger.hpp"

#include "libdnf5/common/impl_ptr.hpp"

#include <memory>
#include <vector>

Expand All @@ -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<std::unique_ptr<Logger>> && loggers) : loggers(std::move(loggers)) {}
LogRouter(std::vector<std::unique_ptr<Logger>> && loggers);

/// Moves (registers) the "logger" into the log router. It gets next free index number.
void add_logger(std::unique_ptr<Logger> && logger) { loggers.push_back(std::move(logger)); }
void add_logger(std::unique_ptr<Logger> && 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<Logger> release_logger(size_t index);

/// Swaps the logger at the "index" position with another "logger".
void swap_logger(std::unique_ptr<Logger> & logger, size_t index) { loggers.at(index).swap(logger); }
void swap_logger(std::unique_ptr<Logger> & 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;

Expand All @@ -63,7 +67,8 @@ class LogRouter : public Logger {
const std::string & message) noexcept override;

private:
std::vector<std::unique_ptr<Logger>> loggers;
class Impl;
ImplPtr<Impl> p_impl;
};

} // namespace libdnf5
Expand Down
40 changes: 36 additions & 4 deletions libdnf5/logger/log_router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,48 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

namespace libdnf5 {

class LogRouter::Impl {
public:
Impl(){};
Impl(std::vector<std::unique_ptr<Logger>> && loggers) : loggers(std::move(loggers)) {}

private:
friend LogRouter;
std::vector<std::unique_ptr<Logger>> loggers;
};

LogRouter::LogRouter() : p_impl(new Impl()){};

LogRouter::~LogRouter() = default;

LogRouter::LogRouter(std::vector<std::unique_ptr<Logger>> && loggers) : p_impl(new Impl(std::move(loggers))) {}

void LogRouter::add_logger(std::unique_ptr<Logger> && 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> & 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<Logger> LogRouter::release_logger(size_t index) {
auto ret = std::move(loggers.at(index));
loggers.erase(loggers.begin() + static_cast<int>(index));
auto ret = std::move(p_impl->loggers.at(index));
p_impl->loggers.erase(p_impl->loggers.begin() + static_cast<int>(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);
}
}
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 4cff419

Please sign in to comment.