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
79 class File
80 {
81 public:
86 File();
87
91 ~File() = default;
92
97 bool
98 isOpen() const noexcept;
99
108 bool
109 open(boost::filesystem::path const& path);
110
115 bool
117
119 void
120 close();
121
125 void
126 write(char const* text);
127
131 void
132 writeln(char const* text);
133
136 void
137 write(std::string const& str)
138 {
139 write(str.c_str());
140 }
141
142 void
144 {
145 writeln(str.c_str());
146 }
149 private:
151 boost::filesystem::path m_path;
152 };
153
155 std::map<
158 boost::beast::iless>
162 bool silent_ = false;
163
164public:
166
167 Logs(Logs const&) = delete;
168 Logs&
169 operator=(Logs const&) = delete;
170
171 virtual ~Logs() = default;
172
173 bool
174 open(boost::filesystem::path const& pathToLogFile);
175
177 get(std::string const& name);
178
180 operator[](std::string const& name);
181
183 journal(std::string const& name);
184
186 threshold() const;
187
188 void
190
192 partition_severities() const;
193
194 void
195 write(
197 std::string const& partition,
198 std::string const& text,
199 bool console);
200
202 rotate();
203
209 void
210 silent(bool bSilent)
211 {
212 silent_ = bSilent;
213 }
214
216 makeSink(
217 std::string const& partition,
218 beast::severities::Severity startingLevel);
219
220public:
221 static LogSeverity
223
225 toSeverity(LogSeverity level);
226
227 static std::string
229
230 static LogSeverity
231 fromString(std::string const& s);
232
233private:
234 enum {
235 // Maximum line length for log messages.
236 // If the message exceeds this length it will be truncated with elipses.
237 maximumMessageCharacters = 12 * 1024
238 };
239
240 static void
241 format(
242 std::string& output,
243 std::string const& message,
245 std::string const& partition);
246};
247
248// Wraps a Journal::Stream to skip evaluation of
249// expensive argument lists if the stream is not active.
250#ifndef JLOG
251#define JLOG(x) \
252 if (!x) \
253 { \
254 } \
255 else \
256 x
257#endif
258
259//------------------------------------------------------------------------------
260// Debug logging:
261
269
276debugLog();
277
278} // namespace ripple
279
280#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:80
bool isOpen() const noexcept
Determine if a system file is associated with the log.
Definition: Log.cpp:57
bool closeAndReopen()
Close and re-open the system file associated with the log This assists in interoperating with externa...
Definition: Log.cpp:86
std::unique_ptr< std::ofstream > m_stream
Definition: Log.h:150
void writeln(std::string const &str)
Definition: Log.h:143
void close()
Close the system file if it is open.
Definition: Log.cpp:94
void write(char const *text)
write to the log file.
Definition: Log.cpp:100
void writeln(char const *text)
write to the log file and append an end of line marker.
Definition: Log.cpp:107
boost::filesystem::path m_path
Definition: Log.h:151
File()
Construct with no associated system file.
Definition: Log.cpp:52
~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
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:210
beast::Journal::Sink & get(std::string const &name)
Definition: Log.cpp:130
beast::severities::Severity thresh_
Definition: Log.h:160
static LogSeverity fromString(std::string const &s)
Definition: Log.cpp:286
Logs(Logs const &)=delete
void write(beast::severities::Severity level, std::string const &partition, std::string const &text, bool console)
Definition: Log.cpp:176
Logs & operator=(Logs const &)=delete
std::map< std::string, std::unique_ptr< beast::Journal::Sink >, boost::beast::iless > sinks_
Definition: Log.h:159
File file_
Definition: Log.h:161
virtual ~Logs()=default
beast::severities::Severity threshold() const
Definition: Log.cpp:150
std::mutex mutex_
Definition: Log.h:154
static std::string toString(LogSeverity s)
Definition: Log.cpp:263
beast::Journal journal(std::string const &name)
Definition: Log.cpp:144
virtual std::unique_ptr< beast::Journal::Sink > makeSink(std::string const &partition, beast::severities::Severity startingLevel)
Definition: Log.cpp:204
std::string rotate()
Definition: Log.cpp:194
static beast::severities::Severity toSeverity(LogSeverity level)
Definition: Log.cpp:237
static void format(std::string &output, std::string const &message, beast::severities::Severity severity, std::string const &partition)
Definition: Log.cpp:311
std::vector< std::pair< std::string, std::string > > partition_severities() const
Definition: Log.cpp:165
bool silent_
Definition: Log.h:162
beast::Journal::Sink & operator[](std::string const &name)
Definition: Log.cpp:138
@ maximumMessageCharacters
Definition: Log.h:237
static LogSeverity fromSeverity(beast::severities::Severity level)
Definition: Log.cpp:210
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:452
std::unique_ptr< beast::Journal::Sink > setDebugLogSink(std::unique_ptr< beast::Journal::Sink > sink)
Set the sink for the debug journal.
Definition: Log.cpp:446
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.