From: Otto Moerbeek Date: Wed, 18 May 2022 15:38:32 +0000 (+0200) Subject: More smart logging of iterables and inline a few methods X-Git-Tag: auth-4.8.0-alpha0~95^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=88ddfdf484fd820a3a2139136b2a157fb01bca06;p=thirdparty%2Fpdns.git More smart logging of iterables and inline a few methods --- diff --git a/pdns/logging.hh b/pdns/logging.hh index 75a7ac48b7..01e0d7a685 100644 --- a/pdns/logging.hh +++ b/pdns/logging.hh @@ -66,17 +66,46 @@ struct Loggable : public Logr::Loggable } }; template <> -std::string Loggable::to_string() const; -template <> -std::string Loggable::to_string() const; -template <> -std::string Loggable::to_string() const; +inline std::string Loggable::to_string() const +{ + return _t.toLogString(); +} template <> -std::string Loggable>::to_string() const; +inline std::string Loggable::to_string() const +{ + return _t.toLogString(); +} template <> -std::string Loggable>::to_string() const; +inline std::string Loggable::to_string() const +{ + return _t; +} -// Loggable::Loggable(const std::string& v): _t(v) {} +template +struct IterLoggable : public Logr::Loggable +{ + const T& _t1; + const T& _t2; + IterLoggable(const T& v1, const T& v2) : + _t1(v1), _t2(v2) + { + } + std::string to_string() const + { + std::ostringstream oss; + bool first = true; + for (auto i = _t1; i != _t2; i++) { + if (!first) { + oss << ' '; + } + else { + first = false; + } + oss << *i; + } + return oss.str(); + } +}; typedef void (*EntryLogger)(const Entry&); diff --git a/pdns/recursordist/logging.cc b/pdns/recursordist/logging.cc index 6ac2bcc4dc..5cfef1752c 100644 --- a/pdns/recursordist/logging.cc +++ b/pdns/recursordist/logging.cc @@ -106,60 +106,6 @@ std::shared_ptr Logger::withValues(const std::map; -template struct Loggable; -template struct Loggable; -template struct Loggable; - -template <> -std::string Loggable::to_string() const -{ - return _t.toLogString(); -} -template <> -std::string Loggable::to_string() const -{ - return _t.toLogString(); -} -template <> -std::string Loggable::to_string() const -{ - return _t; -} -template <> -std::string Loggable>::to_string() const -{ - std::ostringstream oss; - bool first = true; - for (const auto& e : _t) { - if (!first) { - oss << ' '; - } - else { - first = false; - } - oss << e; - } - return oss.str(); -} - -template <> -std::string Loggable>::to_string() const -{ - std::ostringstream oss; - bool first = true; - for (const auto& e : _t) { - if (!first) { - oss << ' '; - } - else { - first = false; - } - oss << e; - } - return oss.str(); -} - std::shared_ptr Logger::withName(const std::string& name) const { std::shared_ptr res; diff --git a/pdns/recursordist/rec-main.cc b/pdns/recursordist/rec-main.cc index 06e4c86a01..a1ee629a87 100644 --- a/pdns/recursordist/rec-main.cc +++ b/pdns/recursordist/rec-main.cc @@ -172,7 +172,7 @@ static void setCPUMap(const std::map>& cpusMap, unsi g_log << Logger::Info << endl; } else { - log->info(Logr::Info, "CPU affinity has been set", "thread", Logging::Loggable(n), "cpumap", Logging::Loggable(cpuMapping->second)); + log->info(Logr::Info, "CPU affinity has been set", "thread", Logging::Loggable(n), "cpumap", Logging::IterLoggable(cpuMapping->second.begin(), cpuMapping->second.end())); } } else { @@ -184,7 +184,7 @@ static void setCPUMap(const std::map>& cpusMap, unsi g_log << Logger::Info << ' ' << strerror(rc) << endl; } else { - log->info(Logr::Warning, "Error setting CPU affinity", "thread", Logging::Loggable(n), "cpumap", Logging::Loggable(cpuMapping->second)); + log->error(Logr::Warning, rc, "Error setting CPU affinity", "thread", Logging::Loggable(n), "cpumap", Logging::IterLoggable(cpuMapping->second.begin(), cpuMapping->second.end())); } } } @@ -993,7 +993,7 @@ static std::shared_ptr parseACL(const std::string& aclFile, const g_log << Logger::Info << endl; } else { - log->info(Logr::Info, "Setting access control", "acl", Logging::Loggable(aclSetting), "addresses", Logging::Loggable(ips)); + log->info(Logr::Info, "Setting access control", "acl", Logging::Loggable(aclSetting), "addresses", Logging::IterLoggable(ips.begin(), ips.end())); } } @@ -1319,7 +1319,7 @@ static int serviceMain(int argc, char* argv[], std::shared_ptr& lo g_log << Logger::Warning << endl; } else { - log->info(Logr::Notice, "Will not send queries to", "addresses", Logging::Loggable(ips)); + log->info(Logr::Notice, "Will not send queries to", "addresses", Logging::IterLoggable(ips.begin(), ips.end())); } } diff --git a/pdns/reczones.cc b/pdns/reczones.cc index 091a706bf3..24ca8cf486 100644 --- a/pdns/reczones.cc +++ b/pdns/reczones.cc @@ -317,7 +317,7 @@ static void convertServersForAD(const std::string& zone, const std::string& inpu g_log << endl; } else { - log->info(Logr::Info, "Redirecting queries", "zone", Logging::Loggable(zone), "recursion", Logging::Loggable(ad.d_rdForward), "addresses", Logging::Loggable(addresses)); + log->info(Logr::Info, "Redirecting queries", "zone", Logging::Loggable(zone), "recursion", Logging::Loggable(ad.d_rdForward), "addresses", Logging::IterLoggable(addresses.begin(), addresses.end())); } } } @@ -548,7 +548,7 @@ std::tuple, std::shared_ptr> ifstream ifs(fname.c_str()); if (!ifs) { SLOG(g_log << Logger::Warning << "Could not open " << fname << " for reading" << endl, - log->error(Logr::Warning, "Could not open file for reading", "file", Logging::Loggable(fname))); + log->error(Logr::Warning, "Could not open file for reading", "file", Logging::Loggable(fname))); } else { string searchSuffix = ::arg()["export-etc-hosts-search-suffix"];