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