]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
allow calling log methods with arbitrary number of key/value pairs
authorCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Fri, 7 May 2021 09:38:26 +0000 (11:38 +0200)
committerCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Fri, 7 May 2021 09:38:26 +0000 (11:38 +0200)
pdns/pdns_recursor.cc
pdns/recursordist/logging.cc
pdns/recursordist/logging.hh
pdns/recursordist/logr.hh
pdns/rpzloader.cc

index 4c8415fd5953cfa91daf6da8c382d36f5d69631f..0b5bc757448d13b9978cdf38ead02d06bd36cce2 100644 (file)
@@ -5701,9 +5701,7 @@ int main(int argc, char **argv)
     auto startupLog = g_slog->withName("startup");
 
     if(!::arg().file(configname.c_str())) {
-      startupLog
-        ->withValues("config_file", Logging::Loggable(configname))
-        ->error("No such file", "Unable to parse configuration file");
+      startupLog->error("No such file", "Unable to parse configuration file", "config_file", Logging::Loggable(configname));
     }
 
     ::arg().parse(argc,argv);
index ec0413a506773366d03c4a8f31b7468cdce1881f..f971f5687f7fd5b5e305dcdca15a31bddbf6fe8d 100644 (file)
@@ -76,10 +76,10 @@ namespace Logging
     return res;
   }
 
-  std::shared_ptr<Logr::Logger> Logger::withValues(const std::string& key, const Logr::Loggable& value) const
+  std::shared_ptr<Logr::Logger> Logger::withValues(const std::map<std::string, std::string>& values) const
   {
     auto res = std::make_shared<Logger>(getptr(), _name, getVerbosity(), _level, _callback);
-    res->_values.insert({key, value.to_string()});
+    res->_values = values;
     return res;
   }
 
