rippled
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 <utility>
14
15namespace xrpl {
16
17// DEPRECATED use beast::severities::Severity instead
19 lsINVALID = -1, // used to indicate an invalid severity
20 lsTRACE = 0, // Very low-level progress information, details inside
21 // an operation
22 lsDEBUG = 1, // Function-level progress information, operations
23 lsINFO = 2, // Server-level progress information, major operations
24 lsWARNING = 3, // Conditions that warrant human attention, may indicate
25 // a problem
26 lsERROR = 4, // A condition that indicates a problem
27 lsFATAL = 5 // A severe condition that indicates a server problem
28};
29
31class Logs
32{
33private:
35 {
36 private:
39
40 public:
41 Sink(std::string const& partition, beast::severities::Severity thresh, Logs& logs);
42
43 Sink(Sink const&) = delete;
44 Sink&
45 operator=(Sink const&) = delete;
46
47 void
48 write(beast::severities::Severity level, std::string const& text) override;
49
50 void
51 writeAlways(beast::severities::Severity level, std::string const& text) override;
52 };
53
61 class File
62 {
63 public:
68 File();
69
73 ~File() = default;
74
79 bool
80 isOpen() const noexcept;
81
90 bool
91 open(boost::filesystem::path const& path);
92
97 bool
99
101 void
102 close();
103
107 void
108 write(char const* text);
109
113 void
114 writeln(char const* text);
115
118 void
119 write(std::string const& str)
120 {
121 write(str.c_str());
122 }
123
124 void
126 {
127 writeln(str.c_str());
128 }
131 private:
133 boost::filesystem::path m_path;
134 };
135
140 bool silent_ = false;
141
142public:
144
145 Logs(Logs const&) = delete;
146 Logs&
147 operator=(Logs const&) = delete;
148
149 virtual ~Logs() = default;
150
151 bool
152 open(boost::filesystem::path const& pathToLogFile);
153
155 get(std::string const& name);
156
158 operator[](std::string const& name);
159
161 journal(std::string const& name);
162
164 threshold() const;
165
166 void
168
170 partition_severities() const;
171
172 void
173 write(beast::severities::Severity level, std::string const& partition, std::string const& text, bool console);
174
176 rotate();
177
183 void
184 silent(bool bSilent)
185 {
186 silent_ = bSilent;
187 }
188
190 makeSink(std::string const& partition, beast::severities::Severity startingLevel);
191
192public:
193 static LogSeverity
195
197 toSeverity(LogSeverity level);
198
199 static std::string
201
202 static LogSeverity
203 fromString(std::string const& s);
204
205private:
206 enum {
207 // Maximum line length for log messages.
208 // If the message exceeds this length it will be truncated with
209 // ellipses.
210 maximumMessageCharacters = 12 * 1024
211 };
212
213 static void
214 format(
215 std::string& output,
216 std::string const& message,
218 std::string const& partition);
219};
220
221// Wraps a Journal::Stream to skip evaluation of
222// expensive argument lists if the stream is not active.
223#ifndef JLOG
224#define JLOG(x) \
225 if (!x) \
226 { \
227 } \
228 else \
229 x
230#endif
231
232#ifndef CLOG
233#define CLOG(ss) \
234 if (!ss) \
235 ; \
236 else \
237 *ss
238#endif
239
240//------------------------------------------------------------------------------
241// Debug logging:
242
250
257debugLog();
258
259} // namespace xrpl
T c_str(T... args)
Abstraction for the underlying message destination.
Definition Journal.h:56
A generic endpoint for log messages.
Definition Journal.h:40
Manages a system file containing logged output.
Definition Log.h:62
void writeln(char const *text)
write to the log file and append an end of line marker.
Definition Log.cpp:98
bool closeAndReopen()
Close and re-open the system file associated with the log This assists in interoperating with externa...
Definition Log.cpp:77
boost::filesystem::path m_path
Definition Log.h:133
void write(char const *text)
write to the log file.
Definition Log.cpp:91
std::unique_ptr< std::ofstream > m_stream
Definition Log.h:132
void writeln(std::string const &str)
Definition Log.h:125
bool isOpen() const noexcept
Determine if a system file is associated with the log.
Definition Log.cpp:49
void close()
Close the system file if it is open.
Definition Log.cpp:85
~File()=default
Destroy the object.
File()
Construct with no associated system file.
Definition Log.cpp:44
Sink & operator=(Sink const &)=delete
Sink(Sink const &)=delete
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:37
std::string partition_
Definition Log.h:38
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition Log.cpp:28
Logs & logs_
Definition Log.h:37
Manages partitions for logging.
Definition Log.h:32
void silent(bool bSilent)
Set flag to write logs to stderr (false) or not (true).
Definition Log.h:184
beast::Journal journal(std::string const &name)
Definition Log.cpp:134
Logs(Logs const &)=delete
beast::severities::Severity threshold() const
Definition Log.cpp:140
beast::severities::Severity thresh_
Definition Log.h:138
void write(beast::severities::Severity level, std::string const &partition, std::string const &text, bool console)
Definition Log.cpp:166
Logs & operator=(Logs const &)=delete
virtual std::unique_ptr< beast::Journal::Sink > makeSink(std::string const &partition, beast::severities::Severity startingLevel)
Definition Log.cpp:190
beast::Journal::Sink & get(std::string const &name)
Definition Log.cpp:120
static LogSeverity fromSeverity(beast::severities::Severity level)
Definition Log.cpp:196
bool silent_
Definition Log.h:140
std::vector< std::pair< std::string, std::string > > partition_severities() const
Definition Log.cpp:155
static LogSeverity fromString(std::string const &s)
Definition Log.cpp:278
@ maximumMessageCharacters
Definition Log.h:210
static void format(std::string &output, std::string const &message, beast::severities::Severity severity, std::string const &partition)
Definition Log.cpp:302
std::mutex mutex_
Definition Log.h:136
virtual ~Logs()=default
std::string rotate()
Definition Log.cpp:180
std::map< std::string, std::unique_ptr< beast::Journal::Sink >, boost::beast::iless > sinks_
Definition Log.h:137
static std::string toString(LogSeverity s)
Definition Log.cpp:253
File file_
Definition Log.h:139
static beast::severities::Severity toSeverity(LogSeverity level)
Definition Log.cpp:225
beast::Journal::Sink & operator[](std::string const &name)
Definition Log.cpp:128
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:12
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:445
std::unique_ptr< beast::Journal::Sink > setDebugLogSink(std::unique_ptr< beast::Journal::Sink > sink)
Set the sink for the debug journal.
Definition Log.cpp:439
LogSeverity
Definition Log.h:18
@ lsERROR
Definition Log.h:26
@ lsWARNING
Definition Log.h:24
@ lsINVALID
Definition Log.h:19
@ lsDEBUG
Definition Log.h:22
@ lsFATAL
Definition Log.h:27
@ lsINFO
Definition Log.h:23
@ lsTRACE
Definition Log.h:20
@ open
We haven't closed our ledger yet, but others might have.