Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Logger.hpp
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6#include <expected>
7#include <memory>
8#include <optional>
9#include <source_location>
10#include <sstream>
11#include <string>
12#include <string_view>
13#include <vector>
14
15// We forward declare spdlog::logger and spdlog::sinks::sink
16// to avoid including the spdlog headers in this header file.
20namespace spdlog {
21
25class logger; // NOLINT(readability-identifier-naming)
26
30namespace sinks {
34class sink; // NOLINT(readability-identifier-naming)
35} // namespace sinks
36
37} // namespace spdlog
38
42struct BenchmarkLoggingInitializer;
46class LoggerFixture;
50struct LogServiceInitTests;
54struct LogFileRotationTests;
55
56namespace util {
57
58namespace impl {
59class OnAssert;
60} // namespace impl
61
62namespace config {
64} // namespace config
65
66#ifndef COVERAGE_ENABLED
75#define LOG(x) \
76 if (auto clio_pump__ = x; not clio_pump__) \
77 ; \
78 else \
79 clio_pump__
80#else
87#define LOG(x) x
88#endif
89
93enum class Severity {
100};
101
110class Logger {
111 std::shared_ptr<spdlog::logger> logger_;
112
113 friend class LogService; // to expose the Pump interface
114 friend struct ::BenchmarkLoggingInitializer;
115
119 class Pump final {
120 std::shared_ptr<spdlog::logger> logger_;
121 Severity const severity_;
122 std::source_location const sourceLocation_;
123 std::ostringstream stream_;
124 bool const enabled_;
125
126 public:
127 ~Pump();
128
129 Pump(std::shared_ptr<spdlog::logger> logger, Severity sev, std::source_location const& loc);
130
131 Pump(Pump&&) = delete;
132 Pump(Pump const&) = delete;
133 Pump&
134 operator=(Pump const&) = delete;
135 Pump&
136 operator=(Pump&&) = delete;
137
146 template <typename T>
147 [[maybe_unused]] Pump&
148 operator<<(T&& data)
149 {
150 if (enabled_)
151 stream_ << std::forward<T>(data);
152 return *this;
153 }
154
158 operator bool() const
159 {
160 return enabled_;
161 }
162 };
163
164public:
168 static constexpr std::array<std::string_view, 8> kChannels = {
169 "General",
170 "WebServer",
171 "Backend",
172 "RPC",
173 "ETL",
174 "Subscriptions",
175 "Performance",
176 "Migration",
177 };
178
188 Logger(std::string_view const channel);
189
190 Logger(Logger const&) = default;
191
196 ~Logger();
197
198 Logger(Logger&&) = default;
199 Logger&
200 operator=(Logger const&) = default;
201
202 Logger&
203 operator=(Logger&&) = default;
204
211 [[nodiscard]] Pump
212 trace(std::source_location const& loc = std::source_location::current()) const;
213
220 [[nodiscard]] Pump
221 debug(std::source_location const& loc = std::source_location::current()) const;
222
229 [[nodiscard]] Pump
230 info(std::source_location const& loc = std::source_location::current()) const;
231
238 [[nodiscard]] Pump
239 warn(std::source_location const& loc = std::source_location::current()) const;
240
247 [[nodiscard]] Pump
248 error(std::source_location const& loc = std::source_location::current()) const;
249
256 [[nodiscard]] Pump
257 fatal(std::source_location const& loc = std::source_location::current()) const;
258
259private:
260 Logger(std::shared_ptr<spdlog::logger> logger);
261};
262
270protected:
271 friend struct ::LogServiceInitTests;
272 friend class ::LoggerFixture;
273 friend struct ::LogFileRotationTests;
274 friend class Logger;
275 friend class ::util::impl::OnAssert;
276
284 static void
285 init(
286 bool isAsync,
287 Severity defaultSeverity,
288 std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks
289 );
290
296 [[nodiscard]] static bool
297 initialized();
298
305 [[nodiscard]] static bool
306 hasSinks();
307
311 static void
312 reset();
313
319 static void
320 replaceSinks(std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks);
321
332 static std::shared_ptr<spdlog::logger>
333 registerLogger(std::string_view channel, std::optional<Severity> severity = std::nullopt);
334
335protected:
336 static bool isAsync_; // NOLINT(readability-identifier-naming)
337 static Severity defaultSeverity_; // NOLINT(readability-identifier-naming)
338 static std::vector<std::shared_ptr<spdlog::sinks::sink>>
339 sinks_; // NOLINT(readability-identifier-naming)
340 static bool initialized_; // NOLINT(readability-identifier-naming)
341};
342
349class LogService : public LogServiceState {
350public:
351 LogService() = delete;
352
359 [[nodiscard]] static std::expected<void, std::string>
361
365 static void
366 shutdown();
367
374 [[nodiscard]] static Logger::Pump
375 trace(std::source_location const& loc = std::source_location::current());
376
383 [[nodiscard]] static Logger::Pump
384 debug(std::source_location const& loc = std::source_location::current());
385
392 [[nodiscard]] static Logger::Pump
393 info(std::source_location const& loc = std::source_location::current());
394
401 [[nodiscard]] static Logger::Pump
402 warn(std::source_location const& loc = std::source_location::current());
403
410 [[nodiscard]] static Logger::Pump
411 error(std::source_location const& loc = std::source_location::current());
412
419 [[nodiscard]] static Logger::Pump
420 fatal(std::source_location const& loc = std::source_location::current());
421
422private:
429 [[nodiscard]] static std::
430 expected<std::vector<std::shared_ptr<spdlog::sinks::sink>>, std::string>
431 getSinks(config::ClioConfigDefinition const& config);
432
433 struct RotationParams {
434 uint32_t sizeMB;
435 uint32_t maxFiles;
436 };
437
438 struct FileLoggingParams {
439 std::string logDir;
440 std::optional<RotationParams> rotation;
441 };
442
443 friend struct ::BenchmarkLoggingInitializer;
444
445 [[nodiscard]]
446 static std::shared_ptr<spdlog::sinks::sink>
447 createFileSink(FileLoggingParams const& params, std::string const& format);
448};
449
450}; // namespace util
Base state management class for the logging service.
Definition Logger.hpp:269
static bool initialized()
Whether the LogService is initialized or not.
Definition Logger.cpp:265
static void replaceSinks(std::vector< std::shared_ptr< spdlog::sinks::sink > > const &sinks)
Replace the current sinks with a new set of sinks.
Definition Logger.cpp:441
static void init(bool isAsync, Severity defaultSeverity, std::vector< std::shared_ptr< spdlog::sinks::sink > > const &sinks)
Initialize the logging core with specified parameters.
Definition Logger.cpp:238
static void reset()
Reset the logging service to uninitialized state.
Definition Logger.cpp:277
static std::shared_ptr< spdlog::logger > registerLogger(std::string_view channel, std::optional< Severity > severity=std::nullopt)
Register a new logger for the specified channel.
Definition Logger.cpp:289
static bool hasSinks()
Whether the LogService has any sink. If there is no sink, logger will not log messages anywhere.
Definition Logger.cpp:271
static Logger::Pump info(std::source_location const &loc=std::source_location::current())
Globally accessible General logger at Severity::NFO severity.
Definition Logger.cpp:417
static Logger::Pump error(std::source_location const &loc=std::source_location::current())
Globally accessible General logger at Severity::ERR severity.
Definition Logger.cpp:429
static Logger::Pump debug(std::source_location const &loc=std::source_location::current())
Globally accessible General logger at Severity::DBG severity.
Definition Logger.cpp:411
static void shutdown()
Shutdown spdlog to guarantee output is not lost.
Definition Logger.cpp:395
static Logger::Pump trace(std::source_location const &loc=std::source_location::current())
Globally accessible General logger at Severity::TRC severity.
Definition Logger.cpp:405
static std::expected< void, std::string > init(config::ClioConfigDefinition const &config)
Global log core initialization from a config::ClioConfigDefinition.
Definition Logger.cpp:361
static Logger::Pump fatal(std::source_location const &loc=std::source_location::current())
Globally accessible General logger at Severity::FTL severity.
Definition Logger.cpp:435
static Logger::Pump warn(std::source_location const &loc=std::source_location::current())
Globally accessible General logger at Severity::WRN severity.
Definition Logger.cpp:423
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
Pump info(std::source_location const &loc=std::source_location::current()) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:499
~Logger()
Destroy the Logger; drops a dynamically created channel's logger from the spdlog registry once no oth...
Definition Logger.cpp:451
Pump error(std::source_location const &loc=std::source_location::current()) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:509
Pump fatal(std::source_location const &loc=std::source_location::current()) const
Interface for logging at Severity::FTL severity.
Definition Logger.cpp:514
static constexpr std::array< std::string_view, 8 > kChannels
The predefined log channels; loggers for other channels are created dynamically.
Definition Logger.hpp:168
Pump debug(std::source_location const &loc=std::source_location::current()) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:494
Pump trace(std::source_location const &loc=std::source_location::current()) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:489
Pump warn(std::source_location const &loc=std::source_location::current()) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:504
Logger(std::string_view const channel)
Construct a new Logger object that produces loglines for the specified channel.
Definition Logger.cpp:447
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:56
spdlog sinks (forward declarations only)
Definition Logger.hpp:30
The spdlog logging library (forward declarations only).
Definition Logger.hpp:20
Clio configuration: definitions of the supported keys, their types, constraints and defaults,...
Definition HandlerRegistry.hpp:24
This namespace contains various utilities.
Definition HandlerRegistry.hpp:24
Severity
Custom severity levels for util::Logger.
Definition Logger.hpp:93
@ WRN
Warning.
Definition Logger.hpp:97
@ TRC
Trace.
Definition Logger.hpp:94
@ NFO
Info.
Definition Logger.hpp:96
@ DBG
Debug.
Definition Logger.hpp:95
@ ERR
Error.
Definition Logger.hpp:98
@ FTL
Fatal.
Definition Logger.hpp:99