xrpld
Loading...
Searching...
No Matches
Log.h
1#pragma once
2
3#include <xrpl/basics/UnorderedContainers.h>
4#include <xrpl/beast/utility/Journal.h>
5
6#include <boost/beast/core/string.hpp>
7#include <boost/filesystem.hpp>
8
9#include <fstream>
10#include <map>
11#include <memory>
12#include <mutex>
13#include <optional>
14#include <utility>
15
16namespace xrpl {
17
19class Logs
20{
21private:
23 {
24 private:
27
28 public:
29 Sink(std::string partition, beast::Severity thresh, Logs& logs);
30
31 Sink(Sink const&) = delete;
32 Sink&
33 operator=(Sink const&) = delete;
34
35 void
36 write(beast::Severity level, std::string const& text) override;
37
38 void
39 writeAlways(beast::Severity level, std::string const& text) override;
40 };
41
49 class File
50 {
51 public:
56 File();
57
61 ~File() = default;
62
67 [[nodiscard]] bool
68 isOpen() const noexcept;
69
78 bool
79 open(boost::filesystem::path const& path);
80
85 bool
87
89 void
90 close();
91
95 void
96 write(char const* text);
97
101 void
102 writeln(char const* text);
103
106 void
107 write(std::string const& str)
108 {
109 write(str.c_str());
110 }
111
112 void
114 {
115 writeln(str.c_str());
116 }
117
118
119 private:
121 boost::filesystem::path path_;
122 };
123
128 bool silent_ = false;
129
130public:
131 Logs(beast::Severity level);
132
133 Logs(Logs const&) = delete;
134 Logs&
135 operator=(Logs const&) = delete;
136
137 virtual ~Logs() = default;
138
139 bool
140 open(boost::filesystem::path const& pathToLogFile);
141
143 get(std::string const& name);
144
146 operator[](std::string const& name);
147
149 journal(std::string const& name);
150
152 threshold() const;
153
154 void
156
158 partitionSeverities() const;
159
160 void
161 write(
162 beast::Severity level,
163 std::string const& partition,
164 std::string const& text,
165 bool console);
166
168 rotate();
169
175 void
176 silent(bool bSilent)
177 {
178 silent_ = bSilent;
179 }
180
182 makeSink(std::string const& partition, beast::Severity startingLevel);
183
184public:
185 static std::string
187
189 fromString(std::string const& s);
190
191private:
192 // Maximum line length for log messages.
193 // If the message exceeds this length it will be truncated with ellipses.
194 static constexpr auto kMaximumMessageCharacters = 12 * 1024;
195
196 static void
197 format(
198 std::string& output,
199 std::string const& message,
200 beast::Severity severity,
201 std::string const& partition);
202};
203
204// Wraps a Journal::Stream to skip evaluation of
205// expensive argument lists if the stream is not active.
206#ifndef JLOG
207#define JLOG(x) \
208 if (!(x)) \
209 ; \
210 else \
211 x
212#endif
213
214#ifndef CLOG
215#define CLOG(ss) \
216 if (!(ss)) \
217 ; \
218 else \
219 *ss
220#endif
221
222//------------------------------------------------------------------------------
223// Debug logging:
224
232
239debugLog();
240
241} // namespace xrpl
T c_str(T... args)
Abstraction for the underlying message destination.
Definition Journal.h:51
A generic endpoint for log messages.
Definition Journal.h:38
Manages a system file containing logged output.
Definition Log.h:50
void writeln(char const *text)
write to the log file and append an end of line marker.
Definition Log.cpp:101
bool open(boost::filesystem::path const &path)
Associate a system file with the log.
Definition Log.cpp:57
bool closeAndReopen()
Close and re-open the system file associated with the log This assists in interoperating with externa...
Definition Log.cpp:80
boost::filesystem::path path_
Definition Log.h:121
void write(char const *text)
write to the log file.
Definition Log.cpp:94
void writeln(std::string const &str)
Definition Log.h:113
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:120
~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:26
Logs & logs_
Definition Log.h:25
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:176
bool open(boost::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:126
bool silent_
Definition Log.h:128
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:124
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:125
static constexpr auto kMaximumMessageCharacters
Definition Log.h:194
Logs(beast::Severity level)
Definition Log.cpp:112
File file_
Definition Log.h:127
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:11
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