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 <fstream>
30#include <map>
31#include <memory>
32#include <mutex>
33#include <utility>
34
35namespace ripple {
36
37// DEPRECATED use beast::severities::Severity instead
39 lsINVALID = -1, // used to indicate an invalid severity
40 lsTRACE = 0, // Very low-level progress information, details inside
41 // an operation
42 lsDEBUG = 1, // Function-level progress information, operations
43 lsINFO = 2, // Server-level progress information, major operations
44 lsWARNING = 3, // Conditions that warrant human attention, may indicate
45 // a problem
46 lsERROR = 4, // A condition that indicates a problem
47 lsFATAL = 5 // A severe condition that indicates a server problem
48};
49
51class Logs
52{
53private:
55 {
56 private:
59
60 public:
61 Sink(
62 std::string const& partition,
64 Logs& logs);
65
66 Sink(Sink const&) = delete;
67 Sink&
68 operator=(Sink const&) = delete;
69
70 void
72 override;
73
74 void
76 override;
77 };
78
86 class File
87 {
88 public:
93 File();
94
98 ~File() = default;
99
104 bool
105 isOpen() const noexcept;
106
115 bool
116 open(boost::filesystem::path const& path);
117
122 bool
124
126 void
127 close();
128
132 void
133 write(char const* text);
134
138 void
139 writeln(char const* text);
140
143 void
144 write(std::string const& str)
145 {
146 write(str.c_str());
147 }
148
149 void
151 {
152 writeln(str.c_str());
153 }
156 private:
158 boost::filesystem::path m_path;
159 };
160
162 std::map<
165 boost::beast::iless>
169 bool silent_ = false;
170
171public:
173
174 Logs(Logs const&) = delete;
175 Logs&
176 operator=(Logs const&) = delete;
177
178 virtual ~Logs() = default;
179
180 bool
181 open(boost::filesystem::path const& pathToLogFile);
182
184 get(std::string const& name);
185
187 operator[](std::string const& name);
188
190 journal(std::string const& name);
191
193 threshold() const;
194
195 void
197
199 partition_severities() const;
200
201 void
202 write(
204 std::string const& partition,
205 std::string const& text,
206 bool console);
207
209 rotate();
210
216 void
217 silent(bool bSilent)
218 {
219 silent_ = bSilent;
220 }
221
223 makeSink(
224 std::string const& partition,
225 beast::severities::Severity startingLevel);
226
227public:
228 static LogSeverity
230
232 toSeverity(LogSeverity level);
233
234 static std::string
236
237 static LogSeverity
238 fromString(std::string const& s);
239
240private:
241 enum {
242 // Maximum line length for log messages.
243 // If the message exceeds this length it will be truncated with elipses.
244 maximumMessageCharacters = 12 * 1024
245 };
246
247 static void
248 format(
249 std::string& output,
250 std::string const& message,
252 std::string const& partition);
253};
254
255// Wraps a Journal::Stream to skip evaluation of
256// expensive argument lists if the stream is not active.
257#ifndef JLOG
258#define JLOG(x) \
259 if (!x) \
260 { \
261 } \
262 else \
263 x
264#endif
265
266#ifndef CLOG
267#define CLOG(ss) \
268 if (!ss) \
269 ; \
270 else \
271 *ss
272#endif
273
274//------------------------------------------------------------------------------
275// Debug logging:
276
284
291debugLog();
292
293} // namespace ripple
294
295#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:87
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:157
void writeln(std::string const &str)
Definition: Log.h:150
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:158
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:58
Logs & logs_
Definition: Log.h:57
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:52
void silent(bool bSilent)
Set flag to write logs to stderr (false) or not (true).
Definition: Log.h:217
beast::Journal::Sink & get(std::string const &name)
Definition: Log.cpp:146
beast::severities::Severity thresh_
Definition: Log.h:167
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:166
File file_
Definition: Log.h:168
virtual ~Logs()=default
beast::severities::Severity threshold() const
Definition: Log.cpp:166
std::mutex mutex_
Definition: Log.h:161
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:169
beast::Journal::Sink & operator[](std::string const &name)
Definition: Log.cpp:154
@ maximumMessageCharacters
Definition: Log.h:244
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:38
@ lsDEBUG
Definition: Log.h:42
@ lsINFO
Definition: Log.h:43
@ lsERROR
Definition: Log.h:46
@ lsWARNING
Definition: Log.h:44
@ lsTRACE
Definition: Log.h:40
@ lsINVALID
Definition: Log.h:39
@ lsFATAL
Definition: Log.h:47
STL namespace.