OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 {
124 }