summaryrefslogtreecommitdiffstats
path: root/dolog.hh
diff options
context:
space:
mode:
Diffstat (limited to 'dolog.hh')
-rw-r--r--dolog.hh268
1 files changed, 172 insertions, 96 deletions
diff --git a/dolog.hh b/dolog.hh
index a28777a..25c4952 100644
--- a/dolog.hh
+++ b/dolog.hh
@@ -20,7 +20,9 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
+#include <array>
#include <fstream>
+#include <iomanip>
#include <iostream>
#include <optional>
#include <sstream>
@@ -31,13 +33,12 @@
#include "logger.hh"
#endif // RECURSOR
-
/* This file is intended not to be metronome specific, and is simple example of C++2011
variadic templates in action.
- The goal is rapid easy to use logging to console & syslog.
+ The goal is rapid easy to use logging to console & syslog.
- Usage:
+ Usage:
string address="localhost";
vinfolog("Got TCP connection from %s", remote);
infolog("Bound to %s port %d", address, port);
@@ -50,162 +51,237 @@
This will happily print a string to %d! Doesn't do further format processing.
*/
-
-#if !defined(RECURSOR)
-inline void dolog(std::ostream& os, const char*s)
+template <typename O>
+inline void dolog(O& outputStream, const char* str)
{
- os<<s;
+ outputStream << str;
}
-template<typename T, typename... Args>
-void dolog(std::ostream& os, const char* s, T value, Args... args)
+template <typename O, typename T, typename... Args>
+void dolog(O& outputStream, const char* formatStr, T value, const Args&... args)
{
- while (*s) {
- if (*s == '%') {
- if (*(s + 1) == '%') {
- ++s;
+ while (*formatStr) {
+ if (*formatStr == '%') {
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
+ if (*(formatStr + 1) == '%') {
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
+ ++formatStr;
}
else {
- os << value;
- s += 2;
- dolog(os, s, args...);
- return;
+ outputStream << value;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
+ formatStr += 2;
+ dolog(outputStream, formatStr, args...);
+ return;
}
}
- os << *s++;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
+ outputStream << *formatStr++;
}
}
+#if !defined(RECURSOR)
+// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
extern bool g_verbose;
-extern bool g_syslog;
+
#ifdef DNSDIST
-extern bool g_logtimestamps;
-extern std::optional<std::ofstream> g_verboseStream;
+namespace dnsdist::logging
+{
+class LoggingConfiguration
+{
+public:
+ enum class TimeFormat
+ {
+ Numeric,
+ ISO8601
+ };
+
+ static void setVerbose(bool value = true)
+ {
+ g_verbose = value;
+ }
+ static void setSyslog(bool value = true)
+ {
+ s_syslog = value;
+ }
+ static void setStructuredLogging(bool value = true, std::string levelPrefix = "")
+ {
+ s_structuredLogging = value;
+ if (value) {
+ s_structuredLevelPrefix = levelPrefix.empty() ? "prio" : std::move(levelPrefix);
+ }
+ }
+ static void setLogTimestamps(bool value = true)
+ {
+ s_logTimestamps = value;
+ }
+ static void setStructuredTimeFormat(TimeFormat format)
+ {
+ s_structuredTimeFormat = format;
+ }
+ static void setVerboseStream(std::ofstream&& stream)
+ {
+ s_verboseStream = std::move(stream);
+ }
+ static bool getVerbose()
+ {
+ return g_verbose;
+ }
+ static bool getSyslog()
+ {
+ return s_syslog;
+ }
+ static bool getLogTimestamps()
+ {
+ return s_logTimestamps;
+ }
+ static std::optional<std::ofstream>& getVerboseStream()
+ {
+ return s_verboseStream;
+ }
+ static bool getStructuredLogging()
+ {
+ return s_structuredLogging;
+ }
+ static const std::string& getStructuredLoggingLevelPrefix()
+ {
+ return s_structuredLevelPrefix;
+ }
+
+ static TimeFormat getStructuredLoggingTimeFormat()
+ {
+ return s_structuredTimeFormat;
+ }
+
+private:
+ static std::optional<std::ofstream> s_verboseStream;
+ static std::string s_structuredLevelPrefix;
+ static TimeFormat s_structuredTimeFormat;
+ static bool s_structuredLogging;
+ static bool s_logTimestamps;
+ static bool s_syslog;
+};
+
+extern void logTime(std::ostream& stream);
+}
#endif
inline void setSyslogFacility(int facility)
{
/* we always call openlog() right away at startup */
closelog();
- openlog("dnsdist", LOG_PID|LOG_NDELAY, facility);
+ openlog("dnsdist", LOG_PID | LOG_NDELAY, facility);
+}
+
+namespace
+{
+inline const char* syslogLevelToStr(int level)
+{
+ static constexpr std::array levelStrs{
+ "Emergency",
+ "Alert",
+ "Critical",
+ "Error",
+ "Warning",
+ "Notice",
+ "Info",
+ "Debug"};
+ return levelStrs.at(level);
+}
}
-template<typename... Args>
-void genlog(std::ostream& stream, int level, bool doSyslog, const char* s, Args... args)
+template <typename... Args>
+void genlog(std::ostream& stream, [[maybe_unused]] int level, [[maybe_unused]] bool skipSyslog, const char* formatStr, const Args&... args)
{
std::ostringstream str;
- dolog(str, s, args...);
+ dolog(str, formatStr, args...);
auto output = str.str();
- if (doSyslog) {
+#ifdef DNSDIST
+ if (!skipSyslog && dnsdist::logging::LoggingConfiguration::getSyslog()) {
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg): syslog is what it is
syslog(level, "%s", output.c_str());
}
-#ifdef DNSDIST
- if (g_logtimestamps) {
- char buffer[50] = "";
- struct tm tm;
- time_t t;
- time(&t);
- localtime_r(&t, &tm);
- if (strftime(buffer, sizeof(buffer), "%b %d %H:%M:%S ", &tm) == 0) {
- buffer[0] = '\0';
- }
- stream<<buffer;
+ if (dnsdist::logging::LoggingConfiguration::getLogTimestamps()) {
+ dnsdist::logging::logTime(stream);
}
-#endif
- stream<<output<<std::endl;
+ if (dnsdist::logging::LoggingConfiguration::getStructuredLogging()) {
+ stream << dnsdist::logging::LoggingConfiguration::getStructuredLoggingLevelPrefix() << "=\"" << syslogLevelToStr(level) << "\" ";
+ stream << "msg=" << std::quoted(output) << std::endl;
+ }
+ else {
+ stream << output << std::endl;
+ }
+#else
+ stream << output << std::endl;
+#endif
}
-template<typename... Args>
-void verboselog(const char* s, Args... args)
+template <typename... Args>
+void verboselog(const char* formatStr, const Args&... args)
{
#ifdef DNSDIST
- if (g_verboseStream) {
- genlog(*g_verboseStream, LOG_DEBUG, false, s, args...);
+ if (auto& stream = dnsdist::logging::LoggingConfiguration::getVerboseStream()) {
+ genlog(*stream, LOG_DEBUG, true, formatStr, args...);
}
else {
#endif /* DNSDIST */
- genlog(std::cout, LOG_DEBUG, g_syslog, s, args...);
+ genlog(std::cout, LOG_DEBUG, false, formatStr, args...);
#ifdef DNSDIST
}
#endif /* DNSDIST */
}
-#define vinfolog if (g_verbose) verboselog
+#define vinfolog \
+ if (g_verbose) \
+ verboselog
-template<typename... Args>
-void infolog(const char* s, Args... args)
+template <typename... Args>
+void infolog(const char* formatStr, const Args&... args)
{
- genlog(std::cout, LOG_INFO, g_syslog, s, args...);
+ genlog(std::cout, LOG_INFO, false, formatStr, args...);
}
-template<typename... Args>
-void warnlog(const char* s, Args... args)
+template <typename... Args>
+void warnlog(const char* formatStr, const Args&... args)
{
- genlog(std::cout, LOG_WARNING, g_syslog, s, args...);
+ genlog(std::cout, LOG_WARNING, false, formatStr, args...);
}
-template<typename... Args>
-void errlog(const char* s, Args... args)
+template <typename... Args>
+void errlog(const char* formatStr, const Args&... args)
{
- genlog(std::cout, LOG_ERR, g_syslog, s, args...);
+ genlog(std::cout, LOG_ERR, false, formatStr, args...);
}
#else // RECURSOR
-
#define g_verbose 0
+#define vinfolog \
+ if (g_verbose) \
+ infolog
-inline void dolog(Logger::Urgency u, const char* s)
-{
- g_log << u << s << std::endl;
-}
-
-inline void dolog(const char* s)
-{
- g_log << s << std::endl;
-}
-
-template<typename T, typename... Args>
-void dolog(Logger::Urgency u, const char* s, T value, Args... args)
-{
- g_log << u;
- while (*s) {
- if (*s == '%') {
- if (*(s + 1) == '%') {
- ++s;
- }
- else {
- g_log << value;
- s += 2;
- dolog(s, args...);
- return;
- }
- }
- g_log << *s++;
- }
-}
-
-#define vinfolog if(g_verbose)infolog
-
-template<typename... Args>
-void infolog(const char* s, Args... args)
+template <typename... Args>
+void infolog(const char* formatStr, const Args&... args)
{
- dolog(Logger::Info, s, args...);
+ g_log << Logger::Info;
+ dolog(g_log, formatStr, args...);
}
-template<typename... Args>
-void warnlog(const char* s, Args... args)
+template <typename... Args>
+void warnlog(const char* formatStr, const Args&... args)
{
- dolog(Logger::Warning, s, args...);
+ g_log << Logger::Warning;
+ dolog(g_log, formatStr, args...);
}
-template<typename... Args>
-void errlog(const char* s, Args... args)
+template <typename... Args>
+void errlog(const char* formatStr, const Args&... args)
{
- dolog(Logger::Error, s, args...);
+ g_log << Logger::Error;
+ dolog(g_log, formatStr, args...);
}
#endif