rippled
Loading...
Searching...
No Matches
Log.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or 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#ifndef RIPPLE_BASICS_LOG_H_INCLUDED
21#define RIPPLE_BASICS_LOG_H_INCLUDED
22
23#include <xrpl/basics/UnorderedContainers.h>
24#include <xrpl/beast/utility/Journal.h>
25
26#include <boost/beast/core/string.hpp>
27#include <boost/filesystem.hpp>
28
29#include <map>
30#include <memory>
31#include <mutex>
32#include <utility>
33
34namespace ripple {
35
36// DEPRECATED use beast::severities::Severity instead
38 lsINVALID = -1, // used to indicate an invalid severity
39 lsTRACE = 0, // Very low-level progress information, details inside
40 // an operation
41 lsDEBUG = 1, // Function-level progress information, operations
42 lsINFO = 2, // Server-level progress information, major operations
43 lsWARNING = 3, // Conditions that warrant human attention, may indicate
44 // a problem
45 lsERROR = 4, // A condition that indicates a problem
46 lsFATAL = 5 // A severe condition that indicates a server problem
47};
48
50class Logs
51{
52private:
54 {
55 private:
58
59 public:
60 Sink(
61 std::string const& partition,
63 Logs& logs);
64
65 Sink(Sink const&) = delete;
66 Sink&
67 operator=(Sink const&) = delete;
68
69 void
71 override;
72
73 void
75 override;
76 };
77
85 class File
86 {
87 public:
92 File();
93
97 ~File() = default;
98
103 bool
104 isOpen() const noexcept;
105
114 bool
115 open(boost::filesystem::path const& path);
116
121 bool
123
125 void
126 close();
127
131 void
132 write(char const* text);
133
137 void
138 writeln(char const* text);
139
142 void
143 write(std::string const& str)
144 {
145 write(str.c_str());
146 }
147
148 void
150 {
151 writeln(str.c_str());
152 }
155 private:
157 boost::filesystem::path m_path;
158 };
159
161 std::map<
164 boost::beast::iless>
168 bool silent_ = false;
169
170public:
172
173 Logs(Logs const&) = delete;
174 Logs&
175 operator=(Logs const&) = delete;
176
177 virtual ~Logs() = default;
178
179 bool
180 open(boost::filesystem::path const& pathToLogFile);
181
183 get(std::string const& name);
184
186 operator[](std::string const& name);
187
189 journal(std::string const& name);
190
192 threshold() const;
193
194 void
196
198 partition_severities() const;
199
200 void
201 write(
203 std::string const& partition,
204 std::string const& text,
205 bool console);
206
208 rotate();
209
215 void
216 silent(bool bSilent)
217 {
218 silent_ = bSilent;
219 }
220
222 makeSink(
223 std::string const& partition,
224 beast::severities::Severity startingLevel);
225
226public:
227 static LogSeverity
229
231 toSeverity(LogSeverity level);
232
233 static std::string
235
236 static LogSeverity
237 fromString(std::string const& s);
238
239private:
240 enum {
241 // Maximum line length for log messages.
242 // If the message exceeds this length it will be truncated with elipses.
243 maximumMessageCharacters = 12 * 1024
244 };
245
246 static void
247 format(
248 std::string& output,
249 std::string const& message,
251 std::string const& partition);
252};
253
254// Wraps a Journal::Stream to skip evaluation of
255// expensive argument lists if the stream is not active.
256#ifndef JLOG
257#define JLOG(x) \
258 if (!x) \
259 { \
260 } \
261 else \
262 x
263#endif
264
265#ifndef CLOG
266#define CLOG(ss) \
267 if (!ss) \
268 ; \
269 else \
270 *ss
271#endif
272
273//------------------------------------------------------------------------------
274// Debug logging:
275
283
290debugLog();
291
292} // namespace ripple
293
294#endif
T c_str(T... args)
Abstraction for the underlying message destination.
Definition: Journal.h:76
A generic endpoint for log messages.
Definition: Journal.h:60
Manages a system file containing logged output.
Definition: Log.h:86
bool isOpen() const noexcept
Determine if a system file is associated with the log.
Definition: Log.cpp:73
bool closeAndReopen()
Close and re-open the system file associated with the log This assists in interoperating with externa...
Definition: Log.cpp:102
std::unique_ptr< std::ofstream > m_stream
Definition: Log.h:156
void writeln(std::string const &str)
Definition: Log.h:149
void close()
Close the system file if it is open.
Definition: Log.cpp:110
void write(char const *text)
write to the log file.
Definition: Log.cpp:116
void writeln(char const *text)
write to the log file and append an end of line marker.
Definition: Log.cpp:123
boost::filesystem::path m_path
Definition: Log.h:157
File()
Construct with no associated system file.
Definition: Log.cpp:68
~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:50
Sink & operator=(Sink const &)=delete
std::string partition_
Definition: Log.h:57
Logs & logs_
Definition: Log.h:56
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:59
Sink(Sink const &)=delete
Manages partitions for logging.
Definition: Log.h:51
void silent(bool bSilent)
Set flag to write logs to stderr (false) or not (true).
Definition: Log.h:216
beast::Journal::Sink & get(std::string const &name)
Definition: Log.cpp:146
beast::severities::Severity thresh_
Definition: Log.h:166
static LogSeverity fromString(std::string const &s)
Definition: Log.cpp:302
Logs(Logs const &)=delete
void write(beast::severities::Severity level, std::string const &partition, std::string const &text, bool console)
Definition: Log.cpp:192
Logs & operator=(Logs const &)=delete
std::map< std::string, std::unique_ptr< beast::Journal::Sink >, boost::beast::iless > sinks_
Definition: Log.h:165
File file_
Definition: Log.h:167
virtual ~Logs()=default
beast::severities::Severity threshold() const
Definition: Log.cpp:166
std::mutex mutex_
Definition: Log.h:160
static std::string toString(LogSeverity s)
Definition: Log.cpp:279
beast::Journal journal(std::string const &name)
Definition: Log.cpp:160
virtual std::unique_ptr< beast::Journal::Sink > makeSink(std::string const &partition, beast::severities::Severity startingLevel)
Definition: Log.cpp:220
std::string rotate()
Definition: Log.cpp:210
static beast::severities::Severity toSeverity(LogSeverity level)
Definition: Log.cpp:253
static void format(std::string &output, std::string const &message, beast::severities::Severity severity, std::string const &partition)
Definition: Log.cpp:327
std::vector< std::pair< std::string, std::string > > partition_severities() const
Definition: Log.cpp:181
bool silent_
Definition: Log.h:168
beast::Journal::Sink & operator[](std::string const &name)
Definition: Log.cpp:154
@ maximumMessageCharacters
Definition: Log.h:243
static LogSeverity fromSeverity(beast::severities::Severity level)
Definition: Log.cpp:226
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:32
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
@ open
We haven't closed our ledger yet, but others might have.
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:468
std::unique_ptr< beast::Journal::Sink > setDebugLogSink(std::unique_ptr< beast::Journal::Sink > sink)
Set the sink for the debug journal.
Definition: Log.cpp:462
LogSeverity
Definition: Log.h:37
@ lsDEBUG
Definition: Log.h:41
@ lsINFO
Definition: Log.h:42
@ lsERROR
Definition: Log.h:45
@ lsWARNING
Definition: Log.h:43
@ lsTRACE
Definition: Log.h:39
@ lsINVALID
Definition: Log.h:38
@ lsFATAL
Definition: Log.h:46
STL namespace.