From: Remi Gacogne Date: Wed, 1 Jun 2022 14:59:04 +0000 (+0200) Subject: dnsdist: Add an option to send 'verbose' messages to a log file X-Git-Tag: auth-4.8.0-alpha0~78^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4935d3cabac87db3fdf2a61c6c84978451f2ab17;p=thirdparty%2Fpdns.git dnsdist: Add an option to send 'verbose' messages to a log file --- diff --git a/pdns/dnsdist-console.cc b/pdns/dnsdist-console.cc index 73beb01aec..fbeb3b2ad3 100644 --- a/pdns/dnsdist-console.cc +++ b/pdns/dnsdist-console.cc @@ -709,6 +709,7 @@ const std::vector g_consoleKeywords{ { "setUDPTimeout", true, "n", "set the maximum time dnsdist will wait for a response from a backend over UDP, in seconds" }, { "setVerbose", true, "bool", "set whether log messages at the verbose level will be logged" }, { "setVerboseHealthChecks", true, "bool", "set whether health check errors will be logged" }, + { "setVerboseLogDestination", true, "destination file", "Set a destination file to write the 'verbose' log messages to, instead of sending them to syslog and/or the standard output" }, { "setWebserverConfig", true, "[{password=string, apiKey=string, customHeaders, statsRequireAuthentication}]", "Updates webserver configuration" }, { "setWeightedBalancingFactor", true, "factor", "Set the balancing factor for bounded-load weighted policies (whashed, wrandom)" }, { "setWHashedPertubation", true, "value", "Set the hash perturbation value to be used in the whashed policy instead of a random one, allowing to have consistent whashed results on different instance" }, diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index a2f2fac7c6..2c27857899 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -1720,6 +1720,19 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) luaCtx.writeFunction("setVerbose", [](bool verbose) { g_verbose = verbose; }); luaCtx.writeFunction("getVerbose", []() { return g_verbose; }); luaCtx.writeFunction("setVerboseHealthChecks", [](bool verbose) { g_verboseHealthChecks = verbose; }); + luaCtx.writeFunction("setVerboseLogDestination", [](const std::string& dest) { + if (g_configurationDone) { + g_outputBuffer = "setVerboseLogDestination() cannot be used at runtime!\n"; + return; + } + try { + auto stream = std::ofstream(dest.c_str()); + g_verboseStream = std::move(stream); + } + catch (const std::exception& e) { + errlog("Error while opening the verbose logging destination file %s: %s", dest, e.what()); + } + }); luaCtx.writeFunction("setStaleCacheEntriesTTL", [](uint64_t ttl) { checkParameterBound("setStaleCacheEntriesTTL", ttl, std::numeric_limits::max()); diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index a4805d03e4..3c98520d6a 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -91,6 +91,7 @@ using std::thread; bool g_verbose; +std::optional g_verboseStream{std::nullopt}; struct DNSDistStats g_stats; diff --git a/pdns/dnsdistdist/docs/reference/config.rst b/pdns/dnsdistdist/docs/reference/config.rst index 8c0f703e62..8d3016bda4 100644 --- a/pdns/dnsdistdist/docs/reference/config.rst +++ b/pdns/dnsdistdist/docs/reference/config.rst @@ -1089,6 +1089,15 @@ Status, Statistics and More :param bool verbose: Set to true if you want to enable health check errors logging +.. function:: setVerboseLogDestination(dest) + + .. versionadded:: 1.8.0 + + Set a destination file to write the 'verbose' log messages to, instead of sending them to syslog and/or the standard output which is the default. + Note that these messages will no longer be sent to syslog or the standard output once that option has been set. + + :param str dest: The destination file + .. function:: showBinds() Print a list of all the current addresses and ports dnsdist is listening on, also called ``frontends`` diff --git a/pdns/dolog.hh b/pdns/dolog.hh index bb554ad470..e363eff83d 100644 --- a/pdns/dolog.hh +++ b/pdns/dolog.hh @@ -20,7 +20,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #pragma once +#include #include +#include #include #include "config.h" #if !defined(RECURSOR) @@ -78,6 +80,7 @@ extern bool g_verbose; extern bool g_syslog; #ifdef DNSDIST extern bool g_logtimestamps; +extern std::optional g_verboseStream; #endif inline void setSyslogFacility(int facility) @@ -88,14 +91,14 @@ inline void setSyslogFacility(int facility) } template -void genlog(int level, const char* s, Args... args) +void genlog(std::ostream& stream, int level, bool doSyslog, const char* s, Args... args) { std::ostringstream str; dolog(str, s, args...); auto output = str.str(); - if (g_syslog) { + if (doSyslog) { syslog(level, "%s", output.c_str()); } @@ -109,17 +112,22 @@ void genlog(int level, const char* s, Args... args) if (strftime(buffer, sizeof(buffer), "%b %d %H:%M:%S ", &tm) == 0) { buffer[0] = '\0'; } - std::cout< void verboselog(const char* s, Args... args) { - genlog(LOG_DEBUG, s, args...); + if (g_verboseStream) { + genlog(*g_verboseStream, LOG_DEBUG, false, s, args...); + } + else { + genlog(std::cout, LOG_DEBUG, g_syslog, s, args...); + } } #define vinfolog if (g_verbose) verboselog @@ -127,22 +135,21 @@ void verboselog(const char* s, Args... args) template void infolog(const char* s, Args... args) { - genlog(LOG_INFO, s, args...); + genlog(std::cout, LOG_INFO, g_syslog, s, args...); } template void warnlog(const char* s, Args... args) { - genlog(LOG_WARNING, s, args...); + genlog(std::cout, LOG_WARNING, g_syslog, s, args...); } template void errlog(const char* s, Args... args) { - genlog(LOG_ERR, s, args...); + genlog(std::cout, LOG_ERR, g_syslog, s, args...); } - #else // RECURSOR #define g_verbose 0 diff --git a/pdns/test-dnscrypt_cc.cc b/pdns/test-dnscrypt_cc.cc index 5e0174e442..f19010c738 100644 --- a/pdns/test-dnscrypt_cc.cc +++ b/pdns/test-dnscrypt_cc.cc @@ -28,11 +28,13 @@ #include "dnsname.hh" #include "dnsparser.hh" #include "dnswriter.hh" +#include "dolog.hh" #include bool g_verbose{false}; bool g_syslog{true}; bool g_logtimestamps{false}; +std::optional g_verboseStream{std::nullopt}; BOOST_AUTO_TEST_SUITE(test_dnscrypt_cc)