]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dolog.hh
Merge pull request #11681 from omoerbeek/rec-sl-subsys
[thirdparty/pdns.git] / pdns / dolog.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
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.
8 *
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.
12 *
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.
17 *
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.
21 */
22 #pragma once
23 #include <fstream>
24 #include <iostream>
25 #include <optional>
26 #include <sstream>
27 #include "config.h"
28 #if !defined(RECURSOR)
29 #include <syslog.h>
30 #else
31 #include "logger.hh"
32 #endif // RECURSOR
33
34
35 /* This file is intended not to be metronome specific, and is simple example of C++2011
36 variadic templates in action.
37
38 The goal is rapid easy to use logging to console & syslog.
39
40 Usage:
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));
46
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
50
51 This will happily print a string to %d! Doesn't do further format processing.
52 */
53
54 #if !defined(RECURSOR)
55 inline void dolog(std::ostream& os, const char*s)
56 {
57 os<<s;
58 }
59
60 template<typename T, typename... Args>
61 void dolog(std::ostream& os, const char* s, T value, Args... args)
62 {
63 while (*s) {
64 if (*s == '%') {
65 if (*(s + 1) == '%') {
66 ++s;
67 }
68 else {
69 os << value;
70 s += 2;
71 dolog(os, s, args...);
72 return;
73 }
74 }
75 os << *s++;
76 }
77 }
78
79 extern bool g_verbose;
80 extern bool g_syslog;
81 #ifdef DNSDIST
82 extern bool g_logtimestamps;
83 extern std::optional<std::ofstream> g_verboseStream;
84 #endif
85
86 inline void setSyslogFacility(int facility)
87 {
88 /* we always call openlog() right away at startup */
89 closelog();
90 openlog("dnsdist", LOG_PID|LOG_NDELAY, facility);
91 }
92
93 template<typename... Args>
94 void genlog(std::ostream& stream, int level, bool doSyslog, const char* s, Args... args)
95 {
96 std::ostringstream str;
97 dolog(str, s, args...);
98
99 auto output = str.str();
100
101 if (doSyslog) {
102 syslog(level, "%s", output.c_str());
103 }
104
105 #ifdef DNSDIST
106 if (g_logtimestamps) {
107 char buffer[50] = "";
108 struct tm tm;
109 time_t t;
110 time(&t);
111 localtime_r(&t, &tm);
112 if (strftime(buffer, sizeof(buffer), "%b %d %H:%M:%S ", &tm) == 0) {
113 buffer[0] = '\0';
114 }
115 stream<<buffer;
116 }
117 #endif
118
119 stream<<output<<std::endl;
120 }
121
122 template<typename... Args>
123 void verboselog(const char* s, Args... args)
124 {
125 #ifdef DNSDIST
126 if (g_verboseStream) {
127 genlog(*g_verboseStream, LOG_DEBUG, false, s, args...);
128 }
129 else {
130 #endif /* DNSDIST */
131 genlog(std::cout, LOG_DEBUG, g_syslog, s, args...);
132 #ifdef DNSDIST
133 }
134 #endif /* DNSDIST */
135 }
136
137 #define vinfolog if (g_verbose) verboselog
138
139 template<typename... Args>
140 void infolog(const char* s, Args... args)
141 {
142 genlog(std::cout, LOG_INFO, g_syslog, s, args...);
143 }
144
145 template<typename... Args>
146 void warnlog(const char* s, Args... args)
147 {
148 genlog(std::cout, LOG_WARNING, g_syslog, s, args...);
149 }
150
151 template<typename... Args>
152 void errlog(const char* s, Args... args)
153 {
154 genlog(std::cout, LOG_ERR, g_syslog, s, args...);
155 }
156
157 #else // RECURSOR
158
159 #define g_verbose 0
160
161 inline void dolog(Logger::Urgency u, const char* s)
162 {
163 g_log << u << s << std::endl;
164 }
165
166 inline void dolog(const char* s)
167 {
168 g_log << s << std::endl;
169 }
170
171 template<typename T, typename... Args>
172 void dolog(Logger::Urgency u, const char* s, T value, Args... args)
173 {
174 g_log << u;
175 while (*s) {
176 if (*s == '%') {
177 if (*(s + 1) == '%') {
178 ++s;
179 }
180 else {
181 g_log << value;
182 s += 2;
183 dolog(s, args...);
184 return;
185 }
186 }
187 g_log << *s++;
188 }
189 }
190
191 #define vinfolog if(g_verbose)infolog
192
193 template<typename... Args>
194 void infolog(const char* s, Args... args)
195 {
196 dolog(Logger::Info, s, args...);
197 }
198
199 template<typename... Args>
200 void warnlog(const char* s, Args... args)
201 {
202 dolog(Logger::Warning, s, args...);
203 }
204
205 template<typename... Args>
206 void errlog(const char* s, Args... args)
207 {
208 dolog(Logger::Error, s, args...);
209 }
210
211 #endif
212