]>
Commit | Line | Data |
---|---|---|
12c86877 | 1 | /* |
12471842 PL |
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 | */ | |
870a0fe4 | 22 | #ifdef HAVE_CONFIG_H |
a2bfc3ff | 23 | #include "config.h" |
870a0fe4 AT |
24 | #endif |
25 | #include "logger.hh" | |
8e203aef | 26 | #include "misc.hh" |
a2bfc3ff | 27 | #ifndef RECURSOR |
12c86877 | 28 | #include "statbag.hh" |
a2bfc3ff BH |
29 | extern StatBag S; |
30 | #endif | |
90e3e939 | 31 | #include "lock.hh" |
10f4eea8 | 32 | #include "namespaces.hh" |
12c86877 | 33 | |
d1449e4d | 34 | pthread_once_t Logger::s_once; |
35 | pthread_key_t Logger::s_loggerKey; | |
36 | ||
12c86877 BH |
37 | Logger &theL(const string &pname) |
38 | { | |
eefd15f9 | 39 | static Logger l("", LOG_DAEMON); |
12c86877 BH |
40 | if(!pname.empty()) |
41 | l.setName(pname); | |
42 | return l; | |
43 | } | |
44 | ||
45 | void Logger::log(const string &msg, Urgency u) | |
46 | { | |
29c2d86d | 47 | #ifndef RECURSOR |
9cfac2e0 | 48 | bool mustAccount(false); |
29c2d86d | 49 | #endif |
8d755517 | 50 | if(u<=consoleUrgency) { |
b18fa400 PL |
51 | char buffer[50] = ""; |
52 | if (d_timestamps) { | |
53 | struct tm tm; | |
54 | time_t t; | |
55 | time(&t); | |
56 | tm=*localtime(&t); | |
57 | strftime(buffer,sizeof(buffer),"%b %d %H:%M:%S ", &tm); | |
58 | } | |
59 | ||
8d755517 | 60 | static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; |
61 | Lock l(&m); // the C++-2011 spec says we need this, and OSX actually does | |
3dd58dd8 | 62 | clog << string(buffer) + msg <<endl; |
29c2d86d | 63 | #ifndef RECURSOR |
9cfac2e0 | 64 | mustAccount=true; |
29c2d86d | 65 | #endif |
12c86877 | 66 | } |
b6cfa948 | 67 | if( u <= d_loglevel && !d_disableSyslog ) { |
9cfac2e0 | 68 | syslog(u,"%s",msg.c_str()); |
29c2d86d | 69 | #ifndef RECURSOR |
9cfac2e0 | 70 | mustAccount=true; |
29c2d86d | 71 | #endif |
9cfac2e0 PL |
72 | } |
73 | ||
a2bfc3ff | 74 | #ifndef RECURSOR |
9cfac2e0 | 75 | if(mustAccount) |
fe1ce82e | 76 | S.ringAccount("logmessages",msg); |
a2bfc3ff | 77 | #endif |
12c86877 BH |
78 | } |
79 | ||
eefd15f9 BH |
80 | void Logger::setLoglevel( Urgency u ) |
81 | { | |
fe1ce82e | 82 | d_loglevel = u; |
eefd15f9 BH |
83 | } |
84 | ||
85 | ||
12c86877 BH |
86 | void Logger::toConsole(Urgency u) |
87 | { | |
12c86877 BH |
88 | consoleUrgency=u; |
89 | } | |
90 | ||
91 | void Logger::open() | |
92 | { | |
93 | if(opened) | |
94 | closelog(); | |
95 | openlog(name.c_str(),flags,d_facility); | |
96 | opened=true; | |
97 | } | |
98 | ||
99 | void Logger::setName(const string &_name) | |
100 | { | |
101 | name=_name; | |
102 | open(); | |
103 | } | |
104 | ||
d1449e4d | 105 | void Logger::initKey() |
106 | { | |
107 | if(pthread_key_create(&s_loggerKey, perThreadDestructor)) | |
108 | unixDie("Creating thread key for logger"); | |
109 | } | |
110 | ||
12c86877 BH |
111 | Logger::Logger(const string &n, int facility) |
112 | { | |
113 | opened=false; | |
114 | flags=LOG_PID|LOG_NDELAY; | |
115 | d_facility=facility; | |
55b6512d | 116 | d_loglevel=Logger::None; |
34c513f9 | 117 | d_disableSyslog=false; |
12c86877 BH |
118 | consoleUrgency=Error; |
119 | name=n; | |
d1449e4d | 120 | |
121 | if(pthread_once(&s_once, initKey)) | |
122 | unixDie("Creating thread key for logger"); | |
123 | ||
12c86877 BH |
124 | open(); |
125 | ||
126 | } | |
127 | ||
128 | Logger& Logger::operator<<(Urgency u) | |
129 | { | |
d1449e4d | 130 | getPerThread()->d_urgency=u; |
12c86877 BH |
131 | return *this; |
132 | } | |
133 | ||
d1449e4d | 134 | void Logger::perThreadDestructor(void* buf) |
12c86877 | 135 | { |
d1449e4d | 136 | PerThread* pt = (PerThread*) buf; |
137 | delete pt; | |
138 | } | |
12c86877 | 139 | |
d1449e4d | 140 | Logger::PerThread* Logger::getPerThread() |
141 | { | |
142 | void *buf=pthread_getspecific(s_loggerKey); | |
143 | PerThread* ret; | |
144 | if(buf) | |
145 | ret = (PerThread*) buf; | |
146 | else { | |
147 | ret = new PerThread(); | |
148 | pthread_setspecific(s_loggerKey, (void*)ret); | |
149 | } | |
150 | return ret; | |
151 | } | |
12c86877 | 152 | |
d1449e4d | 153 | Logger& Logger::operator<<(const string &s) |
154 | { | |
155 | PerThread* pt =getPerThread(); | |
156 | pt->d_output.append(s); | |
12c86877 BH |
157 | return *this; |
158 | } | |
159 | ||
7abbc40f PD |
160 | Logger& Logger::operator<<(const char *s) |
161 | { | |
162 | *this<<string(s); | |
163 | return *this; | |
164 | } | |
165 | ||
12c86877 BH |
166 | Logger& Logger::operator<<(int i) |
167 | { | |
168 | ostringstream tmp; | |
169 | tmp<<i; | |
170 | ||
171 | *this<<tmp.str(); | |
172 | ||
173 | return *this; | |
174 | } | |
175 | ||
f1f85f12 BH |
176 | Logger& Logger::operator<<(double i) |
177 | { | |
178 | ostringstream tmp; | |
179 | tmp<<i; | |
180 | *this<<tmp.str(); | |
181 | return *this; | |
182 | } | |
183 | ||
ff181ffa BH |
184 | Logger& Logger::operator<<(unsigned int i) |
185 | { | |
186 | ostringstream tmp; | |
187 | tmp<<i; | |
188 | ||
189 | *this<<tmp.str(); | |
190 | ||
191 | return *this; | |
192 | } | |
193 | ||
96879e7d BH |
194 | Logger& Logger::operator<<(unsigned long i) |
195 | { | |
196 | ostringstream tmp; | |
197 | tmp<<i; | |
198 | ||
199 | *this<<tmp.str(); | |
200 | ||
201 | return *this; | |
202 | } | |
203 | ||
1d5b3ce6 BH |
204 | Logger& Logger::operator<<(unsigned long long i) |
205 | { | |
206 | ostringstream tmp; | |
207 | tmp<<i; | |
208 | ||
209 | *this<<tmp.str(); | |
210 | ||
211 | return *this; | |
212 | } | |
213 | ||
ac8a2021 BH |
214 | Logger& Logger::operator<<(long i) |
215 | { | |
216 | ostringstream tmp; | |
217 | tmp<<i; | |
218 | ||
219 | *this<<tmp.str(); | |
220 | ||
221 | return *this; | |
222 | } | |
ff181ffa | 223 | |
12c86877 BH |
224 | Logger& Logger::operator<<(ostream & (&)(ostream &)) |
225 | { | |
d1449e4d | 226 | PerThread* pt =getPerThread(); |
12c86877 | 227 | |
d1449e4d | 228 | log(pt->d_output, pt->d_urgency); |
229 | pt->d_output.clear(); | |
230 | pt->d_urgency=Info; | |
12c86877 BH |
231 | return *this; |
232 | } | |
dd72bfaa PL |
233 | |
234 | Logger& Logger::operator<<(const DNSName &d) | |
235 | { | |
c348fec2 | 236 | *this<<d.toLogString(); |
dd72bfaa PL |
237 | |
238 | return *this; | |
239 | } | |
ded6b08d AT |
240 | |
241 | Logger& Logger::operator<<(const ComboAddress &ca) | |
242 | { | |
243 | *this<<ca.toString(); | |
244 | return *this; | |
245 | } | |
246 |