]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Add an option to send 'verbose' messages to a log file
authorRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 1 Jun 2022 14:59:04 +0000 (16:59 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 1 Jun 2022 15:00:46 +0000 (17:00 +0200)
pdns/dnsdist-console.cc
pdns/dnsdist-lua.cc
pdns/dnsdist.cc
pdns/dnsdistdist/docs/reference/config.rst
pdns/dolog.hh
pdns/test-dnscrypt_cc.cc

index 73beb01aecbe94c48090e3dd957591a76ad690da..fbeb3b2ad39700e0be9cfe66820518b39810ccfc 100644 (file)
@@ -709,6 +709,7 @@ const std::vector<ConsoleKeyword> 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" },
index a2f2fac7c6d8484b19480a66836c2a8144805db0..2c278578992bd057158aca8ae220a8ab80fdda97 100644 (file)
@@ -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<uint32_t>::max());
index a4805d03e4cd58304464f76454015da00d814f36..3c98520d6a42a2a7bc2558c13052b6054037a287 100644 (file)
@@ -91,6 +91,7 @@
 
 using std::thread;
 bool g_verbose;
+std::optional<std::ofstream> g_verboseStream{std::nullopt};
 
 struct DNSDistStats g_stats;
 
index 8c0f703e62f38f49e31df2c4deaa858d635e3190..8d3016bda4904ff20901808a40959d32d72768ee 100644 (file)
@@ -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``
index bb554ad470b32e2c380c82b61b9095190b56bb05..e363eff83d1a76dd374f32eddb77eacc1c9a186d 100644 (file)
@@ -20,7 +20,9 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 #pragma once
+#include <fstream>
 #include <iostream>
+#include <optional>
 #include <sstream>
 #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<std::ofstream> g_verboseStream;
 #endif
 
 inline void setSyslogFacility(int facility)
@@ -88,14 +91,14 @@ inline void setSyslogFacility(int facility)
 }
 
 template<typename... Args>
-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<<buffer;
+    stream<<buffer;
   }
 #endif
 
-  std::cout<<output<<std::endl;
+  stream<<output<<std::endl;
 }
 
 template<typename... Args>
 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<typename... Args>
 void infolog(const char* s, Args... args)
 {
-  genlog(LOG_INFO, s, args...);
+  genlog(std::cout, LOG_INFO, g_syslog, s, args...);
 }
 
 template<typename... Args>
 void warnlog(const char* s, Args... args)
 {
-  genlog(LOG_WARNING, s, args...);
+  genlog(std::cout, LOG_WARNING, g_syslog, s, args...);
 }
 
 template<typename... Args>
 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
index 5e0174e442b6acec98981968315ce64c2ca1accc..f19010c738aee7d4d623c3ac23d87c5e50af68cd 100644 (file)
 #include "dnsname.hh"
 #include "dnsparser.hh"
 #include "dnswriter.hh"
+#include "dolog.hh"
 #include <unistd.h>
 
 bool g_verbose{false};
 bool g_syslog{true};
 bool g_logtimestamps{false};
+std::optional<std::ofstream> g_verboseStream{std::nullopt};
 
 BOOST_AUTO_TEST_SUITE(test_dnscrypt_cc)