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 thread_local
Logger::PerThread
Logger::t_perThread
;
38 /* Since the Logger can be called very early, we need to make sure
39 that the relevant parts are initialized no matter what, which is tricky
40 because we can't easily control the initialization order, especially with
42 t_perThread is thread_local, so it will be initialized when first accessed,
43 but we need to make sure that the object itself is initialized, and making
44 it a function-level static variable achieves that, because it will be
45 initialized the first time we enter this function at the very last.
47 static Logger
log("", LOG_DAEMON
);
51 void Logger::log(const string
&msg
, Urgency u
)
54 bool mustAccount(false);
56 if(u
<=consoleUrgency
) {
63 strftime(buffer
,sizeof(buffer
),"%b %d %H:%M:%S ", &tm
);
76 prefix
= "[CRITICAL] ";
82 prefix
= "[WARNING] ";
99 static pthread_mutex_t m
= PTHREAD_MUTEX_INITIALIZER
;
100 Lock
l(&m
); // the C++-2011 spec says we need this, and OSX actually does
101 clog
<< string(buffer
) + prefix
+ msg
<<endl
;
106 if( u
<= d_loglevel
&& !d_disableSyslog
) {
107 syslog(u
,"%s",msg
.c_str());
115 S
.ringAccount("logmessages",msg
);
119 void Logger::setLoglevel( Urgency u
)
125 void Logger::toConsole(Urgency u
)
134 openlog(name
.c_str(),flags
,d_facility
);
138 void Logger::setName(const string
&_name
)
144 Logger::Logger(const string
&n
, int facility
) :
145 name(n
), flags(LOG_PID
|LOG_NDELAY
), d_facility(facility
), d_loglevel(Logger::None
),
146 consoleUrgency(Error
), opened(false), d_disableSyslog(false)
152 Logger
& Logger::operator<<(Urgency u
)
154 getPerThread().d_urgency
=u
;
158 Logger::PerThread
& Logger::getPerThread()
163 Logger
& Logger::operator<<(const string
&s
)
165 PerThread
& pt
= getPerThread();
166 pt
.d_output
.append(s
);
170 Logger
& Logger::operator<<(const char *s
)
176 Logger
& Logger::operator<<(ostream
& (&)(ostream
&))
178 PerThread
& pt
= getPerThread();
180 log(pt
.d_output
, pt
.d_urgency
);
186 Logger
& Logger::operator<<(const DNSName
&d
)
188 *this<<d
.toLogString();
193 Logger
& Logger::operator<<(const ComboAddress
&ca
)
195 *this<<ca
.toLogString();