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 <array>
25#include <cstddef>
26#include <cstdint>
27#include <expected>
28#include <memory>
29#include <sstream>
30#include <string>
31#include <vector>
32
33// We forward declare spdlog::logger and spdlog::sinks::sink
34// to avoid including the spdlog headers in this header file.
35namespace spdlog {
36
37class logger; // NOLINT(readability-identifier-naming)
38
39namespace sinks {
40class sink; // NOLINT(readability-identifier-naming)
41} // namespace sinks
42
43} // namespace spdlog
44
45struct BenchmarkLoggingInitializer;
46
47namespace util {
48
49namespace config {
50class ClioConfigDefinition;
51} // namespace config
52
58#ifndef COVERAGE_ENABLED
59#define LOG(x) \
60 if (auto clio_pump__ = x; not clio_pump__) { \
61 } else \
62 clio_pump__
63#else
64#define LOG(x) x
65#endif
66
70enum class Severity {
71 TRC,
72 DBG,
73 NFO,
74 WRN,
75 ERR,
76 FTL,
77};
78
87class Logger final {
88 std::shared_ptr<spdlog::logger> logger_;
89
90 friend class LogService; // to expose the Pump interface
91 friend struct ::BenchmarkLoggingInitializer;
92
96 class Pump final {
97 std::shared_ptr<spdlog::logger> logger_;
98 Severity const severity_;
99 SourceLocationType const sourceLocation_;
100 std::ostringstream stream_;
101 bool const enabled_;
102
103 public:
104 ~Pump();
105
106 Pump(std::shared_ptr<spdlog::logger> logger, Severity sev, SourceLocationType const& loc);
107
108 Pump(Pump&&) = delete;
109 Pump(Pump const&) = delete;
110 Pump&
111 operator=(Pump const&) = delete;
112 Pump&
113 operator=(Pump&&) = delete;
114
122 template <typename T>
123 [[maybe_unused]] Pump&
124 operator<<(T&& data)
125 {
126 if (enabled_)
127 stream_ << std::forward<T>(data);
128 return *this;
129 }
130
134 operator bool() const
135 {
136 return enabled_;
137 }
138 };
139
140public:
141 static constexpr std::array<char const*, 8> kCHANNELS = {
142 "General",
143 "WebServer",
144 "Backend",
145 "RPC",
146 "ETL",
147 "Subscriptions",
148 "Performance",
149 "Migration",
150 };
151
161 Logger(std::string channel);
162
163 Logger(Logger const&) = default;
164 ~Logger() = default;
165
166 Logger(Logger&&) = default;
167 Logger&
168 operator=(Logger const&) = default;
169
170 Logger&
171 operator=(Logger&&) = default;
172
179 [[nodiscard]] Pump
180 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
181
188 [[nodiscard]] Pump
189 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
190
197 [[nodiscard]] Pump
198 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
199
206 [[nodiscard]] Pump
207 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
208
215 [[nodiscard]] Pump
216 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
217
224 [[nodiscard]] Pump
225 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
226
227private:
228 Logger(std::shared_ptr<spdlog::logger> logger);
229};
230
238 struct Data {
239 bool isAsync;
240 Severity defaultSeverity;
241 std::vector<std::shared_ptr<spdlog::sinks::sink>> allSinks;
242 };
243
244 friend class Logger;
245
246private:
247 static Data data;
248
249 static std::shared_ptr<spdlog::logger>
250 registerLogger(std::string const& channel, Severity severity = data.defaultSeverity);
251
252public:
253 LogService() = delete;
254
261 [[nodiscard]] static std::expected<void, std::string>
262 init(config::ClioConfigDefinition const& config);
263
267 static void
268 shutdown();
269
276 [[nodiscard]] static Logger::Pump
277 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
278
285 [[nodiscard]] static Logger::Pump
286 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
287
294 [[nodiscard]] static Logger::Pump
295 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
296
303 [[nodiscard]] static Logger::Pump
304 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
305
312 [[nodiscard]] static Logger::Pump
313 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
314
321 [[nodiscard]] static Logger::Pump
322 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
323
329 [[nodiscard]] static bool
330 enabled();
331
332private:
333 struct FileLoggingParams {
334 std::string logDir;
335
336 uint32_t rotationSizeMB;
337 uint32_t dirMaxFiles;
338 };
339
340 friend struct ::BenchmarkLoggingInitializer;
341
342 [[nodiscard]]
343 static std::shared_ptr<spdlog::sinks::sink>
344 createFileSink(FileLoggingParams const& params, std::string const& format);
345};
346
347}; // namespace util
A global logging service.
Definition Logger.hpp:237
static Logger::Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::ERR severity.
Definition Logger.cpp:351
static Logger::Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::DBG severity.
Definition Logger.cpp:333
static Logger::Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::WRN severity.
Definition Logger.cpp:345
static void shutdown()
Shutdown spdlog to guarantee output is not lost.
Definition Logger.cpp:321
static std::expected< void, std::string > init(config::ClioConfigDefinition const &config)
Global log core initialization from a config::ClioConfigDefinition.
Definition Logger.cpp:266
static Logger::Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::FTL severity.
Definition Logger.cpp:357
static bool enabled()
Whether the LogService is enabled or not.
Definition Logger.cpp:363
static Logger::Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::TRC severity.
Definition Logger.cpp:327
static Logger::Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::NFO severity.
Definition Logger.cpp:339
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:87
Logger(std::string channel)
Construct a new Logger object that produces loglines for the specified channel.
Definition Logger.cpp:368
Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:409
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:414
Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::FTL severity.
Definition Logger.cpp:419
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:399
Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:394
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:404
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:50
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:70
This namespace contains various utilities.
Definition AccountUtils.hpp:30
Severity
Custom severity levels for util::Logger.
Definition Logger.hpp:70