diff options
author | Andreas Baumann <abaumann@yahoo.com> | 2012-08-23 11:35:32 +0200 |
---|---|---|
committer | Andreas Baumann <abaumann@yahoo.com> | 2012-08-23 11:35:32 +0200 |
commit | 271c652cddfab36ac1eba63e68b7e87de09b9da4 (patch) | |
tree | e35bd19981d5537b02b39d9b3574b2c8951d119e /src | |
parent | f4b113727159010ad332f22671e08534c686e322 (diff) | |
download | crawler-271c652cddfab36ac1eba63e68b7e87de09b9da4.tar.gz crawler-271c652cddfab36ac1eba63e68b7e87de09b9da4.tar.bz2 |
added some helper to open log sinks
Diffstat (limited to 'src')
-rw-r--r-- | src/Logger.cpp | 15 | ||||
-rwxr-xr-x | src/Logger.hpp | 84 | ||||
-rwxr-xr-x | src/crawl.cpp | 3 |
3 files changed, 20 insertions, 82 deletions
diff --git a/src/Logger.cpp b/src/Logger.cpp index 01f528e..20c5862 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -1,5 +1,7 @@ #include "Logger.hpp" #include "LogSink.hpp" +#include "ConsoleLogSink.hpp" +#include "FileLogSink.hpp" #include <algorithm> @@ -17,6 +19,9 @@ LogStream::~LogStream( ) ostream &LogStream::get( ) { + // transform the ostringstream to ostream, so the operator + // << functions work a little bit more as expected (e.g. + // for strings/char *) return static_cast<ostream &>( *this ); } @@ -51,6 +56,16 @@ void Logger::log( const LogLevel level, const string &msg ) } } +void Logger::openConsoleLog( const LogLevel level ) +{ + addSink( new ConsoleLogSink( level ) ); +} + +void Logger::openFileLog( const LogLevel level, const string &filename ) +{ + addSink( new FileLogSink( level, filename ) ); +} + string Logger::toString( const LogLevel level ) { static const char* const buf[] = { "NONE", "FATAL", "CRITICAL", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG", "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4" }; diff --git a/src/Logger.hpp b/src/Logger.hpp index 03e66ba..341283a 100755 --- a/src/Logger.hpp +++ b/src/Logger.hpp @@ -53,9 +53,12 @@ class Logger : public Singleton< Logger > ~Logger( ); - static std::string toString( LogLevel level ); + static std::string toString( const LogLevel level ); static LogLevel fromString( const std::string &s ); + void openConsoleLog( const LogLevel level ); + void openFileLog( const LogLevel level, const std::string &filename ); + private: typedef std::list< LogSink * > SinkList; SinkList m_sinks; @@ -65,83 +68,4 @@ SINGLETON_EXTERN template class SINGLETON_EXPORT Singleton< Logger >; #define LOG( level ) LogStream( Logger::instance( ), level ).get( ) -#if 0 - -using namespace std; - -template<typename T> -class Log -{ - public: - Log( ); - - virtual ~Log( ); - - ostringstream& get( LogLevel level = logINFO ); - - static LogLevel& reportingLevel( ); - - protected: - ostringstream os; - - private: -}; - -template <typename T> -Log<T>::Log( ) -{ -} - -template <typename T> -Log<T>::~Log() -{ - os << endl; - T::Output( os.str( ) ); -} - -template <typename T> -LogLevel& Log<T>::reportingLevel( ) -{ - static LogLevel reportingLevel = logDEBUG4; - return reportingLevel; -} - -class Output2FILE -{ - public: - static FILE*& Stream( ); - static void Output( const string& msg ); -}; - -inline FILE*& Output2FILE::Stream( ) -{ - static FILE* f = stderr; - return f; -} - -inline void Output2FILE::Output( const string& msg ) -{ - FILE* f = Stream( ); - if( !f ) return; - fprintf( f, "%s", msg.c_str( ) ); - fflush( f ); -} - -class FILELog : public Log<Output2FILE> -{ -}; - -//typedef Log<Output2FILE> FILELog; - -#ifndef FILELOG_MAX_LEVEL -#define FILELOG_MAX_LEVEL logDEBUG4 -#endif - -#define LOG(level) \ - if( level > FILELOG_MAX_LEVEL ); \ - else if( level > FILELog::reportingLevel( ) || !Output2FILE::Stream( ) ); \ - else FILELog( ).get( level ) - -#endif - #endif diff --git a/src/crawl.cpp b/src/crawl.cpp index dcc8d11..3f33cbb 100755 --- a/src/crawl.cpp +++ b/src/crawl.cpp @@ -10,7 +10,6 @@ #include "ModuleLoader.hpp" #include "Logger.hpp" -#include "ConsoleLogSink.hpp" #include <set> #include <vector> @@ -56,7 +55,7 @@ BOOL WINAPI termHandler( DWORD ctrlType ) int main( void ) { try { - Logger::instance( ).addSink( new ConsoleLogSink( logINFO ) ); + Logger::instance( ).openConsoleLog( logNOTICE ); #ifndef _WIN32 struct sigaction sa; |