2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #if !defined(RECURSOR)
35 /* This file is intended not to be metronome specific, and is simple example of C++2011
36 variadic templates in action.
38 The goal is rapid easy to use logging to console & syslog.
41 string address="localhost";
42 vinfolog("Got TCP connection from %s", remote);
43 infolog("Bound to %s port %d", address, port);
44 warnlog("Query took %d milliseconds", 1232.4); // yes, %d
45 errlog("Unable to bind to %s: %s", ca.toStringWithPort(), strerr(errno));
47 Will log to stdout. Will syslog in any case with LOG_INFO,
48 LOG_WARNING, LOG_ERR respectively. If g_verbose=false, vinfolog is a noop.
49 More generically, dolog(someiostream, "Hello %s", stream) will log to someiostream
51 This will happily print a string to %d! Doesn't do further format processing.
54 #if !defined(RECURSOR)
55 inline void dolog(std::ostream& os, const char*s)
60 template<typename T, typename... Args>
61 void dolog(std::ostream& os, const char* s, T value, Args... args)
65 if (*(s + 1) == '%') {
71 dolog(os, s, args...);
79 extern bool g_verbose;
82 extern bool g_logtimestamps;
83 extern std::optional<std::ofstream> g_verboseStream;
86 inline void setSyslogFacility(int facility)
88 /* we always call openlog() right away at startup */
90 openlog("dnsdist", LOG_PID|LOG_NDELAY, facility);
93 template<typename... Args>
94 void genlog(std::ostream& stream, int level, bool doSyslog, const char* s, Args... args)
96 std::ostringstream str;
97 dolog(str, s, args...);
99 auto output = str.str();
102 syslog(level, "%s", output.c_str());
106 if (g_logtimestamps) {
107 char buffer[50] = "";
111 localtime_r(&t, &tm);
112 if (strftime(buffer, sizeof(buffer), "%b %d %H:%M:%S ", &tm) == 0) {
119 stream<<output<<std::endl;
122 template<typename... Args>
123 void verboselog(const char* s, Args... args)
126 if (g_verboseStream) {
127 genlog(*g_verboseStream, LOG_DEBUG, false, s, args...);
131 genlog(std::cout, LOG_DEBUG, g_syslog, s, args...);
137 #define vinfolog if (g_verbose) verboselog
139 template<typename... Args>
140 void infolog(const char* s, Args... args)
142 genlog(std::cout, LOG_INFO, g_syslog, s, args...);
145 template<typename... Args>
146 void warnlog(const char* s, Args... args)
148 genlog(std::cout, LOG_WARNING, g_syslog, s, args...);
151 template<typename... Args>
152 void errlog(const char* s, Args... args)
154 genlog(std::cout, LOG_ERR, g_syslog, s, args...);
161 inline void dolog(Logger::Urgency u, const char* s)
163 g_log << u << s << std::endl;
166 inline void dolog(const char* s)
168 g_log << s << std::endl;
171 template<typename T, typename... Args>
172 void dolog(Logger::Urgency u, const char* s, T value, Args... args)
177 if (*(s + 1) == '%') {
191 #define vinfolog if(g_verbose)infolog
193 template<typename... Args>
194 void infolog(const char* s, Args... args)
196 dolog(Logger::Info, s, args...);
199 template<typename... Args>
200 void warnlog(const char* s, Args... args)
202 dolog(Logger::Warning, s, args...);
205 template<typename... Args>
206 void errlog(const char* s, Args... args)
208 dolog(Logger::Error, s, args...);