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 <optional>
30#include <sstream>
31#include <string>
32#include <string_view>
33#include <vector>
34
35// We forward declare spdlog::logger and spdlog::sinks::sink
36// to avoid including the spdlog headers in this header file.
37namespace spdlog {
38
39class logger; // NOLINT(readability-identifier-naming)
40
41namespace sinks {
42class sink; // NOLINT(readability-identifier-naming)
43} // namespace sinks
44
45} // namespace spdlog
46
47struct BenchmarkLoggingInitializer;
48class LoggerFixture;
49struct LogServiceInitTests;
50
51namespace util {
52
53namespace impl {
54class OnAssert;
55} // namespace impl
56
57namespace config {
59} // namespace config
60
66#ifndef COVERAGE_ENABLED
67#define LOG(x) \
68 if (auto clio_pump__ = x; not clio_pump__) { \
69 } else \
70 clio_pump__
71#else
72#define LOG(x) x
73#endif
74
78enum class Severity {
79 TRC,
80 DBG,
81 NFO,
82 WRN,
83 ERR,
84 FTL,
85};
86
95class Logger {
96 std::shared_ptr<spdlog::logger> logger_;
97
98 friend class LogService; // to expose the Pump interface
99 friend struct ::BenchmarkLoggingInitializer;
100
104 class Pump final {
105 std::shared_ptr<spdlog::logger> logger_;
106 Severity const severity_;
107 SourceLocationType const sourceLocation_;
108 std::ostringstream stream_;
109 bool const enabled_;
110
111 public:
112 ~Pump();
113
114 Pump(std::shared_ptr<spdlog::logger> logger, Severity sev, SourceLocationType const& loc);
115
116 Pump(Pump&&) = delete;
117 Pump(Pump const&) = delete;
118 Pump&
119 operator=(Pump const&) = delete;
120 Pump&
121 operator=(Pump&&) = delete;
122
130 template <typename T>
131 [[maybe_unused]] Pump&
132 operator<<(T&& data)
133 {
134 if (enabled_)
135 stream_ << std::forward<T>(data);
136 return *this;
137 }
138
142 operator bool() const
143 {
144 return enabled_;
145 }
146 };
147
148public:
149 static constexpr std::array<std::string_view, 8> kCHANNELS = {
150 "General",
151 "WebServer",
152 "Backend",
153 "RPC",
154 "ETL",
155 "Subscriptions",
156 "Performance",
157 "Migration",
158 };
159
169 Logger(std::string_view const channel);
170
171 Logger(Logger const&) = default;
172 ~Logger();
173
174 Logger(Logger&&) = default;
175 Logger&
176 operator=(Logger const&) = default;
177
178 Logger&
179 operator=(Logger&&) = default;
180
187 [[nodiscard]] Pump
188 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
189
196 [[nodiscard]] Pump
197 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
198
205 [[nodiscard]] Pump
206 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
207
214 [[nodiscard]] Pump
215 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
216
223 [[nodiscard]] Pump
224 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
225
232 [[nodiscard]] Pump
233 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
234
235private:
236 Logger(std::shared_ptr<spdlog::logger> logger);
237};
238
246protected:
247 friend struct ::LogServiceInitTests;
248 friend class ::LoggerFixture;
249 friend class Logger;
250 friend class ::util::impl::OnAssert;
251
259 static void
260 init(bool isAsync, Severity defaultSeverity, std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks);
261
267 [[nodiscard]] static bool
268 initialized();
269
273 static void
274 reset();
275
281 static void
282 replaceSinks(std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks);
283
294 static std::shared_ptr<spdlog::logger>
295 registerLogger(std::string_view channel, std::optional<Severity> severity = std::nullopt);
296
297protected:
298 static bool isAsync_; // NOLINT(readability-identifier-naming)
299 static Severity defaultSeverity_; // NOLINT(readability-identifier-naming)
300 static std::vector<std::shared_ptr<spdlog::sinks::sink>> sinks_; // NOLINT(readability-identifier-naming)
301 static bool initialized_; // NOLINT(readability-identifier-naming)
302};
303
310class LogService : public LogServiceState {
311public:
312 LogService() = delete;
313
320 [[nodiscard]] static std::expected<void, std::string>
321 init(config::ClioConfigDefinition const& config);
322
326 static void
327 shutdown();
328
335 [[nodiscard]] static Logger::Pump
336 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
337
344 [[nodiscard]] static Logger::Pump
345 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
346
353 [[nodiscard]] static Logger::Pump
354 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
355
362 [[nodiscard]] static Logger::Pump
363 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
364
371 [[nodiscard]] static Logger::Pump
372 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
373
380 [[nodiscard]] static Logger::Pump
381 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
382
383private:
390 [[nodiscard]] static std::expected<std::vector<std::shared_ptr<spdlog::sinks::sink>>, std::string>
391 getSinks(config::ClioConfigDefinition const& config);
392
393 struct FileLoggingParams {
394 std::string logDir;
395
396 uint32_t rotationSizeMB;
397 uint32_t dirMaxFiles;
398 };
399
400 friend struct ::BenchmarkLoggingInitializer;
401
402 [[nodiscard]]
403 static std::shared_ptr<spdlog::sinks::sink>
404 createFileSink(FileLoggingParams const& params, std::string const& format);
405};
406
407}; // namespace util
Base state management class for the logging service.
Definition Logger.hpp:245
static bool initialized()
Whether the LogService is initialized or not.
Definition Logger.cpp:269
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:426
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:246
static void reset()
Reset the logging service to uninitialized state.
Definition Logger.cpp:275
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:287
static Logger::Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::ERR severity.
Definition Logger.cpp:414
static Logger::Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::DBG severity.
Definition Logger.cpp:396
static Logger::Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::WRN severity.
Definition Logger.cpp:408
static void shutdown()
Shutdown spdlog to guarantee output is not lost.
Definition Logger.cpp:381
static std::expected< void, std::string > init(config::ClioConfigDefinition const &config)
Global log core initialization from a config::ClioConfigDefinition.
Definition Logger.cpp:347
static Logger::Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::FTL severity.
Definition Logger.cpp:420
static Logger::Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::TRC severity.
Definition Logger.cpp:390
static Logger::Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::NFO severity.
Definition Logger.cpp:402
Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:485
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:490
Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::FTL severity.
Definition Logger.cpp:495
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:475
Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:470
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:480
Logger(std::string_view const channel)
Construct a new Logger object that produces loglines for the specified channel.
Definition Logger.cpp:432
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:50
Definition Assert.hpp:38
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:78