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 <sstream>
30#include <string>
31#include <string_view>
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;
47
48namespace util {
49
50namespace config {
51class ClioConfigDefinition;
52} // namespace config
53
59#ifndef COVERAGE_ENABLED
60#define LOG(x) \
61 if (auto clio_pump__ = x; not clio_pump__) { \
62 } else \
63 clio_pump__
64#else
65#define LOG(x) x
66#endif
67
71enum class Severity {
72 TRC,
73 DBG,
74 NFO,
75 WRN,
76 ERR,
77 FTL,
78};
79
88class Logger final {
89 std::shared_ptr<spdlog::logger> logger_;
90
91 friend class LogService; // to expose the Pump interface
92 friend struct ::BenchmarkLoggingInitializer;
93
97 class Pump final {
98 std::shared_ptr<spdlog::logger> logger_;
99 Severity const severity_;
100 SourceLocationType const sourceLocation_;
101 std::ostringstream stream_;
102 bool const enabled_;
103
104 public:
105 ~Pump();
106
107 Pump(std::shared_ptr<spdlog::logger> logger, Severity sev, SourceLocationType const& loc);
108
109 Pump(Pump&&) = delete;
110 Pump(Pump const&) = delete;
111 Pump&
112 operator=(Pump const&) = delete;
113 Pump&
114 operator=(Pump&&) = delete;
115
123 template <typename T>
124 [[maybe_unused]] Pump&
125 operator<<(T&& data)
126 {
127 if (enabled_)
128 stream_ << std::forward<T>(data);
129 return *this;
130 }
131
135 operator bool() const
136 {
137 return enabled_;
138 }
139
140 private:
141 [[nodiscard]] static std::string_view
142 prettyPath(SourceLocationType const& loc, size_t maxDepth = 3);
143 };
144
145public:
146 static constexpr std::array<char const*, 8> kCHANNELS = {
147 "General",
148 "WebServer",
149 "Backend",
150 "RPC",
151 "ETL",
152 "Subscriptions",
153 "Performance",
154 "Migration",
155 };
156
166 Logger(std::string channel);
167
168 Logger(Logger const&) = default;
169 ~Logger() = default;
170
171 Logger(Logger&&) = default;
172 Logger&
173 operator=(Logger const&) = default;
174
175 Logger&
176 operator=(Logger&&) = default;
177
184 [[nodiscard]] Pump
185 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
186
193 [[nodiscard]] Pump
194 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
195
202 [[nodiscard]] Pump
203 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
204
211 [[nodiscard]] Pump
212 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
213
220 [[nodiscard]] Pump
221 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
222
229 [[nodiscard]] Pump
230 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION) const;
231
232private:
233 Logger(std::shared_ptr<spdlog::logger> logger);
234};
235
243 struct Data {
244 bool isAsync;
245 Severity severity;
246 std::vector<std::shared_ptr<spdlog::sinks::sink>> allSinks;
247 };
248
249 friend class Logger;
250
251private:
252 static Data data;
253
254 static std::shared_ptr<spdlog::logger>
255 registerLogger(std::string const& channel, Severity severity = data.severity);
256
257public:
258 LogService() = delete;
259
266 [[nodiscard]] static std::expected<void, std::string>
267 init(config::ClioConfigDefinition const& config);
268
272 static void
273 shutdown();
274
281 [[nodiscard]] static Logger::Pump
282 trace(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
283
290 [[nodiscard]] static Logger::Pump
291 debug(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
292
299 [[nodiscard]] static Logger::Pump
300 info(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
301
308 [[nodiscard]] static Logger::Pump
309 warn(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
310
317 [[nodiscard]] static Logger::Pump
318 error(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
319
326 [[nodiscard]] static Logger::Pump
327 fatal(SourceLocationType const& loc = CURRENT_SRC_LOCATION);
328
334 [[nodiscard]] static bool
335 enabled();
336
337private:
338 struct FileLoggingParams {
339 std::string logDir;
340
341 uint32_t rotationSizeMB;
342 uint32_t dirMaxFiles;
343 };
344
345 friend struct ::BenchmarkLoggingInitializer;
346
347 [[nodiscard]]
348 static std::shared_ptr<spdlog::sinks::sink>
349 createFileSink(FileLoggingParams const& params);
350};
351
352}; // namespace util
A global logging service.
Definition Logger.hpp:242
static Logger::Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::ERR severity.
Definition Logger.cpp:310
static Logger::Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::DBG severity.
Definition Logger.cpp:292
static Logger::Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::WRN severity.
Definition Logger.cpp:304
static void shutdown()
Shutdown spdlog to guarantee output is not lost.
Definition Logger.cpp:280
static std::expected< void, std::string > init(config::ClioConfigDefinition const &config)
Global log core initialization from a config::ClioConfigDefinition.
Definition Logger.cpp:224
static Logger::Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::FTL severity.
Definition Logger.cpp:316
static bool enabled()
Whether the LogService is enabled or not.
Definition Logger.cpp:322
static Logger::Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::TRC severity.
Definition Logger.cpp:286
static Logger::Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accessible General logger at Severity::NFO severity.
Definition Logger.cpp:298
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:88
Logger(std::string channel)
Construct a new Logger object that produces loglines for the specified channel.
Definition Logger.cpp:327
Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:366
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:371
Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::FTL severity.
Definition Logger.cpp:376
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:356
Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:351
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:361
A class representing the source location of the current code.
Definition SourceLocation.hpp:52
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
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:71