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
275 [[nodiscard]] static bool
276 hasSinks();
277
281 static void
282 reset();
283
289 static void
290 replaceSinks(std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks);
291
302 static std::shared_ptr<spdlog::logger>
303 registerLogger(std::string_view channel, std::optional<Severity> severity = std::nullopt);
304
305protected:
306 static bool isAsync_; // NOLINT(readability-identifier-naming)
307 static Severity defaultSeverity_; // NOLINT(readability-identifier-naming)
308 static std::vector<std::shared_ptr<spdlog::sinks::sink>> sinks_; // NOLINT(readability-identifier-naming)
309 static bool initialized_; // NOLINT(readability-identifier-naming)
310};
311
318class LogService : public LogServiceState {
319public:
320 LogService() = delete;
321
328 [[nodiscard]] static std::expected<void, std::string>
329 init(config::ClioConfigDefinition const& config);
330
334 static void
335 shutdown();
336
343 [[nodiscard]] static Logger::Pump
344 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
345
352 [[nodiscard]] static Logger::Pump
353 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
354
361 [[nodiscard]] static Logger::Pump
362 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
363
370 [[nodiscard]] static Logger::Pump
371 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
372
379 [[nodiscard]] static Logger::Pump
380 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
381
388 [[nodiscard]] static Logger::Pump
389 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
390
391private:
398 [[nodiscard]] static std::expected<std::vector<std::shared_ptr<spdlog::sinks::sink>>, std::string>
399 getSinks(config::ClioConfigDefinition const& config);
400
401 struct FileLoggingParams {
402 std::string logDir;
403
404 uint32_t rotationSizeMB;
405 uint32_t dirMaxFiles;
406 };
407
408 friend struct ::BenchmarkLoggingInitializer;
409
410 [[nodiscard]]
411 static std::shared_ptr<spdlog::sinks::sink>
412 createFileSink(FileLoggingParams const& params, std::string const& format);
413};
414
415}; // 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:432
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:281
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:293
static bool hasSinks()
Whether the LogService has any sink. If there is no sink, logger will not log messages anywhere.
Definition Logger.cpp:275
static Logger::Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::ERR severity.
Definition Logger.cpp:420
static Logger::Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::DBG severity.
Definition Logger.cpp:402
static Logger::Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::WRN severity.
Definition Logger.cpp:414
static void shutdown()
Shutdown spdlog to guarantee output is not lost.
Definition Logger.cpp:387
static std::expected< void, std::string > init(config::ClioConfigDefinition const &config)
Global log core initialization from a config::ClioConfigDefinition.
Definition Logger.cpp:353
static Logger::Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::FTL severity.
Definition Logger.cpp:426
static Logger::Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::TRC severity.
Definition Logger.cpp:396
static Logger::Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::NFO severity.
Definition Logger.cpp:408
Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:491
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:496
Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::FTL severity.
Definition Logger.cpp:501
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:481
Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:476
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:486
Logger(std::string_view const channel)
Construct a new Logger object that produces loglines for the specified channel.
Definition Logger.cpp:438
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:50
Definition Assert.hpp:39
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