Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Logger.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2022, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/SourceLocation.hpp"
23
24#include <boost/algorithm/string/predicate.hpp>
25#include <boost/filesystem.hpp>
26#include <boost/json.hpp>
27#include <boost/json/conversion.hpp>
28#include <boost/json/value.hpp>
29#include <boost/log/core/core.hpp>
30#include <boost/log/core/record.hpp>
31#include <boost/log/expressions/filter.hpp>
32#include <boost/log/expressions/keyword.hpp>
33#include <boost/log/expressions/predicates/channel_severity_filter.hpp>
34#include <boost/log/keywords/channel.hpp>
35#include <boost/log/keywords/severity.hpp>
36#include <boost/log/sinks/unlocked_frontend.hpp>
37#include <boost/log/sources/record_ostream.hpp>
38#include <boost/log/sources/severity_channel_logger.hpp>
39#include <boost/log/sources/severity_feature.hpp>
40#include <boost/log/sources/severity_logger.hpp>
41#include <boost/log/utility/manipulators/add_value.hpp>
42#include <boost/log/utility/setup/common_attributes.hpp>
43#include <boost/log/utility/setup/console.hpp>
44#include <boost/log/utility/setup/file.hpp>
45#include <boost/log/utility/setup/formatter_parser.hpp>
46
47#include <array>
48#include <cstddef>
49#include <optional>
50#include <ostream>
51#include <string>
52
53namespace util {
54
55namespace config {
56class ClioConfigDefinition;
57} // namespace config
58
64#ifndef COVERAGE_ENABLED
65#define LOG(x) \
66 if (auto clio_pump__ = x; not clio_pump__) { \
67 } else \
68 clio_pump__
69#else
70#define LOG(x) x
71#endif
72
76enum class Severity {
77 TRC,
78 DBG,
79 NFO,
80 WRN,
81 ERR,
82 FTL,
83};
84
86// NOLINTBEGIN(readability-identifier-naming)
87BOOST_LOG_ATTRIBUTE_KEYWORD(LogSeverity, "Severity", Severity);
88BOOST_LOG_ATTRIBUTE_KEYWORD(LogChannel, "Channel", std::string);
89// NOLINTEND(readability-identifier-naming)
99std::ostream&
100operator<<(std::ostream& stream, Severity sev);
101
110class Logger final {
111 using LoggerType = boost::log::sources::severity_channel_logger_mt<Severity, std::string>;
112 mutable LoggerType logger_;
113
114 friend class LogService; // to expose the Pump interface
115
119 class Pump final {
120 using PumpOptType = std::optional<boost::log::aux::record_pump<LoggerType>>;
121
122 boost::log::record rec_;
123 PumpOptType pump_ = std::nullopt;
124
125 public:
126 ~Pump() = default;
127
128 Pump(LoggerType& logger, Severity sev, SourceLocationType const& loc)
129 : rec_{logger.open_record(boost::log::keywords::severity = sev)}
130 {
131 if (rec_) {
132 pump_.emplace(boost::log::aux::make_record_pump(logger, rec_));
133 pump_->stream() << boost::log::add_value("SourceLocation", prettyPath(loc));
134 }
135 }
136
137 Pump(Pump&&) = delete;
138 Pump(Pump const&) = delete;
139 Pump&
140 operator=(Pump const&) = delete;
141 Pump&
142 operator=(Pump&&) = delete;
143
151 template <typename T>
152 [[maybe_unused]] Pump&
153 operator<<(T&& data)
154 {
155 if (pump_)
156 pump_->stream() << std::forward<T>(data);
157 return *this;
158 }
159
163 operator bool() const
164 {
165 return pump_.has_value();
166 }
167
168 private:
169 [[nodiscard]] static std::string
170 prettyPath(SourceLocationType const& loc, size_t maxDepth = 3);
171 };
172
173public:
174 static constexpr std::array<char const*, 8> kCHANNELS = {
175 "General",
176 "WebServer",
177 "Backend",
178 "RPC",
179 "ETL",
180 "Subscriptions",
181 "Performance",
182 "Migration",
183 };
184
194 Logger(std::string channel) : logger_{boost::log::keywords::channel = channel}
195 {
196 }
197
198 Logger(Logger const&) = default;
199 ~Logger() = default;
200
201 Logger(Logger&&) = default;
202 Logger&
203 operator=(Logger const&) = default;
204
205 Logger&
206 operator=(Logger&&) = default;
207
214 [[nodiscard]] Pump
215 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
216
223 [[nodiscard]] Pump
224 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
225
232 [[nodiscard]] Pump
233 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
234
241 [[nodiscard]] Pump
242 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
243
250 [[nodiscard]] Pump
251 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
252
259 [[nodiscard]] Pump
260 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
261};
262
270 static Logger generalLog; /*< Global logger for General channel */
271 static Logger alertLog; /*< Global logger for Alerts channel */
272 static boost::log::filter filter;
273
274public:
275 LogService() = delete;
276
282 static void
283 init(config::ClioConfigDefinition const& config);
284
291 [[nodiscard]] static Logger::Pump
292 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION)
293 {
294 return generalLog.trace(loc);
295 }
296
303 [[nodiscard]] static Logger::Pump
304 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION)
305 {
306 return generalLog.debug(loc);
307 }
308
315 [[nodiscard]] static Logger::Pump
316 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION)
317 {
318 return generalLog.info(loc);
319 }
320
327 [[nodiscard]] static Logger::Pump
328 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION)
329 {
330 return generalLog.warn(loc);
331 }
332
339 [[nodiscard]] static Logger::Pump
340 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION)
341 {
342 return generalLog.error(loc);
343 }
344
351 [[nodiscard]] static Logger::Pump
352 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION)
353 {
354 return generalLog.fatal(loc);
355 }
356
363 [[nodiscard]] static Logger::Pump
364 alert(SourceLocationType const& loc = CURRENT_SRC_LOCATION)
365 {
366 return alertLog.warn(loc);
367 }
368};
369
370}; // namespace util
A global logging service.
Definition Logger.hpp:269
static Logger::Pump alert(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accesible Alert logger.
Definition Logger.hpp:364
static Logger::Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accesible General logger at Severity::FTL severity.
Definition Logger.hpp:352
static Logger::Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accesible General logger at Severity::DBG severity.
Definition Logger.hpp:304
static Logger::Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accesible General logger at Severity::TRC severity.
Definition Logger.hpp:292
static Logger::Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accesible General logger at Severity::ERR severity.
Definition Logger.hpp:340
static void init(config::ClioConfigDefinition const &config)
Global log core initialization from a Config.
Definition Logger.cpp:115
static Logger::Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accesible General logger at Severity::NFO severity.
Definition Logger.hpp:316
static Logger::Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accesible General logger at Severity::WRN severity.
Definition Logger.hpp:328
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
Logger(std::string channel)
Construct a new Logger object that produces loglines for the specified channel.
Definition Logger.hpp:194
Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:210
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:215
Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::FTL severity.
Definition Logger.cpp:220
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:200
Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:195
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:205
A class representing the source location of the current code.
Definition SourceLocation.hpp:52
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:70
This namespace contains various utilities.
Definition AccountUtils.hpp:30
std::ostream & operator<<(std::ostream &stream, Severity sev)
Custom labels for Severity in log output.
Definition Logger.cpp:73
Severity
Custom severity levels for util::Logger.
Definition Logger.hpp:76