OpenWalnut  1.5.0dev
WLogger.cpp
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 #include <ostream>
26 #include <string>
27 
28 #include <boost/date_time/posix_time/posix_time.hpp>
29 #include <boost/filesystem/fstream.hpp>
30 
31 #include "exceptions/WSignalSubscriptionInvalid.h"
32 #include "exceptions/WPreconditionNotMet.h"
33 
34 #include "WLogger.h"
35 
36 /**
37  * Used for program wide access to the logger.
38  */
39 WLogger* logger = NULL;
40 
41 void WLogger::startup( std::ostream& output, LogLevel level ) // NOLINT - we need this non-const ref here
42 {
43  if( !logger )
44  {
45  logger = new WLogger( output, level );
46  }
47 }
48 
49 WLogger::WLogger( std::ostream& output, LogLevel level ): // NOLINT - we need this non-const ref here
50  m_outputs()
51 {
52  m_outputs.push_back( WLogStream::SharedPtr( new WLogStream( output, level ) ) );
53 
54  addLogMessage( "Initalizing Logger", "Logger", LL_INFO );
55  addLogMessage( "===============================================================================", "Logger", LL_INFO );
56  addLogMessage( "= Starting Log Session =", "Logger", LL_INFO );
57  addLogMessage( "===============================================================================", "Logger", LL_INFO );
58 }
59 
61 {
62 }
63 
65 {
66  if( !logger )
67  {
68  throw new WPreconditionNotMet( std::string( "Logger not yet initialized." ) );
69  }
70  return logger;
71 }
72 
73 boost::signals2::connection WLogger::subscribeSignal( LogEvent event, LogEntryCallback callback )
74 {
75  switch( event ) // subscription
76  {
77  case AddLog:
78  return m_addLogSignal.connect( callback );
79  default:
80  throw new WSignalSubscriptionInvalid( std::string( "Signal could not be subscribed. The event is not compatible with the callback." ) );
81  }
82 }
83 
84 void WLogger::addLogMessage( std::string message, std::string source, LogLevel level )
85 {
86  boost::posix_time::ptime t( boost::posix_time::second_clock::local_time() );
87  std::string timeString( to_simple_string( t ) );
88  WLogEntry entry( timeString, message, level, source );
89 
90  // signal to all interested
91  m_addLogSignal( entry );
92 
93  // output
95  for( Outputs::ConstIterator i = r->get().begin(); i != r->get().end(); ++i )
96  {
97  ( *i )->printEntry( entry );
98  }
99 }
100 
101 void WLogger::setDefaultFormat( std::string format )
102 {
103  m_outputs[0]->setFormat( format );
104 }
105 
106 void WLogger::setDefaultLogLevel( const LogLevel& level )
107 {
108  m_outputs[0]->setLogLevel( level );
109 }
110 
112 {
113  return m_outputs[0]->getFormat();
114 }
115 
117 {
118  m_outputs.push_back( s );
119 }
120 
122 {
123  m_outputs.remove( s );
124 }
125 
127 {
129 }
Represents a simple log message with some attributes.
Definition: WLogEntry.h:57
Class implementing a capsule for an output stream and the needed level and format information.
Definition: WLogStream.h:39
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
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
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
An exception that gets thrown when preconditions of a function are not met.
std::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:65
ReadTicket getReadTicket() const
Returns a ticket to get read access to the contained data.
void remove(const typename S::value_type &element)
Searches and removes the specified element.
void push_back(const typename S::value_type &x)
Adds a new element at the end of the container.
S::const_iterator ConstIterator
A typedef for the correct const iterator useful to traverse this sequence container.
Indicates invalid use of subscribeSignal methods.
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
std::ostringstream m_logString
queuing up parts of the log message
Definition: WLogger.h:238