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  bool
109  open(boost::filesystem::path const& path);
110 
115  bool
116  closeAndReopen();
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
143  writeln(std::string const& str)
144  {
145  writeln(str.c_str());
146  }
149  private:
151  boost::filesystem::path m_path;
152  };
153 
155  std::map<
156  std::string,
158  boost::beast::iless>
162  bool silent_ = false;
163 
164 public:
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 
220 public:
221  static LogSeverity
223 
225  toSeverity(LogSeverity level);
226 
227  static std::string
229 
230  static LogSeverity
231  fromString(std::string const& s);
232 
233 private:
234  enum {
235  // Maximum line length for log messages.
236  // If the message exceeds this length it will be truncated with elipses.
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 
276 debugLog();
277 
278 } // namespace ripple
279 
280 #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:160
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:150
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:137
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:143
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:161
ripple::Logs::silent
void silent(bool bSilent)
Set flag to write logs to stderr (false) or not (true).
Definition: Log.h:210
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:237
ripple::Logs::silent_
bool silent_
Definition: Log.h:162
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:151
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:159
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:154
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