OpenWalnut  1.5.0dev
WLogger.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WLOGGER_H
26 #define WLOGGER_H
27 
28 #include <memory>
29 #include <ostream>
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 
34 #include <boost/signals2/signal.hpp>
35 
36 #include "WLogEntry.h"
37 #include "WLogStream.h"
38 #include "WSharedSequenceContainer.h"
39 #include "WStringUtils.h"
40 
41 
42 /**
43  * This class defines the interface for adding logs and managing several output streams for them. The actual log entry is in \ref WLogEntry and
44  * the output is done in \ref WLogStream.
45  */
46 class WLogger // NOLINT
47 {
48 public:
49  /**
50  * Create the first and only instance of the logger as it is a singleton.
51  *
52  * \param output the output stream to use
53  * \param level the default log level
54  */
55  static void startup( std::ostream& output = std::cout, LogLevel level = LL_DEBUG ); // NOLINT - we need this non-const ref here
56 
57  /**
58  * Destructor.
59  */
60  virtual ~WLogger();
61 
62  /**
63  * Returns pointer to the currently running logger instance.
64  *
65  * \return pointer to logger instance.
66  */
67  static WLogger* getLogger();
68 
69  /**
70  * Adds a new stream to the logger. This is useful to register file streams or uncolored GUI based outputs.
71  * \note It is not intended to allow getting streams or modifying them except you are the owner/creator.
72  *
73  * \param s the stream to add.
74  */
76 
77  /**
78  * Remove the given stream.
79  *
80  * \param s the stream to remove
81  */
83 
84  /**
85  * Set the default format used for log entries.
86  *
87  * \param format the format string. See WLogEntry for details.
88  */
89  void setDefaultFormat( std::string format );
90 
91  /**
92  * Set the default log-level used for log entries in default console-output
93  *
94  * \param level the log-level
95  */
96  void setDefaultLogLevel( const LogLevel& level );
97 
98  /**
99  * Gets the default format used for log entries. This actually returns the format of the first log stream.
100  *
101  * \return format string. See WLogEntry for details.
102  */
103  std::string getDefaultFormat();
104 
105  /**
106  * Appends a log message to the logging queue.
107  * \param message the log entry
108  * \param source indicates where this entry comes from
109  * \param level The logging level of the current message
110  */
111  void addLogMessage( std::string message, std::string source = "", LogLevel level = LL_DEBUG );
112 
113  /**
114  * Types of signals supported by the logger
115  */
116  typedef enum
117  {
118  AddLog = 0 //!< for added logs
119  }
120  LogEvent;
121 
122  /**
123  * The type for all callbacks which get a log entry as parameter.
124  */
125  typedef boost::function< void ( WLogEntry& ) > LogEntryCallback;
126 
127  /**
128  * Subscribe to the specified signal.
129  *
130  * \note If you want to listen to incoming log entries, you can also utilize the WLogStream class.
131  *
132  * \param event the kind of signal the callback should be used for.
133  * \param callback the callback.
134  *
135  * \return the connection object. Disconnect it explicitly!
136  */
137  boost::signals2::connection subscribeSignal( LogEvent event, LogEntryCallback callback );
138 
139 protected:
140 private:
141  /**
142  * Constructor. The logger is created using the static method startup.
143  *
144  * \param output the stream where to print log messages to
145  * \param level logging level, i.e. verboseness
146  */
147  WLogger( std::ostream& output, LogLevel level ); // NOLINT - we need this non-const ref here
148 
149  /**
150  * We do not want a copy constructor, so we define it private.
151  */
152  WLogger( const WLogger& );
153 
154  /**
155  * The output stream list type.
156  */
158 
159  /**
160  * The list of outputs to print the messages to.
161  */
163 
164  /**
165  * Signal called whenever a new log message arrives.
166  */
167  boost::signals2::signal< void( WLogEntry& ) > m_addLogSignal;
168 };
169 
170 /**
171  * This namespace collects several convenient access points such as wlog::err
172  * for logging with streams to our WLogger.
173  */
174 namespace wlog
175 {
176  /**
177  * Resource class for streamed logging.
178  */
179  class WStreamedLogger // NOLINT
180  {
181  public:
182  /**
183  * Creates new streamed logger instance. Logging is deferred until
184  * destruction of this instance.
185  *
186  * \param source Source from which the log message originates
187  * \param level The LogLevel of the message
188  */
189  WStreamedLogger( const std::string& source, LogLevel level );
190 
191  /**
192  * Appends something loggable (to std::string castable) to the log.
193  *
194  * \param loggable Token that should be streamed into the Logger
195  * \return The streamed logger for further use
196  */
197  template< typename T > WStreamedLogger operator<<( const T& loggable );
198 
199  // Doxygen should ignore the TypeDef below which are just an alias for std::endl etc.
200  // \cond Suppress_Doxygen
201  typedef std::basic_ostream< char, std::char_traits< char > > OutStreamType;
202  typedef OutStreamType& ( *StreamManipulatorFunctor )( OutStreamType& );
203  // \endcond
204 
205  /**
206  * This is totally crazy man! Don't get dizzy on that, watch out and
207  * ask a C++ guru next to your side, which is probably named Christian
208  * or have a look on that: http://stackoverflow.com/questions/1134388/stdendl-is-of-unknown-type-when-overloading-operator
209  *
210  * Allow std::endl to be streamed into log messages.
211  *
212  * \param manip Function pointer e.g. std::endl, std::flush, std::ends
213  * \return The streamed logger for further use
214  */
215  WStreamedLogger operator<<( StreamManipulatorFunctor manip );
216 
217  protected:
218  private:
219  /**
220  * Actually implementing the streaming functionality.
221  */
222  class Buffer
223  {
224  public: // NOLINT inner classes may have also lables
225  /**
226  * Constructs a new stream for logging.
227  *
228  * \param source String identifying the source of the message
229  * \param level LogLevel of the message
230  */
231  Buffer( const std::string& source, LogLevel level );
232 
233  /**
234  * Commits the logging expression to our WLogger
235  */
236  virtual ~Buffer();
237 
238  std::ostringstream m_logString; //!< queuing up parts of the log message
239  LogLevel m_level; //!< Default logging level for this stream
240  std::string m_source; //!< The source of the logging message
241  };
242 
243  /**
244  * Forbid assignment
245  *
246  * \param rhs The instance which SHOULD be copied over
247  * \return A reference to the variable for which assignment was INTENDED.
248  */
249  WStreamedLogger& operator=( const WStreamedLogger& rhs ) = delete;
250 
251  std::shared_ptr< Buffer > m_buffer; //!< Collects the message parts.
252  };
253 
254  inline WStreamedLogger::WStreamedLogger( const std::string& source, LogLevel level )
255  : m_buffer( new Buffer( source, level ) )
256  {
257  }
258 
259  template< typename T > inline WStreamedLogger WStreamedLogger::operator<<( const T& loggable )
260  {
261  using string_utils::operator<<; // in case we want to log arrays or vectors
262  m_buffer->m_logString << loggable;
263  return *this;
264  }
265 
266  inline WStreamedLogger WStreamedLogger::operator<<( StreamManipulatorFunctor manip )
267  {
268  manip( m_buffer->m_logString );
269  return *this;
270  }
271 
272  inline WStreamedLogger::Buffer::Buffer( const std::string& source, LogLevel level )
273  : m_logString(),
274  m_level( level ),
275  m_source( source )
276  {
277  }
278 
279  /**
280  * Convenient function for logging messages to our WLogger but not for
281  * public use outside of this module.
282  *
283  * \param source Indicate the source where this log message origins.
284  * \param level The LogLevel of this message
285  * \return The logger created using the functions parameters
286  */
287  inline WStreamedLogger _wlog( const std::string& source, LogLevel level )
288  {
289  return WStreamedLogger( source, level );
290  }
291 
292  /**
293  * Logging an error message.
294  *
295  * \param source Indicate the source where this log message origins.
296  * \return The logger with the error message.
297  */
298  inline WStreamedLogger error( const std::string& source )
299  {
300  return _wlog( source, LL_ERROR );
301  }
302 
303  /**
304  * Logging a warning message.
305  *
306  * \param source Indicate the source where this log message origins.
307  * \return The logger with the warning message.
308  */
309  inline WStreamedLogger warn( const std::string& source )
310  {
311  return _wlog( source, LL_WARNING );
312  }
313 
314  /**
315  * Logging an information message.
316  *
317  * \param source Indicate the source where this log message origins.
318  * \return The logger with the warning message.
319  */
320  inline WStreamedLogger info( const std::string& source )
321  {
322  return _wlog( source, LL_INFO );
323  }
324 
325  /**
326  * Logging a debug message.
327  *
328  * \param source Indicate the source where this log message origins.
329  * \return The logger with the debug message.
330  */
331  inline WStreamedLogger debug( const std::string& source )
332  {
333  return _wlog( source, LL_DEBUG );
334  }
335 } // end of namespace log
336 
337 #endif // WLOGGER_H
Represents a simple log message with some attributes.
Definition: WLogEntry.h:57
std::shared_ptr< WLogStream > SharedPtr
shared pointer type
Definition: WLogStream.h:41
This class defines the interface for adding logs and managing several output streams for them.
Definition: WLogger.h:47
boost::function< void(WLogEntry &) > LogEntryCallback
The type for all callbacks which get a log entry as parameter.
Definition: WLogger.h:125
virtual ~WLogger()
Destructor.
Definition: WLogger.cpp:60
static void startup(std::ostream &output=std::cout, LogLevel level=LL_DEBUG)
Create the first and only instance of the logger as it is a singleton.
Definition: WLogger.cpp:41
boost::signals2::signal< void(WLogEntry &) > m_addLogSignal
Signal called whenever a new log message arrives.
Definition: WLogger.h:167
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
Definition: WLogger.cpp:84
void removeStream(WLogStream::SharedPtr s)
Remove the given stream.
Definition: WLogger.cpp:121
void setDefaultLogLevel(const LogLevel &level)
Set the default log-level used for log entries in default console-output.
Definition: WLogger.cpp:106
WSharedSequenceContainer< std::vector< WLogStream::SharedPtr > > Outputs
The output stream list type.
Definition: WLogger.h:157
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
Definition: WLogger.cpp:64
void setDefaultFormat(std::string format)
Set the default format used for log entries.
Definition: WLogger.cpp:101
Outputs m_outputs
The list of outputs to print the messages to.
Definition: WLogger.h:162
WLogger(const WLogger &)
We do not want a copy constructor, so we define it private.
LogEvent
Types of signals supported by the logger.
Definition: WLogger.h:117
@ AddLog
for added logs
Definition: WLogger.h:118
void addStream(WLogStream::SharedPtr s)
Adds a new stream to the logger.
Definition: WLogger.cpp:116
std::string getDefaultFormat()
Gets the default format used for log entries.
Definition: WLogger.cpp:111
WLogger(std::ostream &output, LogLevel level)
Constructor.
Definition: WLogger.cpp:49
boost::signals2::connection subscribeSignal(LogEvent event, LogEntryCallback callback)
Subscribe to the specified signal.
Definition: WLogger.cpp:73
This class provides a common interface for thread-safe access to sequence containers (list,...
Actually implementing the streaming functionality.
Definition: WLogger.h:223
std::string m_source
The source of the logging message.
Definition: WLogger.h:240
LogLevel m_level
Default logging level for this stream.
Definition: WLogger.h:239
virtual ~Buffer()
Commits the logging expression to our WLogger.
Definition: WLogger.cpp:126
Buffer(const std::string &source, LogLevel level)
Constructs a new stream for logging.
Definition: WLogger.h:272
std::ostringstream m_logString
queuing up parts of the log message
Definition: WLogger.h:238
Resource class for streamed logging.
Definition: WLogger.h:180
WStreamedLogger(const std::string &source, LogLevel level)
Creates new streamed logger instance.
Definition: WLogger.h:254
WStreamedLogger operator<<(const T &loggable)
Appends something loggable (to std::string castable) to the log.
Definition: WLogger.h:259
std::shared_ptr< Buffer > m_buffer
Collects the message parts.
Definition: WLogger.h:251
WStreamedLogger & operator=(const WStreamedLogger &rhs)=delete
Forbid assignment.
This namespace collects several convenient access points such as wlog::err for logging with streams t...
Definition: WLogger.h:175
WStreamedLogger debug(const std::string &source)
Logging a debug message.
Definition: WLogger.h:331
WStreamedLogger _wlog(const std::string &source, LogLevel level)
Convenient function for logging messages to our WLogger but not for public use outside of this module...
Definition: WLogger.h:287
WStreamedLogger warn(const std::string &source)
Logging a warning message.
Definition: WLogger.h:309
WStreamedLogger info(const std::string &source)
Logging an information message.
Definition: WLogger.h:320
WStreamedLogger error(const std::string &source)
Logging an error message.
Definition: WLogger.h:298