rippled
Loading...
Searching...
No Matches
Log.h
1#ifndef XRPL_BASICS_LOG_H_INCLUDED
2#define XRPL_BASICS_LOG_H_INCLUDED
3
4#include <xrpl/basics/UnorderedContainers.h>
5#include <xrpl/beast/utility/Journal.h>
6
7#include <boost/beast/core/string.hpp>
8#include <boost/filesystem.hpp>
9
10#include <fstream>
11#include <map>
12#include <memory>
13#include <mutex>
14#include <utility>
15
16namespace ripple {
17
18// DEPRECATED use beast::severities::Severity instead
20 lsINVALID = -1, // used to indicate an invalid severity
21 lsTRACE = 0, // Very low-level progress information, details inside
22 // an operation
23 lsDEBUG = 1, // Function-level progress information, operations
24 lsINFO = 2, // Server-level progress information, major operations
25 lsWARNING = 3, // Conditions that warrant human attention, may indicate
26 // a problem
27 lsERROR = 4, // A condition that indicates a problem
28 lsFATAL = 5 // A severe condition that indicates a server problem
29};
30
32class Logs
33{
34private:
36 {
37 private:
40
41 public:
42 Sink(
43 std::string const& partition,
45 Logs& logs);
46
47 Sink(Sink const&) = delete;
48 Sink&
49 operator=(Sink const&) = delete;
50
51 void
53 override;
54
55 void
57 override;
58 };
59
67 class File
68 {
69 public:
74 File();
75
79 ~File() = default;
80
85 bool
86 isOpen() const noexcept;
87
96 bool
97 open(boost::filesystem::path const& path);
98
103 bool
105
107 void
108 close();
109
113 void
114 write(char const* text);
115
119 void
120 writeln(char const* text);
121
124 void
125 write(std::string const& str)
126 {
127 write(str.c_str());
128 }
129
130 void
132 {
133 writeln(str.c_str());
134 }
137 private:
139 boost::filesystem::path m_path;
140 };
141
143 std::map<
146 boost::beast::iless>
150 bool silent_ = false;
151
152public:
154
155 Logs(Logs const&) = delete;
156 Logs&
157 operator=(Logs const&) = delete;
158
159 virtual ~Logs() = default;
160
161 bool
162 open(boost::filesystem::path const& pathToLogFile);
163
165 get(std::string const& name);
166
168 operator[](std::string const& name);
169
171 journal(std::string const& name);
172
174 threshold() const;
175
176 void
178
180 partition_severities() const;
181
182 void
183 write(
185 std::string const& partition,
186 std::string const& text,
187 bool console);
188
190 rotate();
191
197 void
198 silent(bool bSilent)
199 {
200 silent_ = bSilent;
201 }
202
204 makeSink(
205 std::string const& partition,
206 beast::severities::Severity startingLevel);
207
208public:
209 static LogSeverity
211
213 toSeverity(LogSeverity level);
214
215 static std::string
217
218 static LogSeverity
219 fromString(std::string const& s);
220
221private:
222 enum {
223 // Maximum line length for log messages.
224 // If the message exceeds this length it will be truncated with elipses.
225 maximumMessageCharacters = 12 * 1024
226 };
227
228 static void
229 format(
230 std::string& output,
231 std::string const& message,
233 std::string const& partition);
234};
235
236// Wraps a Journal::Stream to skip evaluation of
237// expensive argument lists if the stream is not active.
238#ifndef JLOG
239#define JLOG(x) \
240 if (!x) \
241 { \
242 } \
243 else \
244 x
245#endif
246
247#ifndef CLOG
248#define CLOG(ss) \
249 if (!ss) \
250 ; \
251 else \
252 *ss
253#endif
254
255//------------------------------------------------------------------------------
256// Debug logging:
257
265
272debugLog();
273
274} // namespace ripple
275
276#endif
T c_str(T... args)
Abstraction for the underlying message destination.
Definition Journal.h:57
A generic endpoint for log messages.
Definition Journal.h:41
Manages a system file containing logged output.
Definition Log.h:68
bool isOpen() const noexcept
Determine if a system file is associated with the log.
Definition Log.cpp:54
bool closeAndReopen()
Close and re-open the system file associated with the log This assists in interoperating with externa...
Definition Log.cpp:83
std::unique_ptr< std::ofstream > m_stream
Definition Log.h:138
void writeln(std::string const &str)
Definition Log.h:131
void close()
Close the system file if it is open.
Definition Log.cpp:91
void write(char const *text)
write to the log file.
Definition Log.cpp:97
void writeln(char const *text)
write to the log file and append an end of line marker.
Definition Log.cpp:104
boost::filesystem::path m_path
Definition Log.h:139
File()
Construct with no associated system file.
Definition Log.cpp:49
~File()=default
Destroy the object.
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition Log.cpp:31
Sink & operator=(Sink const &)=delete
std::string partition_
Definition Log.h:39
Logs & logs_
Definition Log.h:38
void writeAlways(beast::severities::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
Definition Log.cpp:40
Sink(Sink const &)=delete
Manages partitions for logging.
Definition Log.h:33
void silent(bool bSilent)
Set flag to write logs to stderr (false) or not (true).
Definition Log.h:198
beast::Journal::Sink & get(std::string const &name)
Definition Log.cpp:127
beast::severities::Severity thresh_
Definition Log.h:148
static LogSeverity fromString(std::string const &s)
Definition Log.cpp:289
Logs(Logs const &)=delete
void write(beast::severities::Severity level, std::string const &partition, std::string const &text, bool console)
Definition Log.cpp:173
Logs & operator=(Logs const &)=delete
std::map< std::string, std::unique_ptr< beast::Journal::Sink >, boost::beast::iless > sinks_
Definition Log.h:147
File file_
Definition Log.h:149
virtual ~Logs()=default
beast::severities::Severity threshold() const
Definition Log.cpp:147
std::mutex mutex_
Definition Log.h:142
static std::string toString(LogSeverity s)
Definition Log.cpp:264
beast::Journal journal(std::string const &name)
Definition Log.cpp:141
virtual std::unique_ptr< beast::Journal::Sink > makeSink(std::string const &partition, beast::severities::Severity startingLevel)
Definition Log.cpp:201
std::string rotate()
Definition Log.cpp:191
static beast::severities::Severity toSeverity(LogSeverity level)
Definition Log.cpp:236
static void format(std::string &output, std::string const &message, beast::severities::Severity severity, std::string const &partition)
Definition Log.cpp:314
std::vector< std::pair< std::string, std::string > > partition_severities() const
Definition Log.cpp:162
bool silent_
Definition Log.h:150
beast::Journal::Sink & operator[](std::string const &name)
Definition Log.cpp:135
@ maximumMessageCharacters
Definition Log.h:225
static LogSeverity fromSeverity(beast::severities::Severity level)
Definition Log.cpp:207
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:13
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ open
We haven't closed our ledger yet, but others might have.
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:457
std::unique_ptr< beast::Journal::Sink > setDebugLogSink(std::unique_ptr< beast::Journal::Sink > sink)
Set the sink for the debug journal.
Definition Log.cpp:451
LogSeverity
Definition Log.h:19
@ lsDEBUG
Definition Log.h:23
@ lsINFO
Definition Log.h:24
@ lsERROR
Definition Log.h:27
@ lsWARNING
Definition Log.h:25
@ lsTRACE
Definition Log.h:21
@ lsINVALID
Definition Log.h:20
@ lsFATAL
Definition Log.h:28
STL namespace.