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
67#ifndef COVERAGE_ENABLED
68#define LOG(x) \
69 if (auto clio_pump__ = x; not clio_pump__) { \
70 } else \
71 clio_pump__
72#else
73#define LOG(x) x
74#endif
75
79enum class Severity {
80 TRC,
81 DBG,
82 NFO,
83 WRN,
84 ERR,
85 FTL,
86};
87
96class Logger {
97 std::shared_ptr<spdlog::logger> logger_;
98
99 friend class LogService; // to expose the Pump interface
100 friend struct ::BenchmarkLoggingInitializer;
101
105 class Pump final {
106 std::shared_ptr<spdlog::logger> logger_;
107 Severity const severity_;
108 SourceLocationType const sourceLocation_;
109 std::ostringstream stream_;
110 bool const enabled_;
111
112 public:
113 ~Pump();
114
115 Pump(std::shared_ptr<spdlog::logger> logger, Severity sev, SourceLocationType const& loc);
116
117 Pump(Pump&&) = delete;
118 Pump(Pump const&) = delete;
119 Pump&
120 operator=(Pump const&) = delete;
121 Pump&
122 operator=(Pump&&) = delete;
123
132 template <typename T>
133 [[maybe_unused]] Pump&
134 operator<<(T&& data)
135 {
136 if (enabled_)
137 stream_ << std::forward<T>(data);
138 return *this;
139 }
140
144 operator bool() const
145 {
146 return enabled_;
147 }
148 };
149
150public:
151 static constexpr std::array<std::string_view, 8> kCHANNELS = {
152 "General",
153 "WebServer",
154 "Backend",
155 "RPC",
156 "ETL",
157 "Subscriptions",
158 "Performance",
159 "Migration",
160 };
161
171 Logger(std::string_view const channel);
172
173 Logger(Logger const&) = default;
174 ~Logger();
175
176 Logger(Logger&&) = default;
177 Logger&
178 operator=(Logger const&) = default;
179
180 Logger&
181 operator=(Logger&&) = default;
182
189 [[nodiscard]] Pump
190 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
191
198 [[nodiscard]] Pump
199 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
200
207 [[nodiscard]] Pump
208 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
209
216 [[nodiscard]] Pump
217 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
218
225 [[nodiscard]] Pump
226 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
227
234 [[nodiscard]] Pump
235 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
236
237private:
238 Logger(std::shared_ptr<spdlog::logger> logger);
239};
240
248protected:
249 friend struct ::LogServiceInitTests;
250 friend class ::LoggerFixture;
251 friend class Logger;
252 friend class ::util::impl::OnAssert;
253
261 static void
262 init(
263 bool isAsync,
264 Severity defaultSeverity,
265 std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks
266 );
267
273 [[nodiscard]] static bool
274 initialized();
275
282 [[nodiscard]] static bool
283 hasSinks();
284
288 static void
289 reset();
290
296 static void
297 replaceSinks(std::vector<std::shared_ptr<spdlog::sinks::sink>> const& sinks);
298
309 static std::shared_ptr<spdlog::logger>
310 registerLogger(std::string_view channel, std::optional<Severity> severity = std::nullopt);
311
312protected:
313 static bool isAsync_; // NOLINT(readability-identifier-naming)
314 static Severity defaultSeverity_; // NOLINT(readability-identifier-naming)
315 static std::vector<std::shared_ptr<spdlog::sinks::sink>>
316 sinks_; // NOLINT(readability-identifier-naming)
317 static bool initialized_; // NOLINT(readability-identifier-naming)
318};
319
326class LogService : public LogServiceState {
327public:
328 LogService() = delete;
329
336 [[nodiscard]] static std::expected<void, std::string>
337 init(config::ClioConfigDefinition const& config);
338
342 static void
343 shutdown();
344
351 [[nodiscard]] static Logger::Pump
352 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
353
360 [[nodiscard]] static Logger::Pump
361 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
362
369 [[nodiscard]] static Logger::Pump
370 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
371
378 [[nodiscard]] static Logger::Pump
379 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
380
387 [[nodiscard]] static Logger::Pump
388 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
389
396 [[nodiscard]] static Logger::Pump
397 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
398
399private:
406 [[nodiscard]] static std::
407 expected<std::vector<std::shared_ptr<spdlog::sinks::sink>>, std::string>
408 getSinks(config::ClioConfigDefinition const& config);
409
410 struct FileLoggingParams {
411 std::string logDir;
412
413 uint32_t rotationSizeMB;
414 uint32_t dirMaxFiles;
415 };
416
417 friend struct ::BenchmarkLoggingInitializer;
418
419 [[nodiscard]]
420 static std::shared_ptr<spdlog::sinks::sink>
421 createFileSink(FileLoggingParams const& params, std::string const& format);
422};
423
424}; // namespace util
Base state management class for the logging service.
Definition Logger.hpp:247
static bool initialized()
Whether the LogService is initialized or not.
Definition Logger.cpp:280
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:449
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:253
static void reset()
Reset the logging service to uninitialized state.
Definition Logger.cpp:292
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:304
static bool hasSinks()
Whether the LogService has any sink. If there is no sink, logger will not log messages anywhere.
Definition Logger.cpp:286
static Logger::Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::ERR severity.
Definition Logger.cpp:437
static Logger::Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::DBG severity.
Definition Logger.cpp:419
static Logger::Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::WRN severity.
Definition Logger.cpp:431
static void shutdown()
Shutdown spdlog to guarantee output is not lost.
Definition Logger.cpp:403
static std::expected< void, std::string > init(config::ClioConfigDefinition const &config)
Global log core initialization from a config::ClioConfigDefinition.
Definition Logger.cpp:369
static Logger::Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::FTL severity.
Definition Logger.cpp:443
static Logger::Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::TRC severity.
Definition Logger.cpp:413
static Logger::Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::NFO severity.
Definition Logger.cpp:425
Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:512
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:517
Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::FTL severity.
Definition Logger.cpp:522
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:502
Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:497
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:507
Logger(std::string_view const channel)
Construct a new Logger object that produces loglines for the specified channel.
Definition Logger.cpp:455
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:75
This namespace contains various utilities.
Definition AccountUtils.hpp:30
Severity
Custom severity levels for util::Logger.
Definition Logger.hpp:79