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