File: logging.cc

package info (click to toggle)
pdns-recursor 5.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,116 kB
  • sloc: cpp: 109,672; javascript: 20,651; python: 5,657; sh: 5,114; makefile: 782; ansic: 582; xml: 37
file content (176 lines) | stat: -rw-r--r-- 5,152 bytes parent folder | download | duplicates (2)
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
/**
 * 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 "logging.hh"
#include <string>
#include <mutex>
#include "utility.hh"

namespace Logging
{

std::shared_ptr<const Logger> Logger::getptr() const
{
  return shared_from_this();
}

bool Logger::enabled(Logr::Priority prio) const
{
  return _level <= _verbosity || prio != Logr::Absent;
}

void Logger::info(const std::string& msg) const
{
  logMessage(msg, Logr::Absent, boost::none);
}

void Logger::info(Logr::Priority prio, const std::string& msg) const
{
  logMessage(msg, prio, boost::none);
}

void Logger::logMessage(const std::string& msg, boost::optional<const std::string> err) const
{
  return logMessage(msg, Logr::Absent, std::move(err));
}

void Logger::logMessage(const std::string& msg, Logr::Priority prio, boost::optional<const std::string> err) const
{
  if (!enabled(prio)) {
    return;
  }
  Entry entry;
  entry.level = _level;
  entry.d_priority = prio;
  Utility::gettimeofday(&entry.d_timestamp);
  entry.name = _name;
  entry.message = msg;
  entry.error = std::move(err);
  auto parent = _parent;
  entry.values.insert(_values.begin(), _values.end());
  while (parent) {
    entry.values.insert(parent->_values.begin(), parent->_values.end());
    parent = parent->_parent;
  }
  _callback(entry);
}

void Logger::error(Logr::Priority prio, int err, const std::string& msg) const
{
  logMessage(msg, prio, std::string(stringerror(err)));
}

void Logger::error(Logr::Priority prio, const std::string& err, const std::string& msg) const
{
  logMessage(msg, prio, err);
}

void Logger::error(int err, const std::string& msg) const
{
  logMessage(msg, Logr::Absent, std::string(stringerror(err)));
}

void Logger::error(const std::string& err, const std::string& msg) const
{
  logMessage(msg, Logr::Absent, err);
}

std::shared_ptr<Logr::Logger> Logger::v(size_t level) const
{
  auto res = std::make_shared<Logger>(getptr(), _name, getVerbosity(), level + _level, _callback);
  return res;
}

std::shared_ptr<Logr::Logger> Logger::withValues(const std::map<std::string, std::string>& values) const
{
  auto res = std::make_shared<Logger>(getptr(), _name, getVerbosity(), _level, _callback);
  res->_values = values;
  return res;
}

std::shared_ptr<Logr::Logger> Logger::withName(const std::string& name) const
{
  std::shared_ptr<Logger> res;
  if (_name) {
    res = std::make_shared<Logger>(getptr(), _name.get() + "." + name, getVerbosity(), _level, _callback);
  }
  else {
    res = std::make_shared<Logger>(getptr(), name, getVerbosity(), _level, _callback);
  }
  res->setVerbosity(getVerbosity());
  return res;
}
std::shared_ptr<Logger> Logger::create(EntryLogger callback)
{
  return std::make_shared<Logger>(callback);
}
std::shared_ptr<Logger> Logger::create(EntryLogger callback, const std::string& name)
{
  return std::make_shared<Logger>(callback, name);
}

size_t Logger::getVerbosity() const
{
  return _verbosity;
}

void Logger::setVerbosity(size_t verbosity)
{
  _verbosity = verbosity;
}

Logger::Logger(EntryLogger callback) :
  _callback(callback)
{
}
Logger::Logger(EntryLogger callback, boost::optional<std::string> name) :
  _callback(callback), _name(std::move(name))
{
}
Logger::Logger(std::shared_ptr<const Logger> parent, boost::optional<std::string> name, size_t verbosity, size_t lvl, EntryLogger callback) :
  _parent(std::move(parent)), _callback(callback), _name(std::move(name)), _level(lvl), _verbosity(verbosity)
{
}

Logger::~Logger() = default;
};

std::shared_ptr<Logging::Logger> g_slog{nullptr};

const char* Logging::toTimestampStringMilli(const struct timeval& tval, std::array<char, 64>& buf, const std::string& format)
{
  size_t len = 0;
  if (format != "%s") {
    // strftime is not thread safe, it can access locale information
    static std::mutex mutex;
    auto lock = std::scoped_lock(mutex);
    struct tm theTime // clang-format insists on formatting it like this
      {};
    len = strftime(buf.data(), buf.size(), format.c_str(), localtime_r(&tval.tv_sec, &theTime));
  }
  if (len == 0) {
    len = snprintf(buf.data(), buf.size(), "%lld", static_cast<long long>(tval.tv_sec));
  }

  snprintf(&buf.at(len), buf.size() - len, ".%03ld", static_cast<long>(tval.tv_usec) / 1000);
  return buf.data();
}