]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Tidy structured logging related files 15217/head
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 24 Feb 2025 14:27:49 +0000 (15:27 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 4 Mar 2025 13:45:20 +0000 (14:45 +0100)
pdns/logging.hh
pdns/recursordist/logging.cc
pdns/recursordist/logr.hh

index ac2054e04f5bdd14d1933cb6e2b06403608e7770..ba8b00d7f8034a5fdf2d8200b2af0bb1c56c1044 100644 (file)
@@ -177,7 +177,7 @@ public:
 
   std::shared_ptr<Logr::Logger> v(size_t level) const override;
   std::shared_ptr<Logr::Logger> withValues(const std::map<std::string, std::string>& values) const override;
-  virtual std::shared_ptr<Logr::Logger> withName(const std::string& name) const override;
+  std::shared_ptr<Logr::Logger> withName(const std::string& name) const override;
 
   static std::shared_ptr<Logger> create(EntryLogger callback);
   static std::shared_ptr<Logger> create(EntryLogger callback, const std::string& name);
@@ -185,7 +185,7 @@ public:
   Logger(EntryLogger callback);
   Logger(EntryLogger callback, boost::optional<std::string> name);
   Logger(std::shared_ptr<const Logger> parent, boost::optional<std::string> name, size_t verbosity, size_t lvl, EntryLogger callback);
-  virtual ~Logger();
+  ~Logger() override;
 
   size_t getVerbosity() const;
   void setVerbosity(size_t verbosity);
index ef415f8262c0493bc8ff234f5b3340ad65eb2d91..544350406aee6a3e185b1e903d04f2ed62028dd9 100644 (file)
@@ -33,9 +33,9 @@ std::shared_ptr<const Logger> 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<const std::string> err) const
@@ -53,14 +53,14 @@ void Logger::logMessage(const std::string& msg, boost::optional<const std::strin
   return logMessage(msg, Logr::Absent, std::move(err));
 }
 
-void Logger::logMessage(const std::string& msg, Logr::Priority p, boost::optional<const std::string> err) const
+void Logger::logMessage(const std::string& msg, Logr::Priority prio, boost::optional<const std::string> 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<const Logger> parent, boost::optional<std::string
 {
 }
 
-Logger::~Logger()
-{
-}
+Logger::~Logger() = default;
 };
 
 std::shared_ptr<Logging::Logger> g_slog{nullptr};
index dea00b806f74bb916bbab3a56ad5178433610105..e24141f2175a6b85de984a8f7125acfa52066ea5 100644 (file)
@@ -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<std::string, 8> names = {"Absent", "Alert", "Critical", "Error", "Warning", "Notice", "Info", "Debug"};
-    auto p = static_cast<unsigned int>(arg);
-    if (p >= names.size()) {
+    auto prio = static_cast<unsigned int>(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 <typename... Args>
-  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 <typename... Args>
-  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 <typename... Args>
-  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<Logger> v(size_t level) const = 0;
+  [[nodiscard]] virtual std::shared_ptr<Logger> v(size_t level) const = 0;
 
   template <typename... Args>
   std::shared_ptr<Logger> 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<Logger> withValues(const std::map<std::string, std::string>& values) const = 0;
+  [[nodiscard]] virtual std::shared_ptr<Logger> withValues(const std::map<std::string, std::string>& 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<Logger> withName(const std::string& name) const = 0;
+  [[nodiscard]] virtual std::shared_ptr<Logger> withName(const std::string& name) const = 0;
 
 private:
   template <typename... Args>