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;
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 } else \
52 clio_pump__
53#else
54#define LOG(x) x
55#endif
56
60enum class Severity {
61 TRC,
62 DBG,
63 NFO,
64 WRN,
65 ERR,
66 FTL,
67};
68
77class Logger {
78 std::shared_ptr<spdlog::logger> logger_;
79
80 friend class LogService; // to expose the Pump interface
81 friend struct ::BenchmarkLoggingInitializer;
82
86 class Pump final {
87 std::shared_ptr<spdlog::logger> logger_;
88 Severity const severity_;
89 SourceLocationType const sourceLocation_;
90 std::ostringstream stream_;
91 bool const enabled_;
92
93 public:
94 ~Pump();
95
96 Pump(std::shared_ptr<spdlog::logger> logger, Severity sev, SourceLocationType const& loc);
97
98 Pump(Pump&&) = delete;
99 Pump(Pump const&) = delete;
100 Pump&
101 operator=(Pump const&) = delete;
102 Pump&
103 operator=(Pump&&) = delete;
104
113 template <typename T>
114 [[maybe_unused]] Pump&
115 operator<<(T&& data)
116 {
117 if (enabled_)
118 stream_ << std::forward<T>(data);
119 return *this;
120 }
121
125 operator bool() const
126 {
127 return enabled_;
128 }
129 };
130
131public:
132 static constexpr std::array<std::string_view, 8> kCHANNELS = {
133 "General",
134 "WebServer",
135 "Backend",
136 "RPC",
137 "ETL",
138 "Subscriptions",
139 "Performance",
140 "Migration",
141 };
142
152 Logger(std::string_view const channel);
153
154 Logger(Logger const&) = default;
155 ~Logger();
156
157 Logger(Logger&&) = default;
158 Logger&
159 operator=(Logger const&) = default;
160
161 Logger&
162 operator=(Logger&&) = default;
163
170 [[nodiscard]] Pump
171 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
172
179 [[nodiscard]] Pump
180 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
181
188 [[nodiscard]] Pump
189 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
190
197 [[nodiscard]] Pump
198 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
199
206 [[nodiscard]] Pump
207 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
208
215 [[nodiscard]] Pump
216 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
217
218private:
219 Logger(std::shared_ptr<spdlog::logger> logger);
220};
221
229protected:
230 friend struct ::LogServiceInitTests;
231 friend class ::LoggerFixture;
232 friend class Logger;
233 friend class ::util::impl::OnAssert;
234
242 static void
243 init(
244 bool isAsync,
245 Severity defaultSeverity,
246 std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks
247 );
248
254 [[nodiscard]] static bool
255 initialized();
256
263 [[nodiscard]] static bool
264 hasSinks();
265
269 static void
270 reset();
271
277 static void
278 replaceSinks(std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks);
279
290 static std::shared_ptr<spdlog::logger>
291 registerLogger(std::string_view channel, std::optional<Severity> severity = std::nullopt);
292
293protected:
294 static bool isAsync_; // NOLINT(readability-identifier-naming)
295 static Severity defaultSeverity_; // NOLINT(readability-identifier-naming)
296 static std::vector<std::shared_ptr<spdlog::sinks::sink>>
297 sinks_; // NOLINT(readability-identifier-naming)
298 static bool initialized_; // NOLINT(readability-identifier-naming)
299};
300
307class LogService : public LogServiceState {
308public:
309 LogService() = delete;
310
317 [[nodiscard]] static std::expected<void, std::string>
318 init(config::ClioConfigDefinition const& config);
319
323 static void
324 shutdown();
325
332 [[nodiscard]] static Logger::Pump
333 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
334
341 [[nodiscard]] static Logger::Pump
342 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
343
350 [[nodiscard]] static Logger::Pump
351 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
352
359 [[nodiscard]] static Logger::Pump
360 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
361
368 [[nodiscard]] static Logger::Pump
369 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
370
377 [[nodiscard]] static Logger::Pump
378 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
379
380private:
387 [[nodiscard]] static std::
388 expected<std::vector<std::shared_ptr<spdlog::sinks::sink>>, std::string>
389 getSinks(config::ClioConfigDefinition const& config);
390
391 struct FileLoggingParams {
392 std::string logDir;
393
394 uint32_t rotationSizeMB;
395 uint32_t dirMaxFiles;
396 };
397
398 friend struct ::BenchmarkLoggingInitializer;
399
400 [[nodiscard]]
401 static std::shared_ptr<spdlog::sinks::sink>
402 createFileSink(FileLoggingParams const& params, std::string const& format);
403};
404
405}; // namespace util
Base state management class for the logging service.
Definition Logger.hpp:228
static bool initialized()
Whether the LogService is initialized or not.
Definition Logger.cpp:261
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:430
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:234
static void reset()
Reset the logging service to uninitialized state.
Definition Logger.cpp:273
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:285
static bool hasSinks()
Whether the LogService has any sink. If there is no sink, logger will not log messages anywhere.
Definition Logger.cpp:267
static Logger::Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::ERR severity.
Definition Logger.cpp:418
static Logger::Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::DBG severity.
Definition Logger.cpp:400
static Logger::Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::WRN severity.
Definition Logger.cpp:412
static void shutdown()
Shutdown spdlog to guarantee output is not lost.
Definition Logger.cpp:384
static std::expected< void, std::string > init(config::ClioConfigDefinition const &config)
Global log core initialization from a config::ClioConfigDefinition.
Definition Logger.cpp:350
static Logger::Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::FTL severity.
Definition Logger.cpp:424
static Logger::Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::TRC severity.
Definition Logger.cpp:394
static Logger::Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::NFO severity.
Definition Logger.cpp:406
Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:493
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:498
Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::FTL severity.
Definition Logger.cpp:503
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:483
Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:478
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:488
Logger(std::string_view const channel)
Construct a new Logger object that produces loglines for the specified channel.
Definition Logger.cpp:436
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:60