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.
17namespace spdlog {
18
19class logger; // NOLINT(readability-identifier-naming)
20
21namespace sinks {
22class sink; // NOLINT(readability-identifier-naming)
23} // namespace sinks
24
25} // namespace spdlog
26
27struct BenchmarkLoggingInitializer;
28class LoggerFixture;
29struct LogServiceInitTests;
30struct LogFileRotationTests;
31
32namespace util {
33
34namespace impl {
35class OnAssert;
36} // namespace impl
37
38namespace config {
40} // namespace config
41
48#ifndef COVERAGE_ENABLED
49#define LOG(x) \
50 if (auto clio_pump__ = x; not clio_pump__) \
51 ; \
52 else \
53 clio_pump__
54#else
55#define LOG(x) x
56#endif
57
61enum class Severity {
62 TRC,
63 DBG,
64 NFO,
65 WRN,
66 ERR,
67 FTL,
68};
69
78class Logger {
79 std::shared_ptr<spdlog::logger> logger_;
80
81 friend class LogService; // to expose the Pump interface
82 friend struct ::BenchmarkLoggingInitializer;
83
87 class Pump final {
88 std::shared_ptr<spdlog::logger> logger_;
89 Severity const severity_;
90 std::source_location const sourceLocation_;
91 std::ostringstream stream_;
92 bool const enabled_;
93
94 public:
95 ~Pump();
96
97 Pump(std::shared_ptr<spdlog::logger> logger, Severity sev, std::source_location const& loc);
98
99 Pump(Pump&&) = delete;
100 Pump(Pump const&) = delete;
101 Pump&
102 operator=(Pump const&) = delete;
103 Pump&
104 operator=(Pump&&) = delete;
105
114 template <typename T>
115 [[maybe_unused]] Pump&
116 operator<<(T&& data)
117 {
118 if (enabled_)
119 stream_ << std::forward<T>(data);
120 return *this;
121 }
122
126 operator bool() const
127 {
128 return enabled_;
129 }
130 };
131
132public:
133 static constexpr std::array<std::string_view, 8> kChannels = {
134 "General",
135 "WebServer",
136 "Backend",
137 "RPC",
138 "ETL",
139 "Subscriptions",
140 "Performance",
141 "Migration",
142 };
143
153 Logger(std::string_view const channel);
154
155 Logger(Logger const&) = default;
156 ~Logger();
157
158 Logger(Logger&&) = default;
159 Logger&
160 operator=(Logger const&) = default;
161
162 Logger&
163 operator=(Logger&&) = default;
164
171 [[nodiscard]] Pump
172 trace(std::source_location const& loc = std::source_location::current()) const;
173
180 [[nodiscard]] Pump
181 debug(std::source_location const& loc = std::source_location::current()) const;
182
189 [[nodiscard]] Pump
190 info(std::source_location const& loc = std::source_location::current()) const;
191
198 [[nodiscard]] Pump
199 warn(std::source_location const& loc = std::source_location::current()) const;
200
207 [[nodiscard]] Pump
208 error(std::source_location const& loc = std::source_location::current()) const;
209
216 [[nodiscard]] Pump
217 fatal(std::source_location const& loc = std::source_location::current()) const;
218
219private:
220 Logger(std::shared_ptr<spdlog::logger> logger);
221};
222
230protected:
231 friend struct ::LogServiceInitTests;
232 friend class ::LoggerFixture;
233 friend struct ::LogFileRotationTests;
234 friend class Logger;
235 friend class ::util::impl::OnAssert;
236
244 static void
245 init(
246 bool isAsync,
247 Severity defaultSeverity,
248 std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks
249 );
250
256 [[nodiscard]] static bool
257 initialized();
258
265 [[nodiscard]] static bool
266 hasSinks();
267
271 static void
272 reset();
273
279 static void
280 replaceSinks(std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks);
281
292 static std::shared_ptr<spdlog::logger>
293 registerLogger(std::string_view channel, std::optional<Severity> severity = std::nullopt);
294
295protected:
296 static bool isAsync_; // NOLINT(readability-identifier-naming)
297 static Severity defaultSeverity_; // NOLINT(readability-identifier-naming)
298 static std::vector<std::shared_ptr<spdlog::sinks::sink>>
299 sinks_; // NOLINT(readability-identifier-naming)
300 static bool initialized_; // NOLINT(readability-identifier-naming)
301};
302
309class LogService : public LogServiceState {
310public:
311 LogService() = delete;
312
319 [[nodiscard]] static std::expected<void, std::string>
320 init(config::ClioConfigDefinition const& config);
321
325 static void
326 shutdown();
327
334 [[nodiscard]] static Logger::Pump
335 trace(std::source_location const& loc = std::source_location::current());
336
343 [[nodiscard]] static Logger::Pump
344 debug(std::source_location const& loc = std::source_location::current());
345
352 [[nodiscard]] static Logger::Pump
353 info(std::source_location const& loc = std::source_location::current());
354
361 [[nodiscard]] static Logger::Pump
362 warn(std::source_location const& loc = std::source_location::current());
363
370 [[nodiscard]] static Logger::Pump
371 error(std::source_location const& loc = std::source_location::current());
372
379 [[nodiscard]] static Logger::Pump
380 fatal(std::source_location const& loc = std::source_location::current());
381
382private:
389 [[nodiscard]] static std::
390 expected<std::vector<std::shared_ptr<spdlog::sinks::sink>>, std::string>
391 getSinks(config::ClioConfigDefinition const& config);
392
393 struct RotationParams {
394 uint32_t sizeMB;
395 uint32_t maxFiles;
396 };
397
398 struct FileLoggingParams {
399 std::string logDir;
400 std::optional<RotationParams> rotation;
401 };
402
403 friend struct ::BenchmarkLoggingInitializer;
404
405 [[nodiscard]]
406 static std::shared_ptr<spdlog::sinks::sink>
407 createFileSink(FileLoggingParams const& params, std::string const& format);
408};
409
410}; // namespace util
Base state management class for the logging service.
Definition Logger.hpp:229
static bool initialized()
Whether the LogService is initialized or not.
Definition Logger.cpp:268
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:444
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:241
static void reset()
Reset the logging service to uninitialized state.
Definition Logger.cpp:280
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:292
static bool hasSinks()
Whether the LogService has any sink. If there is no sink, logger will not log messages anywhere.
Definition Logger.cpp:274
static Logger::Pump info(std::source_location const &loc=std::source_location::current())
Globally accessible General logger at Severity::NFO severity.
Definition Logger.cpp:420
static Logger::Pump error(std::source_location const &loc=std::source_location::current())
Globally accessible General logger at Severity::ERR severity.
Definition Logger.cpp:432
static Logger::Pump debug(std::source_location const &loc=std::source_location::current())
Globally accessible General logger at Severity::DBG severity.
Definition Logger.cpp:414
static void shutdown()
Shutdown spdlog to guarantee output is not lost.
Definition Logger.cpp:398
static Logger::Pump trace(std::source_location const &loc=std::source_location::current())
Globally accessible General logger at Severity::TRC severity.
Definition Logger.cpp:408
static std::expected< void, std::string > init(config::ClioConfigDefinition const &config)
Global log core initialization from a config::ClioConfigDefinition.
Definition Logger.cpp:364
static Logger::Pump fatal(std::source_location const &loc=std::source_location::current())
Globally accessible General logger at Severity::FTL severity.
Definition Logger.cpp:438
static Logger::Pump warn(std::source_location const &loc=std::source_location::current())
Globally accessible General logger at Severity::WRN severity.
Definition Logger.cpp:426
Pump info(std::source_location const &loc=std::source_location::current()) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:502
Pump error(std::source_location const &loc=std::source_location::current()) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:512
Pump fatal(std::source_location const &loc=std::source_location::current()) const
Interface for logging at Severity::FTL severity.
Definition Logger.cpp:517
Pump debug(std::source_location const &loc=std::source_location::current()) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:497
Pump trace(std::source_location const &loc=std::source_location::current()) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:492
Pump warn(std::source_location const &loc=std::source_location::current()) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:507
Logger(std::string_view const channel)
Construct a new Logger object that produces loglines for the specified channel.
Definition Logger.cpp:450
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
Definition Assert.hpp:18
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:56
This namespace contains various utilities.
Definition AccountUtils.hpp:11
Severity
Custom severity levels for util::Logger.
Definition Logger.hpp:61