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#include <boost/beast/core/string.hpp>
26#include <boost/filesystem.hpp>
27#include <map>
28#include <memory>
29#include <mutex>
30#include <utility>
31
32namespace ripple {
33
34// DEPRECATED use beast::severities::Severity instead
36 lsINVALID = -1, // used to indicate an invalid severity
37 lsTRACE = 0, // Very low-level progress information, details inside
38 // an operation
39 lsDEBUG = 1, // Function-level progress information, operations
40 lsINFO = 2, // Server-level progress information, major operations
41 lsWARNING = 3, // Conditions that warrant human attention, may indicate
42 // a problem
43 lsERROR = 4, // A condition that indicates a problem
44 lsFATAL = 5 // A severe condition that indicates a server problem
45};
46
48class Logs
49{
50private:
52 {
53 private:
56
57 public:
58 Sink(
59 std::string const& partition,
61 Logs& logs);
62
63 Sink(Sink const&) = delete;
64 Sink&
65 operator=(Sink const&) = delete;
66
67 void
69 override;
70
71 void
73 override;
74 };
75
83 class File
84 {
85 public:
90 File();
91
95 ~File() = default;
96
101 bool
102 isOpen() const noexcept;
103
112 bool
113 open(boost::filesystem::path const& path);
114
119 bool
121
123 void
124 close();
125
129 void
130 write(char const* text);
131
135 void
136 writeln(char const* text);
137
140 void
141 write(std::string const& str)
142 {
143 write(str.c_str());
144 }
145
146 void
148 {
149 writeln(str.c_str());
150 }
153 private:
155 boost::filesystem::path m_path;
156 };
157
159 std::map<
162 boost::beast::iless>
166 bool silent_ = false;
167
168public:
170
171 Logs(Logs const&) = delete;
172 Logs&
173 operator=(Logs const&) = delete;
174
175 virtual ~Logs() = default;
176
177 bool
178 open(boost::filesystem::path const& pathToLogFile);
179
181 get(std::string const& name);
182
184 operator[](std::string const& name);
185
187 journal(std::string const& name);
188
190 threshold() const;
191
192 void
194
196 partition_severities() const;
197
198 void
199 write(
201 std::string const& partition,
202 std::string const& text,
203 bool console);
204
206 rotate();
207
213 void
214 silent(bool bSilent)
215 {
216 silent_ = bSilent;
217 }
218
220 makeSink(
221 std::string const& partition,
222 beast::severities::Severity startingLevel);
223
224public:
225 static LogSeverity
227
229 toSeverity(LogSeverity level);
230
231 static std::string
233
234 static LogSeverity
235 fromString(std::string const& s);
236
237private:
238 enum {
239 // Maximum line length for log messages.
240 // If the message exceeds this length it will be truncated with elipses.
241 maximumMessageCharacters = 12 * 1024
242 };
243
244 static void
245 format(
246 std::string& output,
247 std::string const& message,
249 std::string const& partition);
250};
251
252// Wraps a Journal::Stream to skip evaluation of
253// expensive argument lists if the stream is not active.
254#ifndef JLOG
255#define JLOG(x) \
256 if (!x) \
257 { \
258 } \
259 else \
260 x
261#endif
262
263#ifndef CLOG
264#define CLOG(ss) \
265 if (!ss) \
266 ; \
267 else \
268 *ss
269#endif
270
271//------------------------------------------------------------------------------
272// Debug logging:
273
281
288debugLog();
289
290} // namespace ripple
291
292#endif
T c_str(T... args)
Abstraction for the underlying message destination.
Definition: Journal.h:75
A generic endpoint for log messages.
Definition: Journal.h:59
Manages a system file containing logged output.
Definition: Log.h:84
bool isOpen() const noexcept
Determine if a system file is associated with the log.
Definition: Log.cpp:65
bool closeAndReopen()
Close and re-open the system file associated with the log This assists in interoperating with externa...
Definition: Log.cpp:94
std::unique_ptr< std::ofstream > m_stream
Definition: Log.h:154
void writeln(std::string const &str)
Definition: Log.h:147
void close()
Close the system file if it is open.
Definition: Log.cpp:102
void write(char const *text)
write to the log file.
Definition: Log.cpp:108
void writeln(char const *text)
write to the log file and append an end of line marker.
Definition: Log.cpp:115
boost::filesystem::path m_path
Definition: Log.h:155
File()
Construct with no associated system file.
Definition: Log.cpp:60
~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:42
Sink & operator=(Sink const &)=delete
std::string partition_
Definition: Log.h:55
Logs & logs_
Definition: Log.h:54
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:51
Sink(Sink const &)=delete
Manages partitions for logging.
Definition: Log.h:49
void silent(bool bSilent)
Set flag to write logs to stderr (false) or not (true).
Definition: Log.h:214
beast::Journal::Sink & get(std::string const &name)
Definition: Log.cpp:138
beast::severities::Severity thresh_
Definition: Log.h:164
static LogSeverity fromString(std::string const &s)
Definition: Log.cpp:294
Logs(Logs const &)=delete
void write(beast::severities::Severity level, std::string const &partition, std::string const &text, bool console)
Definition: Log.cpp:184
Logs & operator=(Logs const &)=delete
std::map< std::string, std::unique_ptr< beast::Journal::Sink >, boost::beast::iless > sinks_
Definition: Log.h:163
File file_
Definition: Log.h:165
virtual ~Logs()=default
beast::severities::Severity threshold() const
Definition: Log.cpp:158
std::mutex mutex_
Definition: Log.h:158
static std::string toString(LogSeverity s)
Definition: Log.cpp:271
beast::Journal journal(std::string const &name)
Definition: Log.cpp:152
virtual std::unique_ptr< beast::Journal::Sink > makeSink(std::string const &partition, beast::severities::Severity startingLevel)
Definition: Log.cpp:212
std::string rotate()
Definition: Log.cpp:202
static beast::severities::Severity toSeverity(LogSeverity level)
Definition: Log.cpp:245
static void format(std::string &output, std::string const &message, beast::severities::Severity severity, std::string const &partition)
Definition: Log.cpp:319
std::vector< std::pair< std::string, std::string > > partition_severities() const
Definition: Log.cpp:173
bool silent_
Definition: Log.h:166
beast::Journal::Sink & operator[](std::string const &name)
Definition: Log.cpp:146
@ maximumMessageCharacters
Definition: Log.h:241
static LogSeverity fromSeverity(beast::severities::Severity level)
Definition: Log.cpp:218
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:31
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
@ open
We haven't closed our ledger yet, but others might have.
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:460
std::unique_ptr< beast::Journal::Sink > setDebugLogSink(std::unique_ptr< beast::Journal::Sink > sink)
Set the sink for the debug journal.
Definition: Log.cpp:454
LogSeverity
Definition: Log.h:35
@ lsDEBUG
Definition: Log.h:39
@ lsINFO
Definition: Log.h:40
@ lsERROR
Definition: Log.h:43
@ lsWARNING
Definition: Log.h:41
@ lsTRACE
Definition: Log.h:37
@ lsINVALID
Definition: Log.h:36
@ lsFATAL
Definition: Log.h:44
STL namespace.