rippled
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 <ripple/basics/UnorderedContainers.h>
24 #include <ripple/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 
32 namespace 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 
48 class Logs
49 {
50 private:
51  class Sink : public beast::Journal::Sink
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  // VFALCO NOTE The parameter is unfortunately a boost type because it
109  // can be either wchar or char based depending on platform.
110  // TODO Replace with beast::File
111  //
112  bool
113  open(boost::filesystem::path const& path);
114 
119  bool
120  closeAndReopen();
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
147  writeln(std::string const& str)
148  {
149  writeln(str.c_str());
150  }
153  private:
155  boost::filesystem::path m_path;
156  };
157 
159  std::map<
160  std::string,
162  boost::beast::iless>
166  bool silent_ = false;
167 
168 public:
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 
224 public:
225  static LogSeverity
227 
229  toSeverity(LogSeverity level);
230 
231  static std::string
233 
234  static LogSeverity
235  fromString(std::string const& s);
236 
237 private:
238  enum {
239  // Maximum line length for log messages.
240  // If the message exceeds this length it will be truncated with elipses.
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 //------------------------------------------------------------------------------
264 // Debug logging:
265 
273 
280 debugLog();
281 
282 } // namespace ripple
283 
284 #endif
beast::Journal::Sink
Abstraction for the underlying message destination.
Definition: Journal.h:74
ripple::lsINFO
@ lsINFO
Definition: Log.h:40
ripple::Logs::partition_severities
std::vector< std::pair< std::string, std::string > > partition_severities() const
Definition: Log.cpp:165
std::string
STL class.
ripple::Logs::thresh_
beast::severities::Severity thresh_
Definition: Log.h:164
ripple::Logs::File::close
void close()
Close the system file if it is open.
Definition: Log.cpp:94
utility
ripple::Logs
Manages partitions for logging.
Definition: Log.h:48
ripple::lsTRACE
@ lsTRACE
Definition: Log.h:37
ripple::Logs::File::writeln
void writeln(char const *text)
write to the log file and append an end of line marker.
Definition: Log.cpp:107
ripple::Logs::File::write
void write(char const *text)
write to the log file.
Definition: Log.cpp:100
std::vector
STL class.
ripple::lsFATAL
@ lsFATAL
Definition: Log.h:44
ripple::Logs::File::m_stream
std::unique_ptr< std::ofstream > m_stream
Definition: Log.h:154
ripple::Logs::Sink::partition_
std::string partition_
Definition: Log.h:55
ripple::Logs::File::File
File()
Construct with no associated system file.
Definition: Log.cpp:52
ripple::Logs::operator[]
beast::Journal::Sink & operator[](std::string const &name)
Definition: Log.cpp:138
ripple::Logs::~Logs
virtual ~Logs()=default
ripple::Logs::Sink::write
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition: Log.cpp:42
ripple::Logs::File::closeAndReopen
bool closeAndReopen()
Close and re-open the system file associated with the log This assists in interoperating with externa...
Definition: Log.cpp:86
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
ripple::lsINVALID
@ lsINVALID
Definition: Log.h:36
ripple::Logs::toString
static std::string toString(LogSeverity s)
Definition: Log.cpp:263
ripple::Logs::fromSeverity
static LogSeverity fromSeverity(beast::severities::Severity level)
Definition: Log.cpp:210
ripple::Logs::rotate
std::string rotate()
Definition: Log.cpp:194
ripple::Logs::operator=
Logs & operator=(Logs const &)=delete
ripple::Logs::File::write
void write(std::string const &str)
Write to the log file using std::string.
Definition: Log.h:141
std::string::c_str
T c_str(T... args)
ripple::setDebugLogSink
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
beast::Journal::Sink::Sink
Sink()=delete
ripple::Logs::File
Manages a system file containing logged output.
Definition: Log.h:79
ripple::Logs::Sink
Definition: Log.h:51
ripple::Logs::File::isOpen
bool isOpen() const noexcept
Determine if a system file is associated with the log.
Definition: Log.cpp:57
ripple::Logs::File::open
bool open(boost::filesystem::path const &path)
Associate a system file with the log.
Definition: Log.cpp:63
ripple::Logs::fromString
static LogSeverity fromString(std::string const &s)
Definition: Log.cpp:286
ripple::Logs::File::writeln
void writeln(std::string const &str)
Definition: Log.h:147
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
map
memory
ripple::Logs::Logs
Logs(beast::severities::Severity level)
Definition: Log.cpp:118
ripple::Logs::File::~File
~File()=default
Destroy the object.
ripple::Logs::file_
File file_
Definition: Log.h:165
ripple::Logs::silent
void silent(bool bSilent)
Set flag to write logs to stderr (false) or not (true).
Definition: Log.h:214
ripple::Logs::threshold
beast::severities::Severity threshold() const
Definition: Log.cpp:150
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Logs::Sink::operator=
Sink & operator=(Sink const &)=delete
ripple::Logs::journal
beast::Journal journal(std::string const &name)
Definition: Log.cpp:144
ripple::Logs::makeSink
virtual std::unique_ptr< beast::Journal::Sink > makeSink(std::string const &partition, beast::severities::Severity startingLevel)
Definition: Log.cpp:204
ripple::Logs::maximumMessageCharacters
@ maximumMessageCharacters
Definition: Log.h:241
ripple::Logs::silent_
bool silent_
Definition: Log.h:166
ripple::Logs::Sink::logs_
Logs & logs_
Definition: Log.h:54
beast::severities::Severity
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:31
ripple::lsERROR
@ lsERROR
Definition: Log.h:43
ripple::Logs::File::m_path
boost::filesystem::path m_path
Definition: Log.h:155
mutex
ripple::Logs::toSeverity
static beast::severities::Severity toSeverity(LogSeverity level)
Definition: Log.cpp:237
ripple::Logs::sinks_
std::map< std::string, std::unique_ptr< beast::Journal::Sink >, boost::beast::iless > sinks_
Definition: Log.h:163
ripple::Logs::write
void write(beast::severities::Severity level, std::string const &partition, std::string const &text, bool console)
Definition: Log.cpp:176
std::unique_ptr< std::ofstream >
ripple::LogSeverity
LogSeverity
Definition: Log.h:35
ripple::lsWARNING
@ lsWARNING
Definition: Log.h:41
ripple::Logs::mutex_
std::mutex mutex_
Definition: Log.h:158
ripple::Logs::get
beast::Journal::Sink & get(std::string const &name)
Definition: Log.cpp:130
ripple::Logs::format
static void format(std::string &output, std::string const &message, beast::severities::Severity severity, std::string const &partition)
Definition: Log.cpp:311
ripple::lsDEBUG
@ lsDEBUG
Definition: Log.h:39
ripple::Logs::open
bool open(boost::filesystem::path const &pathToLogFile)
Definition: Log.cpp:124