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 <boost/beast/core/string.hpp>
25 #include <ripple/beast/utility/Journal.h>
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 {
37  lsINVALID = -1, // used to indicate an invalid severity
38  lsTRACE = 0, // Very low-level progress information, details inside
39  // an operation
40  lsDEBUG = 1, // Function-level progress information, operations
41  lsINFO = 2, // Server-level progress information, major operations
42  lsWARNING = 3, // Conditions that warrant human attention, may indicate
43  // a problem
44  lsERROR = 4, // A condition that indicates a problem
45  lsFATAL = 5 // A severe condition that indicates a server problem
46 };
47 
49 class Logs
50 {
51 private:
52  class Sink : public beast::Journal::Sink
53  {
54  private:
57 
58  public:
59  Sink (std::string const& partition,
60  beast::severities::Severity thresh, Logs& logs);
61 
62  Sink (Sink const&) = delete;
63  Sink& operator= (Sink const&) = delete;
64 
65  void
67  std::string const& text) override;
68  };
69 
77  class File
78  {
79  public:
84  File ();
85 
89  ~File () = default;
90 
95  bool isOpen () const noexcept;
96 
105  // VFALCO NOTE The parameter is unfortunately a boost type because it
106  // can be either wchar or char based depending on platform.
107  // TODO Replace with beast::File
108  //
109  bool open (boost::filesystem::path const& path);
110 
115  bool closeAndReopen ();
116 
118  void close ();
119 
123  void write (char const* text);
124 
128  void writeln (char const* text);
129 
132  void write (std::string const& str)
133  {
134  write (str.c_str ());
135  }
136 
137  void writeln (std::string const& str)
138  {
139  writeln (str.c_str ());
140  }
143  private:
145  boost::filesystem::path m_path;
146  };
147 
151  boost::beast::iless> sinks_;
154  bool silent_ = false;
155 
156 public:
158 
159  Logs (Logs const&) = delete;
160  Logs& operator= (Logs const&) = delete;
161 
162  virtual ~Logs() = default;
163 
164  bool
165  open (boost::filesystem::path const& pathToLogFile);
166 
168  get (std::string const& name);
169 
171  operator[] (std::string const& name);
172 
174  journal (std::string const& name);
175 
177  threshold() const;
178 
179  void
181 
183  partition_severities() const;
184 
185  void
186  write (beast::severities::Severity level, std::string const& partition,
187  std::string const& text, bool console);
188 
190  rotate();
191 
197  void
198  silent (bool bSilent)
199  {
200  silent_ = bSilent;
201  }
202 
203  virtual
205  makeSink(std::string const& partition,
206  beast::severities::Severity startingLevel);
207 
208 public:
209  static
212 
213  static
215  toSeverity (LogSeverity level);
216 
217  static
219  toString (LogSeverity s);
220 
221  static
223  fromString (std::string const& s);
224 
225 private:
226  enum
227  {
228  // Maximum line length for log messages.
229  // If the message exceeds this length it will be truncated with elipses.
231  };
232 
233  static
234  void
235  format (std::string& output, std::string const& message,
236  beast::severities::Severity severity, std::string const& partition);
237 };
238 
239 // Wraps a Journal::Stream to skip evaluation of
240 // expensive argument lists if the stream is not active.
241 #ifndef JLOG
242 #define JLOG(x) if (!x) { } else x
243 #endif
244 
245 //------------------------------------------------------------------------------
246 // Debug logging:
247 
256 
263 debugLog();
264 
265 } // ripple
266 
267 #endif
beast::Journal::Sink
Abstraction for the underlying message destination.
Definition: Journal.h:76
ripple::lsINFO
@ lsINFO
Definition: Log.h:41
ripple::Logs::partition_severities
std::vector< std::pair< std::string, std::string > > partition_severities() const
Definition: Log.cpp:161
std::string
STL class.
ripple::Logs::thresh_
beast::severities::Severity thresh_
Definition: Log.h:152
ripple::Logs::File::close
void close()
Close the system file if it is open.
Definition: Log.cpp:91
utility
ripple::Logs
Manages partitions for logging.
Definition: Log.h:49
ripple::lsTRACE
@ lsTRACE
Definition: Log.h:38
ripple::Logs::File::writeln
void writeln(char const *text)
write to the log file and append an end of line marker.
Definition: Log.cpp:102
ripple::Logs::File::write
void write(char const *text)
write to the log file.
Definition: Log.cpp:96
std::vector
STL class.
ripple::lsFATAL
@ lsFATAL
Definition: Log.h:45
ripple::Logs::File::m_stream
std::unique_ptr< std::ofstream > m_stream
Definition: Log.h:144
ripple::Logs::sinks_
std::map< std::string, std::unique_ptr< beast::Journal::Sink >, boost::beast::iless > sinks_
Definition: Log.h:151
ripple::Logs::Sink::partition_
std::string partition_
Definition: Log.h:56
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:134
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:84
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:417
ripple::lsINVALID
@ lsINVALID
Definition: Log.h:37
ripple::Logs::toString
static std::string toString(LogSeverity s)
Definition: Log.cpp:248
ripple::Logs::fromSeverity
static LogSeverity fromSeverity(beast::severities::Severity level)
Definition: Log.cpp:205
ripple::Logs::rotate
std::string rotate()
Definition: Log.cpp:187
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:132
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:410
beast::Journal::Sink::Sink
Sink()=delete
ripple::Logs::File
Manages a system file containing logged output.
Definition: Log.h:77
ripple::Logs::Sink
Definition: Log.h:52
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:62
ripple::Logs::fromString
static LogSeverity fromString(std::string const &s)
Definition: Log.cpp:265
ripple::Logs::File::writeln
void writeln(std::string const &str)
Definition: Log.h:137
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
map
memory
ripple::Logs::Logs
Logs(beast::severities::Severity level)
Definition: Log.cpp:113
ripple::Logs::File::~File
~File()=default
Destroy the object.
ripple::Logs::file_
File file_
Definition: Log.h:153
ripple::Logs::silent
void silent(bool bSilent)
Set flag to write logs to stderr (false) or not (true).
Definition: Log.h:198
ripple::Logs::threshold
beast::severities::Severity threshold() const
Definition: Log.cpp:146
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:140
ripple::Logs::makeSink
virtual std::unique_ptr< beast::Journal::Sink > makeSink(std::string const &partition, beast::severities::Severity startingLevel)
Definition: Log.cpp:197
ripple::Logs::maximumMessageCharacters
@ maximumMessageCharacters
Definition: Log.h:230
ripple::Logs::silent_
bool silent_
Definition: Log.h:154
ripple::Logs::Sink::logs_
Logs & logs_
Definition: Log.h:55
beast::severities::Severity
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:32
ripple::lsERROR
@ lsERROR
Definition: Log.h:44
ripple::Logs::File::m_path
boost::filesystem::path m_path
Definition: Log.h:145
mutex
ripple::Logs::toSeverity
static beast::severities::Severity toSeverity(LogSeverity level)
Definition: Log.cpp:227
ripple::Logs::write
void write(beast::severities::Severity level, std::string const &partition, std::string const &text, bool console)
Definition: Log.cpp:172
std::unique_ptr< std::ofstream >
ripple::LogSeverity
LogSeverity
Definition: Log.h:35
ripple::lsWARNING
@ lsWARNING
Definition: Log.h:42
ripple::Logs::mutex_
std::mutex mutex_
Definition: Log.h:148
ripple::Logs::get
beast::Journal::Sink & get(std::string const &name)
Definition: Log.cpp:125
ripple::Logs::format
static void format(std::string &output, std::string const &message, beast::severities::Severity severity, std::string const &partition)
Definition: Log.cpp:289
ripple::lsDEBUG
@ lsDEBUG
Definition: Log.h:40
ripple::Logs::open
bool open(boost::filesystem::path const &pathToLogFile)
Definition: Log.cpp:119