]>
Commit | Line | Data |
---|---|---|
12471842 PL |
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 | */ | |
64e4ebb4 | 22 | #pragma once |
4935d3ca | 23 | #include <fstream> |
64e4ebb4 | 24 | #include <iostream> |
4935d3ca | 25 | #include <optional> |
64e4ebb4 | 26 | #include <sstream> |
927df82c | 27 | #include "config.h" |
6f4d5e75 | 28 | #if !defined(RECURSOR) |
64e4ebb4 | 29 | #include <syslog.h> |
6f4d5e75 O |
30 | #else |
31 | #include "logger.hh" | |
32 | #endif // RECURSOR | |
33 | ||
64e4ebb4 | 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"; | |
fb7f8ec3 | 42 | vinfolog("Got TCP connection from %s", remote); |
64e4ebb4 | 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 | ||
9c9b4998 | 47 | Will log to stdout. Will syslog in any case with LOG_INFO, |
fb7f8ec3 | 48 | LOG_WARNING, LOG_ERR respectively. If g_verbose=false, vinfolog is a noop. |
64e4ebb4 | 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 | ||
6f4d5e75 | 54 | #if !defined(RECURSOR) |
64e4ebb4 | 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; | |
6f4d5e75 | 71 | dolog(os, s, args...); |
64e4ebb4 | 72 | return; |
73 | } | |
74 | } | |
75 | os << *s++; | |
6f4d5e75 | 76 | } |
64e4ebb4 | 77 | } |
78 | ||
64e4ebb4 | 79 | extern bool g_verbose; |
bbfaaa6f | 80 | extern bool g_syslog; |
927df82c PD |
81 | #ifdef DNSDIST |
82 | extern bool g_logtimestamps; | |
4935d3ca | 83 | extern std::optional<std::ofstream> g_verboseStream; |
927df82c | 84 | #endif |
64e4ebb4 | 85 | |
0ca6a67f RG |
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 | ||
64e4ebb4 | 93 | template<typename... Args> |
4935d3ca | 94 | void genlog(std::ostream& stream, int level, bool doSyslog, const char* s, Args... args) |
64e4ebb4 | 95 | { |
96 | std::ostringstream str; | |
97 | dolog(str, s, args...); | |
927df82c | 98 | |
996fd9d2 RG |
99 | auto output = str.str(); |
100 | ||
4935d3ca | 101 | if (doSyslog) { |
996fd9d2 RG |
102 | syslog(level, "%s", output.c_str()); |
103 | } | |
927df82c PD |
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); | |
87e6f000 PD |
112 | if (strftime(buffer, sizeof(buffer), "%b %d %H:%M:%S ", &tm) == 0) { |
113 | buffer[0] = '\0'; | |
114 | } | |
4935d3ca | 115 | stream<<buffer; |
927df82c PD |
116 | } |
117 | #endif | |
118 | ||
4935d3ca | 119 | stream<<output<<std::endl; |
64e4ebb4 | 120 | } |
121 | ||
37a8e048 RG |
122 | template<typename... Args> |
123 | void verboselog(const char* s, Args... args) | |
124 | { | |
bc5f8799 | 125 | #ifdef DNSDIST |
4935d3ca RG |
126 | if (g_verboseStream) { |
127 | genlog(*g_verboseStream, LOG_DEBUG, false, s, args...); | |
128 | } | |
129 | else { | |
bc5f8799 | 130 | #endif /* DNSDIST */ |
4935d3ca | 131 | genlog(std::cout, LOG_DEBUG, g_syslog, s, args...); |
bc5f8799 | 132 | #ifdef DNSDIST |
4935d3ca | 133 | } |
bc5f8799 | 134 | #endif /* DNSDIST */ |
37a8e048 | 135 | } |
b2ad6825 | 136 | |
37a8e048 | 137 | #define vinfolog if (g_verbose) verboselog |
b2ad6825 | 138 | |
64e4ebb4 | 139 | template<typename... Args> |
140 | void infolog(const char* s, Args... args) | |
141 | { | |
4935d3ca | 142 | genlog(std::cout, LOG_INFO, g_syslog, s, args...); |
64e4ebb4 | 143 | } |
144 | ||
145 | template<typename... Args> | |
146 | void warnlog(const char* s, Args... args) | |
147 | { | |
4935d3ca | 148 | genlog(std::cout, LOG_WARNING, g_syslog, s, args...); |
64e4ebb4 | 149 | } |
150 | ||
151 | template<typename... Args> | |
152 | void errlog(const char* s, Args... args) | |
153 | { | |
4935d3ca | 154 | genlog(std::cout, LOG_ERR, g_syslog, s, args...); |
64e4ebb4 | 155 | } |
b2ad6825 | 156 | |
6f4d5e75 O |
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 | ||
50111728 O |
166 | inline void dolog(const char* s) |
167 | { | |
168 | g_log << s << std::endl; | |
169 | } | |
170 | ||
6f4d5e75 O |
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; | |
50111728 | 183 | dolog(s, args...); |
6f4d5e75 O |
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 |