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 <vector>
33
34// We forward declare spdlog::logger and spdlog::sinks::sink
35// to avoid including the spdlog headers in this header file.
36namespace spdlog {
37
38class logger; // NOLINT(readability-identifier-naming)
39
40namespace sinks {
41class sink; // NOLINT(readability-identifier-naming)
42} // namespace sinks
43
44} // namespace spdlog
45
46struct BenchmarkLoggingInitializer;
47class LoggerFixture;
48struct LogServiceInitTests;
49
50namespace util {
51
52namespace impl {
53class OnAssert;
54} // namespace impl
55
56namespace config {
58} // namespace config
59
65#ifndef COVERAGE_ENABLED
66#define LOG(x) \
67 if (auto clio_pump__ = x; not clio_pump__) { \
68 } else \
69 clio_pump__
70#else
71#define LOG(x) x
72#endif
73
77enum class Severity {
78 TRC,
79 DBG,
80 NFO,
81 WRN,
82 ERR,
83 FTL,
84};
85
94class Logger final {
95 std::shared_ptr<spdlog::logger> logger_;
96
97 friend class LogService; // to expose the Pump interface
98 friend struct ::BenchmarkLoggingInitializer;
99
103 class Pump final {
104 std::shared_ptr<spdlog::logger> logger_;
105 Severity const severity_;
106 SourceLocationType const sourceLocation_;
107 std::ostringstream stream_;
108 bool const enabled_;
109
110 public:
111 ~Pump();
112
113 Pump(std::shared_ptr<spdlog::logger> logger, Severity sev, SourceLocationType const& loc);
114
115 Pump(Pump&&) = delete;
116 Pump(Pump const&) = delete;
117 Pump&
118 operator=(Pump const&) = delete;
119 Pump&
120 operator=(Pump&&) = delete;
121
129 template <typename T>
130 [[maybe_unused]] Pump&
131 operator<<(T&& data)
132 {
133 if (enabled_)
134 stream_ << std::forward<T>(data);
135 return *this;
136 }
137
141 operator bool() const
142 {
143 return enabled_;
144 }
145 };
146
147public:
148 static constexpr std::array<char const*, 8> kCHANNELS = {
149 "General",
150 "WebServer",
151 "Backend",
152 "RPC",
153 "ETL",
154 "Subscriptions",
155 "Performance",
156 "Migration",
157 };
158
168 Logger(std::string channel);
169
170 Logger(Logger const&) = default;
171 ~Logger() = default;
172
173 Logger(Logger&&) = default;
174 Logger&
175 operator=(Logger const&) = default;
176
177 Logger&
178 operator=(Logger&&) = default;
179
186 [[nodiscard]] Pump
187 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
188
195 [[nodiscard]] Pump
196 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
197
204 [[nodiscard]] Pump
205 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
206
213 [[nodiscard]] Pump
214 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
215
222 [[nodiscard]] Pump
223 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
224
231 [[nodiscard]] Pump
232 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
233
234private:
235 Logger(std::shared_ptr<spdlog::logger> logger);
236};
237
245protected:
246 friend struct ::LogServiceInitTests;
247 friend class ::LoggerFixture;
248 friend class Logger;
249 friend class ::util::impl::OnAssert;
250
258 static void
259 init(bool isAsync, Severity defaultSeverity, std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks);
260
266 [[nodiscard]] static bool
267 initialized();
268
272 static void
273 reset();
274
280 static void
281 replaceSinks(std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks);
282
293 static std::shared_ptr<spdlog::logger>
294 registerLogger(std::string const& channel, std::optional<Severity> severity = std::nullopt);
295
296protected:
297 static bool isAsync_; // NOLINT(readability-identifier-naming)
298 static Severity defaultSeverity_; // NOLINT(readability-identifier-naming)
299 static std::vector<std::shared_ptr<spdlog::sinks::sink>> 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::expected<std::vector<std::shared_ptr<spdlog::sinks::sink>>, std::string>
390 getSinks(config::ClioConfigDefinition const& config);
391
392 struct FileLoggingParams {
393 std::string logDir;
394
395 uint32_t rotationSizeMB;
396 uint32_t dirMaxFiles;
397 };
398
399 friend struct ::BenchmarkLoggingInitializer;
400
401 [[nodiscard]]
402 static std::shared_ptr<spdlog::sinks::sink>
403 createFileSink(FileLoggingParams const& params, std::string const& format);
404};
405
406}; // namespace util
Base state management class for the logging service.
Definition Logger.hpp:244
static bool initialized()
Whether the LogService is initialized or not.
Definition Logger.cpp:269
static std::shared_ptr< spdlog::logger > registerLogger(std::string const &channel, std::optional< Severity > severity=std::nullopt)
Register a new logger for the specified channel.
Definition Logger.cpp:287
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:424
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 Logger::Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::ERR severity.
Definition Logger.cpp:412
static Logger::Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::DBG severity.
Definition Logger.cpp:394
static Logger::Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::WRN severity.
Definition Logger.cpp:406
static void shutdown()
Shutdown spdlog to guarantee output is not lost.
Definition Logger.cpp:379
static std::expected< void, std::string > init(config::ClioConfigDefinition const &config)
Global log core initialization from a config::ClioConfigDefinition.
Definition Logger.cpp:345
static Logger::Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::FTL severity.
Definition Logger.cpp:418
static Logger::Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::TRC severity.
Definition Logger.cpp:388
static Logger::Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::NFO severity.
Definition Logger.cpp:400
Logger(std::string channel)
Construct a new Logger object that produces loglines for the specified channel.
Definition Logger.cpp:430
Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:468
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:473
Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::FTL severity.
Definition Logger.cpp:478
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:458
Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:453
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:463
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:77