00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00038
00039 WLogger* logger = NULL;
00040
00041 void WLogger::startup( std::ostream& output, LogLevel level )
00042 {
00043 if( !logger )
00044 {
00045 logger = new WLogger( output, level );
00046 }
00047 }
00048
00049 WLogger::WLogger( std::ostream& output, LogLevel level ):
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 )
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
00091 m_addLogSignal( entry );
00092
00093
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 wlog::WStreamedLogger::Buffer::~Buffer()
00122 {
00123 WLogger::getLogger()->addLogMessage( m_logString.str(), m_source, m_level );
00124 }