From 210baf3c947f22e0cc07996f0a1a439973bada61 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Mon, 24 Feb 2025 15:27:49 +0100 Subject: [PATCH] Tidy structured logging related files --- pdns/logging.hh | 4 ++-- pdns/recursordist/logging.cc | 28 ++++++++++++------------ pdns/recursordist/logr.hh | 41 ++++++++++++++++++++++++------------ 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/pdns/logging.hh b/pdns/logging.hh index ac2054e04f..ba8b00d7f8 100644 --- a/pdns/logging.hh +++ b/pdns/logging.hh @@ -177,7 +177,7 @@ public: std::shared_ptr v(size_t level) const override; std::shared_ptr withValues(const std::map& values) const override; - virtual std::shared_ptr withName(const std::string& name) const override; + std::shared_ptr withName(const std::string& name) const override; static std::shared_ptr create(EntryLogger callback); static std::shared_ptr create(EntryLogger callback, const std::string& name); @@ -185,7 +185,7 @@ public: Logger(EntryLogger callback); Logger(EntryLogger callback, boost::optional name); Logger(std::shared_ptr parent, boost::optional name, size_t verbosity, size_t lvl, EntryLogger callback); - virtual ~Logger(); + ~Logger() override; size_t getVerbosity() const; void setVerbosity(size_t verbosity); diff --git a/pdns/recursordist/logging.cc b/pdns/recursordist/logging.cc index ef415f8262..544350406a 100644 --- a/pdns/recursordist/logging.cc +++ b/pdns/recursordist/logging.cc @@ -33,9 +33,9 @@ std::shared_ptr Logger::getptr() const return shared_from_this(); } -bool Logger::enabled(Logr::Priority p) const +bool Logger::enabled(Logr::Priority prio) const { - return _level <= _verbosity || p != Logr::Absent; + return _level <= _verbosity || prio != Logr::Absent; } void Logger::info(const std::string& msg) const @@ -43,9 +43,9 @@ void Logger::info(const std::string& msg) const logMessage(msg, Logr::Absent, boost::none); } -void Logger::info(Logr::Priority p, const std::string& msg) const +void Logger::info(Logr::Priority prio, const std::string& msg) const { - logMessage(msg, p, boost::none); + logMessage(msg, prio, boost::none); } void Logger::logMessage(const std::string& msg, boost::optional err) const @@ -53,14 +53,14 @@ void Logger::logMessage(const std::string& msg, boost::optional err) const +void Logger::logMessage(const std::string& msg, Logr::Priority prio, boost::optional err) const { - if (!enabled(p)) { + if (!enabled(prio)) { return; } Entry entry; entry.level = _level; - entry.d_priority = p; + entry.d_priority = prio; Utility::gettimeofday(&entry.d_timestamp); entry.name = _name; entry.message = msg; @@ -74,19 +74,19 @@ void Logger::logMessage(const std::string& msg, Logr::Priority p, boost::optiona _callback(entry); } -void Logger::error(Logr::Priority p, int err, const std::string& msg) const +void Logger::error(Logr::Priority prio, int err, const std::string& msg) const { - logMessage(msg, p, std::string(std::strerror(err))); + logMessage(msg, prio, std::string(stringerror(err))); } -void Logger::error(Logr::Priority p, const std::string& err, const std::string& msg) const +void Logger::error(Logr::Priority prio, const std::string& err, const std::string& msg) const { - logMessage(msg, p, err); + logMessage(msg, prio, err); } void Logger::error(int err, const std::string& msg) const { - logMessage(msg, Logr::Absent, std::string(std::strerror(err))); + logMessage(msg, Logr::Absent, std::string(stringerror(err))); } void Logger::error(const std::string& err, const std::string& msg) const @@ -151,9 +151,7 @@ Logger::Logger(std::shared_ptr parent, boost::optional g_slog{nullptr}; diff --git a/pdns/recursordist/logr.hh b/pdns/recursordist/logr.hh index dea00b806f..e24141f217 100644 --- a/pdns/recursordist/logr.hh +++ b/pdns/recursordist/logr.hh @@ -33,7 +33,13 @@ namespace Logr { struct Loggable { - virtual std::string to_string() const = 0; + Loggable() = default; + Loggable(const Loggable&) = delete; + Loggable(Loggable&&) = delete; + Loggable& operator=(const Loggable&) = delete; + Loggable& operator=(Loggable&&) = delete; + virtual ~Loggable() = default; + [[nodiscard]] virtual std::string to_string() const = 0; }; // In addition to level which specifies the amount of detail and is @@ -60,19 +66,26 @@ enum Priority : uint8_t class Logger { public: + Logger() = default; + Logger(const Logger&) = delete; + Logger(Logger&&) = delete; + Logger& operator=(const Logger&) = delete; + Logger& operator=(Logger&&) = delete; + virtual ~Logger() = default; + // Enabled tests whether this Logger is enabled. For example, commandline // flags might be used to set the logging verbosity and disable some info // logs. - virtual bool enabled(Priority) const = 0; + [[nodiscard]] virtual bool enabled(Priority) const = 0; static std::string toString(Priority arg) { const std::array names = {"Absent", "Alert", "Critical", "Error", "Warning", "Notice", "Info", "Debug"}; - auto p = static_cast(arg); - if (p >= names.size()) { + auto prio = static_cast(arg); + if (prio >= names.size()) { return "?"; } - return names.at(p); + return names.at(prio); } // Info logs a non-error message with the given key/value pairs as context. // @@ -91,10 +104,10 @@ public: } template - void info(Priority p, const std::string& msg, const std::string& key, const Loggable& value, const Args&... args) const + void info(Priority prio, const std::string& msg, const std::string& key, const Loggable& value, const Args&... args) const { auto logger = this->withValues(key, value, args...); - logger->info(p, msg); + logger->info(prio, msg); } // Error logs an error, with the given message and key/value pairs as context. @@ -125,24 +138,24 @@ public: } template - void error(Priority p, const std::string& err, const std::string& msg, const std::string& key, const Loggable& value, const Args&... args) const + void error(Priority prio, const std::string& err, const std::string& msg, const std::string& key, const Loggable& value, const Args&... args) const { auto logger = this->withValues(key, value, args...); - logger->error(p, err, msg); + logger->error(prio, err, msg); } template - void error(Priority p, int err, const std::string& msg, const std::string& key, const Loggable& value, const Args&... args) const + void error(Priority prio, int err, const std::string& msg, const std::string& key, const Loggable& value, const Args&... args) const { auto logger = this->withValues(key, value, args...); - logger->error(p, err, msg); + logger->error(prio, err, msg); } // V returns an Logger value for a specific verbosity level, relative to // this Logger. In other words, V values are additive. V higher verbosity // level means a log message is less important. It's illegal to pass a log // level less than zero. - virtual std::shared_ptr v(size_t level) const = 0; + [[nodiscard]] virtual std::shared_ptr v(size_t level) const = 0; template std::shared_ptr withValues(const std::string& key, const Loggable& value, const Args&... args) const @@ -154,14 +167,14 @@ public: // WithValues adds some key-value pairs of context to a logger. // See Info for documentation on how key/value pairs work. - virtual std::shared_ptr withValues(const std::map& values) const = 0; + [[nodiscard]] virtual std::shared_ptr withValues(const std::map& values) const = 0; // WithName adds a new element to the logger's name. // Successive calls with WithName continue to append // suffixes to the logger's name. It's strongly recommended // that name segments contain only letters, digits, and hyphens // (see the package documentation for more information). - virtual std::shared_ptr withName(const std::string& name) const = 0; + [[nodiscard]] virtual std::shared_ptr withName(const std::string& name) const = 0; private: template -- 2.47.2