OpenWalnut
1.4.0
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #include <ostream> 00026 #include <string> 00027 00028 #include <boost/date_time/posix_time/posix_time.hpp> 00029 #include <boost/filesystem/fstream.hpp> 00030 00031 #include "exceptions/WSignalSubscriptionInvalid.h" 00032 #include "exceptions/WPreconditionNotMet.h" 00033 00034 #include "WLogger.h" 00035 00036 /** 00037 * Used for program wide access to the logger. 00038 */ 00039 WLogger* logger = NULL; 00040 00041 void WLogger::startup( std::ostream& output, LogLevel level ) // NOLINT - we need this non-const ref here 00042 { 00043 if( !logger ) 00044 { 00045 logger = new WLogger( output, level ); 00046 } 00047 } 00048 00049 WLogger::WLogger( std::ostream& output, LogLevel level ): // NOLINT - we need this non-const ref here 00050 m_outputs() 00051 { 00052 m_outputs.push_back( WLogStream::SharedPtr( new WLogStream( output, level ) ) ); 00053 00054 addLogMessage( "Initalizing Logger", "Logger", LL_INFO ); 00055 addLogMessage( "===============================================================================", "Logger", LL_INFO ); 00056 addLogMessage( "= Starting Log Session =", "Logger", LL_INFO ); 00057 addLogMessage( "===============================================================================", "Logger", LL_INFO ); 00058 } 00059 00060 WLogger::~WLogger() 00061 { 00062 } 00063 00064 WLogger* WLogger::getLogger() 00065 { 00066 if( !logger ) 00067 { 00068 throw new WPreconditionNotMet( std::string( "Logger not yet initialized." ) ); 00069 } 00070 return logger; 00071 } 00072 00073 boost::signals2::connection WLogger::subscribeSignal( LogEvent event, LogEntryCallback callback ) 00074 { 00075 switch( event ) // subscription 00076 { 00077 case AddLog: 00078 return m_addLogSignal.connect( callback ); 00079 default: 00080 throw new WSignalSubscriptionInvalid( std::string( "Signal could not be subscribed. The event is not compatible with the callback." ) ); 00081 } 00082 } 00083 00084 void WLogger::addLogMessage( std::string message, std::string source, LogLevel level ) 00085 { 00086 boost::posix_time::ptime t( boost::posix_time::second_clock::local_time() ); 00087 std::string timeString( to_simple_string( t ) ); 00088 WLogEntry entry( timeString, message, level, source ); 00089 00090 // signal to all interested 00091 m_addLogSignal( entry ); 00092 00093 // output 00094 Outputs::ReadTicket r = m_outputs.getReadTicket(); 00095 for( Outputs::ConstIterator i = r->get().begin(); i != r->get().end(); ++i ) 00096 { 00097 ( *i )->printEntry( entry ); 00098 } 00099 } 00100 00101 void WLogger::setDefaultFormat( std::string format ) 00102 { 00103 m_outputs[0]->setFormat( format ); 00104 } 00105 00106 void WLogger::setDefaultLogLevel( const LogLevel& level ) 00107 { 00108 m_outputs[0]->setLogLevel( level ); 00109 } 00110 00111 std::string WLogger::getDefaultFormat() 00112 { 00113 return m_outputs[0]->getFormat(); 00114 } 00115 00116 void WLogger::addStream( WLogStream::SharedPtr s ) 00117 { 00118 m_outputs.push_back( s ); 00119 } 00120 00121 void WLogger::removeStream( WLogStream::SharedPtr s ) 00122 { 00123 m_outputs.remove( s ); 00124 } 00125 00126 wlog::WStreamedLogger::Buffer::~Buffer() 00127 { 00128 WLogger::getLogger()->addLogMessage( m_logString.str(), m_source, m_level ); 00129 }