]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: add --log-timestamps flag
authorPeter van Dijk <peter.van.dijk@powerdns.com>
Thu, 3 Mar 2022 14:04:11 +0000 (15:04 +0100)
committerPeter van Dijk <peter.van.dijk@powerdns.com>
Thu, 10 Mar 2022 16:02:55 +0000 (17:02 +0100)
pdns/dnsdist.cc
pdns/dnsdistdist/docs/manpages/dnsdist.1.rst
pdns/dnsdistdist/test-dnsdistlbpolicies_cc.cc
pdns/dolog.hh
pdns/test-dnscrypt_cc.cc

index 44be1ef507c6e9e368419b679c173a13f5efa9e2..24a5c5bd2bb6c50f5ab8ca5de087b0d9e69c2fa7 100644 (file)
@@ -95,6 +95,7 @@ struct DNSDistStats g_stats;
 uint16_t g_maxOutstanding{std::numeric_limits<uint16_t>::max()};
 uint32_t g_staleCacheEntriesTTL{0};
 bool g_syslog{true};
+bool g_logtimestamps{false};
 bool g_allowEmptyResponse{false};
 
 GlobalStateHolder<NetmaskGroup> g_ACL;
@@ -2140,6 +2141,7 @@ static void usage()
   cout<<"                        (use with e.g. systemd and daemontools)\n";
   cout<<"--disable-syslog      Don't log to syslog, only to stdout\n";
   cout<<"                        (use with e.g. systemd)\n";
+  cout<<"--log-timestamps      Prepend timestamps to messages logged to stdout.\n";
   cout<<"-u,--uid uid          Change the process user ID after binding sockets\n";
   cout<<"-v,--verbose          Enable verbose mode\n";
   cout<<"-V,--version          Show dnsdist version information and exit\n";
@@ -2207,6 +2209,7 @@ int main(int argc, char** argv)
       {"gid", required_argument, 0, 'g'},
       {"help", no_argument, 0, 'h'},
       {"local", required_argument, 0, 'l'},
+      {"log-timestamps", no_argument, 0, 4},
       {"setkey", required_argument, 0, 'k'},
       {"supervised", no_argument, 0, 3},
       {"uid", required_argument, 0, 'u'},
@@ -2230,6 +2233,9 @@ int main(int argc, char** argv)
       case 3:
         g_cmdLine.beSupervised=true;
         break;
+      case 4:
+        g_logtimestamps=true;
+        break;
       case 'C':
         g_cmdLine.config=optarg;
         break;
index 0b8ea74b657a7dd16d7595f035f67a3453c79a8a..8a0eddc6d9f01461158c53ab4785ec6daca1fd90 100644 (file)
@@ -69,6 +69,7 @@ Options
                                        daemontools).
 --disable-syslog                       Disable logging to syslog. Use this when running inside a supervisor
                                        that handles logging (like systemd).
+--log-timestamps                       Prepend timestamps to messages logged to standard out.
 -u, --uid <uid>                        Change the process user to *uid* after binding sockets. *uid* can be
                                        a name or number.
 -g, --gid <gid>                        Change the process group to *gid* after binding sockets. *gid* Can
index 3ae268d27a347b02486111f0691a3e99ace3d812..9eee1cca51b26699897f438f8a6f2c3f40ab7608 100644 (file)
@@ -21,6 +21,7 @@ DNSDistSNMPAgent* g_snmpAgent{nullptr};
 #if BENCH_POLICIES
 bool g_verbose{true};
 bool g_syslog{true};
+bool g_logtimestamps{false};
 #include "dnsdist-rings.hh"
 Rings g_rings;
 GlobalStateHolder<NetmaskTree<DynBlock>> g_dynblockNMG;
index 557c767aadeecb1933c978324e974c854efb469f..fd6097dd105dbb8793845b0464ba48a4a91512a4 100644 (file)
@@ -22,6 +22,7 @@
 #pragma once
 #include <iostream>
 #include <sstream>
+#include "config.h"
 #if !defined(RECURSOR)
 #include <syslog.h>
 #else
@@ -75,6 +76,9 @@ void dolog(std::ostream& os, const char* s, T value, Args... args)
 
 extern bool g_verbose;
 extern bool g_syslog;
+#ifdef DNSDIST
+extern bool g_logtimestamps;
+#endif
 
 inline void setSyslogFacility(int facility)
 {
@@ -88,8 +92,22 @@ void genlog(int level, const char* s, Args... args)
 {
   std::ostringstream str;
   dolog(str, s, args...);
+
   if(g_syslog)
     syslog(level, "%s", str.str().c_str());
+
+#ifdef DNSDIST
+  if (g_logtimestamps) {
+    char buffer[50] = "";
+    struct tm tm;
+    time_t t;
+    time(&t);
+    localtime_r(&t, &tm);
+    strftime(buffer, sizeof(buffer), "%b %d %H:%M:%S ", &tm);
+    std::cout<<buffer;
+  }
+#endif
+
   std::cout<<str.str()<<std::endl;
 }
 
index 8d834ca9b3af29eaa7f10b30217c863a2c4c40f0..5e0174e442b6acec98981968315ce64c2ca1accc 100644 (file)
@@ -32,6 +32,7 @@
 
 bool g_verbose{false};
 bool g_syslog{true};
+bool g_logtimestamps{false};
 
 BOOST_AUTO_TEST_SUITE(test_dnscrypt_cc)