xrpld
Loading...
Searching...
No Matches
Log.h
1#pragma once
2
3#include <xrpl/beast/utility/Journal.h>
4
5#include <boost/beast/core/string.hpp>
6
7#include <filesystem>
8#include <fstream>
9#include <map>
10#include <memory>
11#include <mutex>
12#include <optional>
13#include <string>
14#include <utility>
15#include <vector>
16
17namespace xrpl {
18
22class Logs
23{
24private:
26 {
27 private:
30
31 public:
32 Sink(std::string partition, beast::Severity thresh, Logs& logs);
33
34 Sink(Sink const&) = delete;
35 Sink&
36 operator=(Sink const&) = delete;
37
38 void
39 write(beast::Severity level, std::string const& text) override;
40
41 void
42 writeAlways(beast::Severity level, std::string const& text) override;
43 };
44
53 class File
54 {
55 public:
61 File();
62
67 ~File() = default;
68
74 [[nodiscard]] bool
75 isOpen() const noexcept;
76
86 bool
87 open(std::filesystem::path const& path);
88
94 bool
96
100 void
101 close();
102
107 void
108 write(char const* text);
109
114 void
115 writeln(char const* text);
116
121 void
122 write(std::string const& str)
123 {
124 write(str.c_str());
125 }
126
127 void
129 {
130 writeln(str.c_str());
131 }
132
133
134 private:
137 };
138
143 bool silent_ = false;
144
145public:
146 Logs(beast::Severity level);
147
148 Logs(Logs const&) = delete;
149 Logs&
150 operator=(Logs const&) = delete;
151
152 virtual ~Logs() = default;
153
154 bool
155 open(std::filesystem::path const& pathToLogFile);
156
158 get(std::string const& name);
159
161 operator[](std::string const& name);
162
164 journal(std::string const& name);
165
167 threshold() const;
168
169 void
171
173 partitionSeverities() const;
174
175 void
176 write(
177 beast::Severity level,
178 std::string const& partition,
179 std::string const& text,
180 bool console);
181
183 rotate();
184
190 void
191 silent(bool bSilent)
192 {
193 silent_ = bSilent;
194 }
195
197 makeSink(std::string const& partition, beast::Severity startingLevel);
198
199public:
200 static std::string
202
204 fromString(std::string const& s);
205
206private:
207 // Maximum line length for log messages.
208 // If the message exceeds this length it will be truncated with ellipses.
209 static constexpr auto kMaximumMessageCharacters = 12 * 1024;
210
211 static void
212 format(
213 std::string& output,
214 std::string const& message,
215 beast::Severity severity,
216 std::string const& partition);
217};
218
219// Wraps a Journal::Stream to skip evaluation of
220// expensive argument lists if the stream is not active.
221#ifndef JLOG
222#define JLOG(x) \
223 if (!(x)) \
224 ; \
225 else \
226 x
227#endif
228
229#ifndef CLOG
230#define CLOG(ss) \
231 if (!(ss)) \
232 ; \
233 else \
234 *ss
235#endif
236
237//------------------------------------------------------------------------------
238// Debug logging:
239
248
256debugLog();
257
258} // namespace xrpl
T c_str(T... args)
Abstraction for the underlying message destination.
Definition Journal.h:59
A generic endpoint for log messages.
Definition Journal.h:44
Manages a system file containing logged output.
Definition Log.h:54
void writeln(char const *text)
write to the log file and append an end of line marker.
Definition Log.cpp:101
bool closeAndReopen()
Close and re-open the system file associated with the log This assists in interoperating with externa...
Definition Log.cpp:80
void write(char const *text)
write to the log file.
Definition Log.cpp:94
void writeln(std::string const &str)
Definition Log.h:128
bool open(std::filesystem::path const &path)
Associate a system file with the log.
Definition Log.cpp:57
bool isOpen() const noexcept
Determine if a system file is associated with the log.
Definition Log.cpp:51
void close()
Close the system file if it is open.
Definition Log.cpp:88
std::unique_ptr< std::ofstream > stream_
Definition Log.h:135
std::filesystem::path path_
Definition Log.h:136
~File()=default
Destroy the object.
File()
Construct with no associated system file.
Definition Log.cpp:46
Sink & operator=(Sink const &)=delete
Sink(Sink const &)=delete
void writeAlways(beast::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
Definition Log.cpp:39
void write(beast::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition Log.cpp:30
Sink(std::string partition, beast::Severity thresh, Logs &logs)
Definition Log.cpp:24
std::string partition_
Definition Log.h:29
Logs & logs_
Definition Log.h:28
std::vector< std::pair< std::string, std::string > > partitionSeverities() const
Definition Log.cpp:158
void silent(bool bSilent)
Set flag to write logs to stderr (false) or not (true).
Definition Log.h:191
bool open(std::filesystem::path const &pathToLogFile)
Definition Log.cpp:117
beast::Journal journal(std::string const &name)
Definition Log.cpp:137
Logs(Logs const &)=delete
Logs & operator=(Logs const &)=delete
static std::string toString(beast::Severity s)
Definition Log.cpp:203
beast::Journal::Sink & get(std::string const &name)
Definition Log.cpp:123
beast::Severity thresh_
Definition Log.h:141
bool silent_
Definition Log.h:143
beast::Severity threshold() const
Definition Log.cpp:143
void write(beast::Severity level, std::string const &partition, std::string const &text, bool console)
Definition Log.cpp:169
static void format(std::string &output, std::string const &message, beast::Severity severity, std::string const &partition)
Definition Log.cpp:252
std::mutex mutex_
Definition Log.h:139
static std::optional< beast::Severity > fromString(std::string const &s)
Definition Log.cpp:228
virtual ~Logs()=default
std::string rotate()
Definition Log.cpp:187
std::map< std::string, std::unique_ptr< beast::Journal::Sink >, boost::beast::iless > sinks_
Definition Log.h:140
static constexpr auto kMaximumMessageCharacters
Definition Log.h:209
Logs(beast::Severity level)
Definition Log.cpp:112
File file_
Definition Log.h:142
virtual std::unique_ptr< beast::Journal::Sink > makeSink(std::string const &partition, beast::Severity startingLevel)
Definition Log.cpp:197
beast::Journal::Sink & operator[](std::string const &name)
Definition Log.cpp:131
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:16
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:399
std::unique_ptr< beast::Journal::Sink > setDebugLogSink(std::unique_ptr< beast::Journal::Sink > sink)
Set the sink for the debug journal.
Definition Log.cpp:393