From 89d45ab678be07eab8e92ddfa93cb0d366b1937c Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Fri, 17 Oct 2014 19:19:41 +0200 Subject: [PATCH] Make logger less crash prone --- src/libtomahawk/utils/Logger.cpp | 56 +++++++++----------------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/src/libtomahawk/utils/Logger.cpp b/src/libtomahawk/utils/Logger.cpp index 1288003cc..f1394fbdf 100644 --- a/src/libtomahawk/utils/Logger.cpp +++ b/src/libtomahawk/utils/Logger.cpp @@ -38,35 +38,7 @@ using namespace std; -# if defined (_WIN32) && defined (__GLIBCXX__) -#include -#include - -std::ostream* -open_ofstream( const QString& fileName ) -{ - FILE* c_file = _wfopen( fileName.toStdWString().c_str(), L"a+" ); - - __gnu_cxx::stdio_filebuf< char >* buffer = new __gnu_cxx::stdio_filebuf< char >( c_file, std::ios_base::out, 1 ); - - return new ostream( buffer ); -} -# elif defined( _MSC_VER ) -std::ostream* -open_ofstream( const QString& fileName ) -{ - return new ofstream( fileName.toStdWString().c_str() ); -} -# else -std::ostream* -open_ofstream( const QString& fileName ) -{ - return new ofstream( fileName.toStdString().c_str() ); -} -#endif - - -QSharedPointer logStream; +ofstream logStream; static int s_threshold = -1; QMutex s_mutex; bool shutdownInProgress = false; @@ -98,19 +70,19 @@ log( const char *msg, unsigned int debugLevel, bool toDisk = true ) toDisk = true; #endif - if ( !logStream.isNull() && ( toDisk || (int)debugLevel <= s_threshold ) ) + if ( toDisk || (int)debugLevel <= s_threshold ) { QMutexLocker lock( &s_mutex ); #ifdef LOG_SQL_QUERIES if ( debugLevel == LOGSQL ) - *logStream << "TSQLQUERY: "; + logStream << "TSQLQUERY: "; #endif if ( shutdownInProgress ) { // Do not use locales anymore in shutdown - *logStream << QDate::currentDate().day() << "." + logStream << QDate::currentDate().day() << "." << QDate::currentDate().month() << "." << QDate::currentDate().year() << " - " << QTime::currentTime().hour() << ":" @@ -121,14 +93,14 @@ log( const char *msg, unsigned int debugLevel, bool toDisk = true ) } else { - *logStream << QDate::currentDate().toString().toUtf8().data() + logStream << QDate::currentDate().toString().toUtf8().data() << " - " << QTime::currentTime().toString().toUtf8().data() << " [" << QString::number( debugLevel ).toUtf8().data() << "]: " << msg << endl; } - logStream->flush(); + logStream.flush(); } if ( debugLevel <= LOGEXTRA || (int)debugLevel <= s_threshold ) @@ -137,20 +109,20 @@ log( const char *msg, unsigned int debugLevel, bool toDisk = true ) if ( shutdownInProgress ) { - cout << QTime::currentTime().hour() << ":" + wcout << QTime::currentTime().hour() << ":" << QTime::currentTime().minute() << ":" << QTime::currentTime().second() - << " [" << QString::number( debugLevel ).toUtf8().data() << "]: " + << " [" << QString::number( debugLevel ).toStdWString().c_str() << "]: " << msg << endl; } else { - cout << QTime::currentTime().toString().toUtf8().data() - << " [" << QString::number( debugLevel ).toUtf8().data() << "]: " + wcout << QTime::currentTime().toString().toUtf8().data() + << " [" << QString::number( debugLevel ).toStdWString().c_str() << "]: " << msg << endl; } - cout.flush(); + wcout.flush(); } } @@ -216,7 +188,11 @@ setupLogfile( QFile& f ) } } - logStream = QSharedPointer( open_ofstream( f.fileName() ) ); +#ifdef _WIN32 + logStream.open( f.fileName().toStdWString().c_str() ); +#else + logStream.open( f.fileName().toStdString().c_str() ); +#endif #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 ) qInstallMessageHandler( TomahawkLogHandler );