Coverage Report

Created: 2026-05-30 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pdns/pdns/logr.hh
Line
Count
Source
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
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.
8
 *
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.
12
 *
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.
17
 *
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.
21
 */
22
23
#pragma once
24
25
#include <array>
26
#include <cstdint>
27
#include <string>
28
#include <memory>
29
#include <map>
30
31
// Minimal logging API based on https://github.com/go-logr/logr
32
33
namespace Logr
34
{
35
struct Loggable
36
{
37
0
  Loggable() = default;
38
  Loggable(const Loggable&) = delete;
39
  Loggable(Loggable&&) = delete;
40
  Loggable& operator=(const Loggable&) = delete;
41
  Loggable& operator=(Loggable&&) = delete;
42
0
  virtual ~Loggable() = default;
43
  [[nodiscard]] virtual std::string to_string() const = 0;
44
};
45
46
// In addition to level which specifies the amount of detail and is
47
// structured so that a derived logger always has a higher level
48
// than its parent we also have a priority/urgency field that maps
49
// to the same field of the old logger which in turns has a direct
50
// mapping to syslog priority. This is done to make it easier to
51
// move step by step to structured logging. We consider both level
52
// and priority to select which messages are logged, and a backend
53
// can further use priority to pass to syslog.
54
55
enum Priority : uint8_t
56
{
57
  Alert = 1,
58
  Critical = 2,
59
  Error = 3,
60
  Warning = 4,
61
  Notice = 5,
62
  Info = 6,
63
  Debug = 7
64
};
65
66
class Logger
67
{
68
public:
69
0
  Logger() = default;
70
  Logger(const Logger&) = delete;
71
  Logger(Logger&&) = delete;
72
  Logger& operator=(const Logger&) = delete;
73
  Logger& operator=(Logger&&) = delete;
74
0
  virtual ~Logger() = default;
75
76
  static std::string toString(Priority arg)
77
0
  {
78
0
    const std::array<std::string, 8> names = {"?", "Alert", "Critical", "Error", "Warning", "Notice", "Info", "Debug"};
79
0
    auto prio = static_cast<unsigned int>(arg);
80
0
    if (prio >= names.size()) {
81
0
      return "?";
82
0
    }
83
0
    return names.at(prio);
84
0
  }
85
  // Info logs a non-error message with the given key/value pairs as context.
86
  //
87
  // The msg argument should be used to add some constant description to
88
  // the log line.  The key/value pairs can then be used to add additional
89
  // variable information.  The key/value pairs should alternate string
90
  // keys and arbitrary values.
91
  virtual void info(Logr::Priority, const std::string& msg) const = 0;
92
93
  template <typename... Args>
94
  void info(Priority prio, const std::string& msg, const std::string& key, const Loggable& value, const Args&... args) const
95
0
  {
96
0
    auto logger = this->withValues(key, value, args...);
97
0
    logger->info(prio, msg);
98
0
  }
99
100
  // Error logs an error, with the given message and key/value pairs as context.
101
  // It functions similarly to calling Info with the "error" named value, but may
102
  // have unique behavior, and should be preferred for logging errors (see the
103
  // package documentations for more information).
104
  //
105
  // The msg field should be used to add context to any underlying error,
106
  // while the err field should be used to attach the actual error that
107
  // triggered this log line, if present.
108
  virtual void error(Logr::Priority, const std::string& err, const std::string& msg) const = 0;
109
  virtual void error(Logr::Priority, int err, const std::string& msg) const = 0;
110
111
  template <typename... Args>
112
  void error(Priority prio, const std::string& err, const std::string& msg, const std::string& key, const Loggable& value, const Args&... args) const
113
  {
114
    auto logger = this->withValues(key, value, args...);
115
    logger->error(prio, err, msg);
116
  }
117
118
  template <typename... Args>
119
  void error(Priority prio, int err, const std::string& msg, const std::string& key, const Loggable& value, const Args&... args) const
120
0
  {
121
0
    auto logger = this->withValues(key, value, args...);
122
0
    logger->error(prio, err, msg);
123
0
  }
124
125
  // V returns an Logger value for a specific verbosity level, relative to
126
  // this Logger.  In other words, V values are additive.  V higher verbosity
127
  // level means a log message is less important.  It's illegal to pass a log
128
  // level less than zero.
129
  [[nodiscard]] virtual std::shared_ptr<Logger> v(size_t level) const = 0;
130
131
  template <typename... Args>
132
  std::shared_ptr<Logger> withValues(const std::string& key, const Loggable& value, const Args&... args) const
133
0
  {
134
0
    std::map<std::string, std::string> map = {};
135
0
    this->mapArguments(map, key, value, args...);
136
0
    return this->withValues(map);
137
0
  }
138
139
  // WithValues adds some key-value pairs of context to a logger.
140
  // See Info for documentation on how key/value pairs work.
141
  [[nodiscard]] virtual std::shared_ptr<Logger> withValues(const std::map<std::string, std::string>& values) const = 0;
142
143
  // WithName adds a new element to the logger's name.
144
  // Successive calls with WithName continue to append
145
  // suffixes to the logger's name.  It's strongly recommended
146
  // that name segments contain only letters, digits, and hyphens
147
  // (see the package documentation for more information).
148
  [[nodiscard]] virtual std::shared_ptr<Logger> withName(const std::string& name) const = 0;
149
150
private:
151
  template <typename... Args>
152
  void mapArguments(std::map<std::string, std::string>& map, const std::string& key, const Loggable& value, const Args&... args) const
153
0
  {
154
0
    map.emplace(key, value.to_string());
155
0
    mapArguments(map, args...);
156
0
  }
157
158
0
  void mapArguments(std::map<std::string, std::string>& /* map */) const {}
159
};
160
161
using log_t = const std::shared_ptr<Logger>&;
162
}