diff --git a/src/libtomahawk/utils/logger.cpp b/src/libtomahawk/utils/logger.cpp index 99f7a86fb..730b9c28d 100644 --- a/src/libtomahawk/utils/logger.cpp +++ b/src/libtomahawk/utils/logger.cpp @@ -38,22 +38,17 @@ ofstream logfile; namespace Logger { -tLog& -tLog::operator<<( const QVariant& v ) +static void +log( const char *msg, unsigned int debugLevel, bool toDisk = true ) { - QString const s = v.toString(); - if ( !s.isEmpty() ) - log( s.toAscii().data() ); + if ( toDisk ) + { + logfile << QTime::currentTime().toString().toAscii().data() << " [" << QString::number( debugLevel ).toAscii().data() << "]: " << msg << endl; + logfile.flush(); + } - return *this; -} - - -void -tLog::log( const char *msg ) -{ - logfile << QTime::currentTime().toString().toAscii().data() << " " << msg << endl; - logfile.flush(); + cout << msg << endl; + cout.flush(); } @@ -66,27 +61,23 @@ TomahawkLogHandler( QtMsgType type, const char *msg ) switch( type ) { case QtDebugMsg: - // Disable debug logging in release builds: #ifndef QT_NO_DEBUG - tLog::log( msg ); + log( msg, 2, false ); #endif break; case QtCriticalMsg: - tLog::log( msg ); + log( msg, 0 ); break; case QtWarningMsg: - tLog::log( msg ); + log( msg, 0 ); break; case QtFatalMsg: - tLog::log( msg ); + log( msg, 0 ); break; } - - cout << msg << endl; - cout.flush(); } @@ -117,4 +108,29 @@ setupLogfile() qInstallMsgHandler( TomahawkLogHandler ); } -} // ns +} + +using namespace Logger; + +TLog::TLog( unsigned int debugLevel ) + : m_debugLevel( debugLevel ) +{ +} + + +TLog::~TLog() +{ + log( m_msgs.join( " " ).toAscii().data(), m_debugLevel ); +} + + +TLog& +TLog::operator<<( const QVariant& v ) +{ + QString const s = v.toString(); + if ( !s.isEmpty() ) + m_msgs << s; + + return *this; +} + diff --git a/src/libtomahawk/utils/logger.h b/src/libtomahawk/utils/logger.h index e0c8185ee..7d21c0f67 100644 --- a/src/libtomahawk/utils/logger.h +++ b/src/libtomahawk/utils/logger.h @@ -20,22 +20,38 @@ #define LOGGER_H #include +#include #include "dllmacro.h" namespace Logger { - class DLLEXPORT tLog + class DLLEXPORT TLog { public: - tLog& operator<<( const QVariant& v ); - static void log( const char *msg ); + TLog( unsigned int debugLevel = 0 ); + virtual ~TLog(); + + TLog& operator<<( const QVariant& v ); + + private: + QStringList m_msgs; + unsigned int m_debugLevel; + }; + + class DLLEXPORT TDebug : public TLog + { + public: + TDebug( unsigned int debugLevel = 1 ) : TLog( debugLevel ) + { + } }; DLLEXPORT void TomahawkLogHandler( QtMsgType type, const char *msg ); DLLEXPORT void setupLogfile(); } -#define tLog Logger::tLog +#define tLog Logger::TLog +#define tDebug Logger::TDebug #endif \ No newline at end of file