2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
32 #include "namespaces.hh"
34 pthread_once_t
Logger::s_once
;
35 pthread_key_t
Logger::g_loggerKey
;
37 Logger
g_log("", LOG_DAEMON
);
39 void Logger::log(const string
&msg
, Urgency u
)
42 bool mustAccount(false);
44 if(u
<=consoleUrgency
) {
51 strftime(buffer
,sizeof(buffer
),"%b %d %H:%M:%S ", &tm
);
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
56 clog
<< string(buffer
) + msg
<<endl
;
61 if( u
<= d_loglevel
&& !d_disableSyslog
) {
62 syslog(u
,"%s",msg
.c_str());
70 S
.ringAccount("logmessages",msg
);
74 void Logger::setLoglevel( Urgency u
)
80 void Logger::toConsole(Urgency u
)
89 openlog(name
.c_str(),flags
,d_facility
);
93 void Logger::setName(const string
&_name
)
99 void Logger::initKey()
101 if(pthread_key_create(&g_loggerKey
, perThreadDestructor
))
102 unixDie("Creating thread key for logger");
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)
109 if(pthread_once(&s_once
, initKey
))
110 unixDie("Creating thread key for logger");
116 Logger
& Logger::operator<<(Urgency u
)
118 getPerThread()->d_urgency
=u
;
122 void Logger::perThreadDestructor(void* buf
)
124 PerThread
* pt
= (PerThread
*) buf
;
128 Logger::PerThread
* Logger::getPerThread()
130 void *buf
=pthread_getspecific(g_loggerKey
);
133 ret
= (PerThread
*) buf
;
135 ret
= new PerThread();
136 pthread_setspecific(g_loggerKey
, (void*)ret
);
141 Logger
& Logger::operator<<(const string
&s
)
143 PerThread
* pt
=getPerThread();
144 pt
->d_output
.append(s
);
148 Logger
& Logger::operator<<(const char *s
)
154 Logger
& Logger::operator<<(int i
)
164 Logger
& Logger::operator<<(double i
)
172 Logger
& Logger::operator<<(unsigned int i
)
182 Logger
& Logger::operator<<(unsigned long i
)
192 Logger
& Logger::operator<<(unsigned long long i
)
202 Logger
& Logger::operator<<(long i
)
212 Logger
& Logger::operator<<(ostream
& (&)(ostream
&))
214 PerThread
* pt
=getPerThread();
216 log(pt
->d_output
, pt
->d_urgency
);
217 pt
->d_output
.clear();
222 Logger
& Logger::operator<<(const DNSName
&d
)
224 *this<<d
.toLogString();
229 Logger
& Logger::operator<<(const ComboAddress
&ca
)
231 *this<<ca
.toString();