From: Peter van Dijk Date: Thu, 3 Mar 2022 14:04:11 +0000 (+0100) Subject: dnsdist: add --log-timestamps flag X-Git-Tag: rec-4.7.0-beta1~68^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=927df82c5aa4518b48679c4aaf59cda949fa1c2d;p=thirdparty%2Fpdns.git dnsdist: add --log-timestamps flag --- diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index 44be1ef507..24a5c5bd2b 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -95,6 +95,7 @@ struct DNSDistStats g_stats; uint16_t g_maxOutstanding{std::numeric_limits::max()}; uint32_t g_staleCacheEntriesTTL{0}; bool g_syslog{true}; +bool g_logtimestamps{false}; bool g_allowEmptyResponse{false}; GlobalStateHolder 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; diff --git a/pdns/dnsdistdist/docs/manpages/dnsdist.1.rst b/pdns/dnsdistdist/docs/manpages/dnsdist.1.rst index 0b8ea74b65..8a0eddc6d9 100644 --- a/pdns/dnsdistdist/docs/manpages/dnsdist.1.rst +++ b/pdns/dnsdistdist/docs/manpages/dnsdist.1.rst @@ -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 Change the process user to *uid* after binding sockets. *uid* can be a name or number. -g, --gid Change the process group to *gid* after binding sockets. *gid* Can diff --git a/pdns/dnsdistdist/test-dnsdistlbpolicies_cc.cc b/pdns/dnsdistdist/test-dnsdistlbpolicies_cc.cc index 3ae268d27a..9eee1cca51 100644 --- a/pdns/dnsdistdist/test-dnsdistlbpolicies_cc.cc +++ b/pdns/dnsdistdist/test-dnsdistlbpolicies_cc.cc @@ -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> g_dynblockNMG; diff --git a/pdns/dolog.hh b/pdns/dolog.hh index 557c767aad..fd6097dd10 100644 --- a/pdns/dolog.hh +++ b/pdns/dolog.hh @@ -22,6 +22,7 @@ #pragma once #include #include +#include "config.h" #if !defined(RECURSOR) #include #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<