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