From: Charles-Henri Bruyand Date: Fri, 7 May 2021 09:38:26 +0000 (+0200) Subject: allow calling log methods with arbitrary number of key/value pairs X-Git-Tag: dnsdist-1.7.0-alpha1~122^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fecf182368fb83d64589d0615a5c025eaf11e877;p=thirdparty%2Fpdns.git allow calling log methods with arbitrary number of key/value pairs --- diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 4c8415fd59..0b5bc75744 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -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); diff --git a/pdns/recursordist/logging.cc b/pdns/recursordist/logging.cc index ec0413a506..f971f5687f 100644 --- a/pdns/recursordist/logging.cc +++ b/pdns/recursordist/logging.cc @@ -76,10 +76,10 @@ namespace Logging return res; } - std::shared_ptr Logger::withValues(const std::string& key, const Logr::Loggable& value) const + std::shared_ptr Logger::withValues(const std::map& values) const { auto res = std::make_shared(getptr(), _name, getVerbosity(), _level, _callback); - res->_values.insert({key, value.to_string()}); + res->_values = values; return res; } diff --git a/pdns/recursordist/logging.hh b/pdns/recursordist/logging.hh index 0e07905a64..69e5b18bed 100644 --- a/pdns/recursordist/logging.hh +++ b/pdns/recursordist/logging.hh @@ -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 v(size_t level) const override; - std::shared_ptr withValues(const std::string& key, const Logr::Loggable& value) const override; + std::shared_ptr withValues(const std::map& values) const override; virtual std::shared_ptr withName(const std::string& name) const override; static std::shared_ptr create(EntryLogger callback); diff --git a/pdns/recursordist/logr.hh b/pdns/recursordist/logr.hh index 629a91533b..c271a36e63 100644 --- a/pdns/recursordist/logr.hh +++ b/pdns/recursordist/logr.hh @@ -47,6 +47,13 @@ namespace Logr { // keys and arbitrary values. virtual void info(const std::string& msg) const = 0; + template + 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 + 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 + 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 v(size_t level) const = 0; + template + std::shared_ptr withValues(const std::string& key, const Loggable& value, const Args&... args) const + { + std::map 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 withValues(const std::string& key, const Loggable& value) const = 0; + 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 @@ -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 withName(const std::string& name) const = 0; + + private: + + template + void mapArguments(std::map& 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& map) const { + return ; + } }; } diff --git a/pdns/rpzloader.cc b/pdns/rpzloader.cc index b387cd5674..b06cf54adb 100644 --- a/pdns/rpzloader.cc +++ b/pdns/rpzloader.cc @@ -190,10 +190,10 @@ static void RPZRecordToPolicy(const DNSRecord& dr, std::shared_ptr loadRPZFromServer(const ComboAddress& primary, const DNSName& zoneName, std::shared_ptr zone, boost::optional 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 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& 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_ptrwithValues("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; }