Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Logger.hpp
1#pragma once
2
3#include "util/SourceLocation.hpp"
4
5#include <array>
6#include <cstddef>
7#include <cstdint>
8#include <expected>
9#include <memory>
10#include <optional>
11#include <sstream>
12#include <string>
13#include <string_view>
14#include <vector>
15
16// We forward declare spdlog::logger and spdlog::sinks::sink
17// to avoid including the spdlog headers in this header file.
18namespace spdlog {
19
20class logger; // NOLINT(readability-identifier-naming)
21
22namespace sinks {
23class sink; // NOLINT(readability-identifier-naming)
24} // namespace sinks
25
26} // namespace spdlog
27
28struct BenchmarkLoggingInitializer;
29class LoggerFixture;
30struct LogServiceInitTests;
31struct LogFileRotationTests;
32
33namespace util {
34
35namespace impl {
36class OnAssert;
37} // namespace impl
38
39namespace config {
41} // namespace config
42
49#ifndef COVERAGE_ENABLED
50#define LOG(x) \
51 if (auto clio_pump__ = x; not clio_pump__) { \
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 SourceLocationType 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, SourceLocationType 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(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
173
180 [[nodiscard]] Pump
181 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
182
189 [[nodiscard]] Pump
190 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
191
198 [[nodiscard]] Pump
199 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
200
207 [[nodiscard]] Pump
208 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
209
216 [[nodiscard]] Pump
217 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION) 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(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
336
343 [[nodiscard]] static Logger::Pump
344 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
345
352 [[nodiscard]] static Logger::Pump
353 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
354
361 [[nodiscard]] static Logger::Pump
362 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
363
370 [[nodiscard]] static Logger::Pump
371 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
372
379 [[nodiscard]] static Logger::Pump
380 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
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 error(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::ERR severity.
Definition Logger.cpp:432
static Logger::Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::DBG severity.
Definition Logger.cpp:414
static Logger::Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::WRN severity.
Definition Logger.cpp:426
static void shutdown()
Shutdown spdlog to guarantee output is not lost.
Definition Logger.cpp:398
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(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::FTL severity.
Definition Logger.cpp:438
static Logger::Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::TRC severity.
Definition Logger.cpp:408
static Logger::Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::NFO severity.
Definition Logger.cpp:420
Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:507
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:512
Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::FTL severity.
Definition Logger.cpp:517
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:497
Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:492
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:502
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:20
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