]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/logger.cc
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.
34 #include "namespaces.hh"
36 thread_local
Logger::PerThread
Logger::t_perThread
;
40 /* Since the Logger can be called very early, we need to make sure
41 that the relevant parts are initialized no matter what, which is tricky
42 because we can't easily control the initialization order, especially with
44 t_perThread is thread_local, so it will be initialized when first accessed,
45 but we need to make sure that the object itself is initialized, and making
46 it a function-level static variable achieves that, because it will be
47 initialized the first time we enter this function at the very last.
49 static Logger
log("", LOG_DAEMON
);
53 void Logger::log(const string
&msg
, Urgency u
)
56 bool mustAccount(false);
58 if(u
<=consoleUrgency
) {
65 strftime(buffer
,sizeof(buffer
),"%b %d %H:%M:%S ", &tm
);
78 prefix
= "[CRITICAL] ";
84 prefix
= "[WARNING] ";
102 std::lock_guard
<std::mutex
> l(m
); // the C++-2011 spec says we need this, and OSX actually does
103 clog
<< string(buffer
) + prefix
+ msg
<<endl
;
108 if( u
<= d_loglevel
&& !d_disableSyslog
) {
109 syslog(u
,"%s",msg
.c_str());
117 S
.ringAccount("logmessages",msg
);
121 void Logger::setLoglevel( Urgency u
)
127 void Logger::toConsole(Urgency u
)
136 openlog(name
.c_str(),flags
,d_facility
);
140 void Logger::setName(const string
&_name
)
146 Logger::Logger(const string
&n
, int facility
) :
147 name(n
), flags(LOG_PID
|LOG_NDELAY
), d_facility(facility
), d_loglevel(Logger::None
),
148 consoleUrgency(Error
), opened(false), d_disableSyslog(false)
154 Logger
& Logger::operator<<(Urgency u
)
156 getPerThread().d_urgency
=u
;
160 Logger::PerThread
& Logger::getPerThread()
165 Logger
& Logger::operator<<(const string
&s
)
167 PerThread
& pt
= getPerThread();
168 pt
.d_output
.append(s
);
172 Logger
& Logger::operator<<(const char *s
)
178 Logger
& Logger::operator<<(ostream
& (&)(ostream
&))
180 PerThread
& pt
= getPerThread();
182 log(pt
.d_output
, pt
.d_urgency
);
188 Logger
& Logger::operator<<(const DNSName
&d
)
190 *this<<d
.toLogString();
195 Logger
& Logger::operator<<(const ComboAddress
&ca
)
197 *this<<ca
.toLogString();