From dfd0f3c9a314b904c53d801f0232eb3b70a180a0 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Thu, 19 May 2022 13:06:57 +0200 Subject: [PATCH] Try to be smart: if to_string() or toLogString() is there, use it. --- pdns/logging.hh | 62 +++++++++++++++++++++++++---------- pdns/pdns_recursor.cc | 2 +- pdns/recursordist/rec-main.cc | 2 +- pdns/recursordist/rec-tcp.cc | 2 +- 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/pdns/logging.hh b/pdns/logging.hh index 01e0d7a685..f4ed25c0d7 100644 --- a/pdns/logging.hh +++ b/pdns/logging.hh @@ -50,6 +50,36 @@ struct Entry Logr::Priority d_priority; // (syslog) priority) }; +// Warning: some meta-programming is going on. We define helper +// templates that can be used to see if specific string output +// functions are available. If so, we use those instead of << into an +// ostringstream. Note that this decision happpens compile time. +// Some hints taken from https://www.cppstories.com/2019/07/detect-overload-from-chars/ +// (I could not get function templates with enabled_if<> to work in this case) +// +// Default: std::string(T) is not available +template +struct is_to_string_available : std::false_type +{ +}; + +// If std::string(T) is available this template is used +template +struct is_to_string_available()))>> : std::true_type +{ +}; + +// Same mechanism for t.toLogString() +template +struct is_toLogString_available : std::false_type +{ +}; + +template +struct is_toLogString_available().toLogString())>> : std::true_type +{ +}; + template struct Loggable : public Logr::Loggable { @@ -60,26 +90,22 @@ struct Loggable : public Logr::Loggable } std::string to_string() const { - std::ostringstream oss; - oss << _t; - return oss.str(); + if constexpr (std::is_same_v) { + return _t; + } + else if constexpr (is_to_string_available::value) { + return std::to_string(_t); + } + else if constexpr (is_toLogString_available::value) { + return _t.toLogString(); + } + else { + std::ostringstream oss; + oss << _t; + return oss.str(); + } } }; -template <> -inline std::string Loggable::to_string() const -{ - return _t.toLogString(); -} -template <> -inline std::string Loggable::to_string() const -{ - return _t.toLogString(); -} -template <> -inline std::string Loggable::to_string() const -{ - return _t; -} template struct IterLoggable : public Logr::Loggable diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 5e4cdb9d8f..8fd633136b 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -2384,7 +2384,7 @@ void makeUDPServerSockets(deferredAdd_t& deferredAdds, std::shared_ptrinfo(Logr::Info, "Listening for queries", "protocol", Logging::Loggable("UDP"), "address", Logging::Loggable(sin.toStringWithPort()))); + log->info(Logr::Info, "Listening for queries", "protocol", Logging::Loggable("UDP"), "address", Logging::Loggable(sin))); } } diff --git a/pdns/recursordist/rec-main.cc b/pdns/recursordist/rec-main.cc index a1ee629a87..4c6baded67 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::IterLoggable(cpuMapping->second.begin(), cpuMapping->second.end())); + log->info(Logr::Info, "CPU affinity has been set", "thread", Logging::Loggable(n), "cpumap", Logging::IterLoggable(cpuMapping->second.begin(), cpuMapping->second.end())); } } else { diff --git a/pdns/recursordist/rec-tcp.cc b/pdns/recursordist/rec-tcp.cc index f3ae5393ab..599350af59 100644 --- a/pdns/recursordist/rec-tcp.cc +++ b/pdns/recursordist/rec-tcp.cc @@ -1100,6 +1100,6 @@ void makeTCPServerSockets(deferredAdd_t& deferredAdds, std::set& tcpSockets // we don't need to update g_listenSocketsAddresses since it doesn't work for TCP/IP: // - fd is not that which we know here, but returned from accept() SLOG(g_log << Logger::Info << "Listening for TCP queries on " << sin.toStringWithPort() << endl, - log->info(Logr::Info, "Listening for queries", "protocol", Logging::Loggable("TCP"), "address", Logging::Loggable(sin.toStringWithPort()))); + log->info(Logr::Info, "Listening for queries", "protocol", Logging::Loggable("TCP"), "address", Logging::Loggable(sin))); } } -- 2.47.2