1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <ostream>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <iomanip>
#include <mutex>
#include "logger.hh"
#include "misc.hh"
#ifndef RECURSOR
#include "statbag.hh"
extern StatBag S;
#endif
#include "namespaces.hh"
thread_local Logger::PerThread Logger::t_perThread;
Logger& getLogger()
{
/* Since the Logger can be called very early, we need to make sure
that the relevant parts are initialized no matter what, which is tricky
because we can't easily control the initialization order, especially with
built-in backends.
t_perThread is thread_local, so it will be initialized when first accessed,
but we need to make sure that the object itself is initialized, and making
it a function-level static variable achieves that, because it will be
initialized the first time we enter this function at the very last.
*/
static Logger log("", LOG_DAEMON);
return log;
}
void Logger::log(const string& msg, Urgency u) noexcept
{
#ifndef RECURSOR
bool mustAccount(false);
#endif
if (u <= consoleUrgency) {
std::array<char, 50> buffer{};
buffer[0] = '\0';
if (d_timestamps) {
struct tm tm;
time_t t;
time(&t);
localtime_r(&t, &tm);
if (strftime(buffer.data(), buffer.size(), "%b %d %H:%M:%S ", &tm) == 0) {
buffer[0] = '\0';
}
}
string severity;
if (d_prefixed) {
switch (u) {
case All:
severity = "All";
break;
case Alert:
severity = "Alert";
break;
case Critical:
severity = "Critical";
break;
case Error:
severity = "Error";
break;
case Warning:
severity = "Warning";
break;
case Notice:
severity = "Notice";
break;
case Info:
severity = "Info";
break;
case Debug:
severity = "Debug";
break;
case None:
severity = "None";
break;
}
}
static std::mutex mutex;
auto lock = std::scoped_lock(mutex); // the C++-2011 spec says we need this, and OSX actually does
// To avoid issuing multiple syscalls, we write the complete line to clog with a single << call.
// For that we need a buffer allocated, we might want to use writev(2) one day to avoid that.
ostringstream line;
line << buffer.data();
if (d_prefixed) {
line << "msg=" << std::quoted(msg) << " prio=" << std::quoted(severity) << endl;
}
else {
line << msg << endl;
}
clog << line.str() << std::flush;
#ifndef RECURSOR
mustAccount = true;
#endif
}
if (u <= d_loglevel && !d_disableSyslog) {
syslog(u, "%s", msg.c_str());
#ifndef RECURSOR
mustAccount = true;
#endif
}
#ifndef RECURSOR
if (mustAccount) {
try {
S.ringAccount("logmessages", msg);
}
catch (const runtime_error& e) {
cerr << e.what() << endl;
}
}
#endif
}
void Logger::setLoglevel(Urgency u)
{
d_loglevel = u;
}
void Logger::toConsole(Urgency u)
{
consoleUrgency = u;
}
void Logger::open()
{
if (opened)
closelog();
openlog(name.c_str(), flags, d_facility);
opened = true;
}
void Logger::setName(const string& _name)
{
name = _name;
open();
}
Logger::Logger(string n, int facility) :
name(std::move(n)), flags(LOG_PID | LOG_NDELAY), d_facility(facility)
{
open();
}
Logger& Logger::operator<<(Urgency u)
{
getPerThread().d_urgency = u;
return *this;
}
Logger::PerThread& Logger::getPerThread()
{
return t_perThread;
}
Logger& Logger::operator<<(const string& s)
{
PerThread& pt = getPerThread();
pt.d_output.append(s);
return *this;
}
Logger& Logger::operator<<(const char* s)
{
*this << string(s);
return *this;
}
Logger& Logger::operator<<(ostream& (&)(ostream&))
{
PerThread& pt = getPerThread();
log(pt.d_output, pt.d_urgency);
pt.d_output.clear();
pt.d_urgency = Info;
return *this;
}
Logger& Logger::operator<<(const DNSName& d)
{
*this << d.toLogString();
return *this;
}
#if defined(PDNS_AUTH)
Logger& Logger::operator<<(const ZoneName& zone)
{
*this << zone.toLogString();
return *this;
}
#endif
Logger& Logger::operator<<(const ComboAddress& ca)
{
*this << ca.toLogString();
return *this;
}
Logger& Logger::operator<<(const SockaddrWrapper& sockaddr)
{
*this << sockaddr.toString();
return *this;
}
void addTraceTS(const timeval& start, ostringstream& str)
{
const auto& content = str.str();
if (content.empty() || content.back() == '\n') {
timeval time{};
gettimeofday(&time, nullptr);
auto elapsed = time - start;
auto diff = elapsed.tv_sec * 1000000 + static_cast<time_t>(elapsed.tv_usec);
str << diff << ' ';
}
}
|