index 0e07905a646cf24e96e05169bdaf6ae5ef0e4154..69e5b18bed500ce1906ad904495c885ed02e9f27 100644 (file)
@@ -70,12 +70,13 @@ namespace Logging {
   {
   public:
     bool enabled() const override;
+
     void info(const std::string& msg) const override;
     void error(int err, const std::string& msg) const override;
     void error(const std::string& err, const std::string& msg) const override;
 
     std::shared_ptr<Logr::Logger> v(size_t level) const override;
-    std::shared_ptr<Logr::Logger> withValues(const std::string& key, const Logr::Loggable& value) 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;
 
     static std::shared_ptr<Logger> create(EntryLogger callback);
index 629a91533bda43ed833d45b51d5847e7ff1ab425..c271a36e6374a111b95d3332900a62bec6d9453a 100644 (file)
@@ -47,6 +47,13 @@ namespace Logr {
     // keys and arbitrary values.
     virtual void info(const std::string& msg) const = 0;
 
+    template<typename... Args>
+    void info(const std::string& msg, const std::string& key, const Loggable& value, const Args&... args) const
+    {
+      auto logger = this->withValues(key, value, args...);
+      logger->info(msg);
+    }
+
     // Error logs an error, with the given message and key/value pairs as context.
     // It functions similarly to calling Info with the "error" named value, but may
     // have unique behavior, and should be preferred for logging errors (see the
@@ -58,15 +65,38 @@ namespace Logr {
     virtual void error(const std::string& err, const std::string& msg) const = 0;
     virtual void error(int err, const std::string& msg) const = 0;
 
+    template<typename... Args>
+    void error(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(err, msg);
+    }
+
+    template<typename... Args>
+    void error(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(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;
 
+    template<typename... Args>
+    std::shared_ptr<Logger> withValues(const std::string& key, const Loggable& value, const Args&... args) const
+    {
+      std::map<std::string, std::string> map = {};
+      this->mapArguments(map, key, value, args...);
+      return this->withValues(map);
+    }
+
     // 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::string& key, const Loggable& value) const = 0;
+    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
@@ -74,5 +104,17 @@ namespace Logr {
     // 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;
+
+  private:
+
+    template<typename... Args>
+    void mapArguments(std::map<std::string, std::string>& map, const std::string& key, const Loggable& value, const Args&... args) const
+    {
+      map.emplace(key, value.to_string());
+      mapArguments(map, args...);
+    }
+    void mapArguments(std::map<std::string, std::string>& map) const {
+      return ;
+    }
   };
 }
index b387cd56741e9ea4e44b8c206dc286ecebfd4efe..b06cf54adb2d19e58e3c4b7dd20980d7c2c8244f 100644 (file)
@@ -190,10 +190,10 @@ static void RPZRecordToPolicy(const DNSRecord& dr, std::shared_ptr<DNSFilterEngi
 static shared_ptr<SOARecordContent> loadRPZFromServer(const ComboAddress& primary, const DNSName& zoneName, std::shared_ptr<DNSFilterEngine::Zone> zone, boost::optional<DNSFilterEngine::Policy> defpol, bool defpolOverrideLocal, uint32_t maxTTL, const TSIGTriplet& tt, size_t maxReceivedBytes, const ComboAddress& localAddress, uint16_t axfrTimeout)
 {
 
-  auto logger = g_slog->withName("rpz")->withValues("zone", Logging::Loggable(zoneName))->withValues("primary", Logging::Loggable(primary));
+  auto logger = g_slog->withName("rpz")->withValues("zone", Logging::Loggable(zoneName)"primary", Logging::Loggable(primary));
   logger->info("Loading RPZ from nameserver");
   if(!tt.name.empty()) {
-    logger->withValues("tsig_key_name", Logging::Loggable(tt.name))->withValues("tsig_key_algorithm", Logging::Loggable(tt.algo))->info("Using TSIG key for authentication");
+    logger->info("Using TSIG key for authentication", "tsig_key_name", Logging::Loggable(tt.name), "tsig_key_algorithm", Logging::Loggable(tt.algo));
   }
 
   ComboAddress local(localAddress);
@@ -228,11 +228,11 @@ static shared_ptr<SOARecordContent> loadRPZFromServer(const ComboAddress& primar
       throw PDNSException("Total AXFR time exceeded!");
     }
     if(last != time(0)) {
-      logger->withValues("nrecords", Logging::Loggable(nrecords))->info("RPZ load in progress");
+      logger->info("RPZ load in progress", "nrecords", Logging::Loggable(nrecords));
       last=time(0);
     }
   }
-  logger->withValues("nrecords", Logging::Loggable(nrecords))->withValues("soa", Logging::Loggable(sr->getZoneRepresentation()))->info("RPZ load completed");
+  logger->info("RPZ load completed", "nrecords", Logging::Loggable(nrecords), "soa", Logging::Loggable(sr->getZoneRepresentation()));
   return sr;
 }
 
@@ -309,7 +309,7 @@ static void setRPZZoneNewState(const std::string& zone, uint32_t serial, uint64_
 static bool dumpZoneToDisk(const DNSName& zoneName, const std::shared_ptr<DNSFilterEngine::Zone>& newZone, const std::string& dumpZoneFileName)
 {
   auto logger = g_slog->withName("rpz")->withValues("zone", Logging::Loggable(zoneName));
-  logger->v(1)->withValues("destination_file", Logging::Loggable(dumpZoneFileName))->info("Dumping zone to disk");
+  logger->v(1)->info("Dumping zone to disk", "destination_file", Logging::Loggable(dumpZoneFileName));
   std::string temp = dumpZoneFileName + "XXXXXX";
   int fd = mkstemp(&temp.at(0));
   if (fd < 0) {
@@ -351,8 +351,7 @@ static bool dumpZoneToDisk(const DNSName& zoneName, const std::shared_ptr<DNSFil
 
   if (rename(temp.c_str(), dumpZoneFileName.c_str()) != 0) {
     logger
-      ->withValues("destination_file", Logging::Loggable(dumpZoneFileName))
-      ->error(errno, "Error while moving the content of the RPZ");
+      ->error(errno, "Error while moving the content of the RPZ", "destination_file", Logging::Loggable(dumpZoneFileName));
     return false;
   }