1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-05 05:37:29 +02:00

Make logger less crash prone

This commit is contained in:
Dominik Schmidt
2014-10-17 19:19:41 +02:00
parent 223be945eb
commit 89d45ab678

View File

@@ -38,35 +38,7 @@
using namespace std; using namespace std;
# if defined (_WIN32) && defined (__GLIBCXX__) ofstream logStream;
#include <ext/stdio_filebuf.h>
#include <cstdio>
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<ostream> logStream;
static int s_threshold = -1; static int s_threshold = -1;
QMutex s_mutex; QMutex s_mutex;
bool shutdownInProgress = false; bool shutdownInProgress = false;
@@ -98,19 +70,19 @@ log( const char *msg, unsigned int debugLevel, bool toDisk = true )
toDisk = true; toDisk = true;
#endif #endif
if ( !logStream.isNull() && ( toDisk || (int)debugLevel <= s_threshold ) ) if ( toDisk || (int)debugLevel <= s_threshold )
{ {
QMutexLocker lock( &s_mutex ); QMutexLocker lock( &s_mutex );
#ifdef LOG_SQL_QUERIES #ifdef LOG_SQL_QUERIES
if ( debugLevel == LOGSQL ) if ( debugLevel == LOGSQL )
*logStream << "TSQLQUERY: "; logStream << "TSQLQUERY: ";
#endif #endif
if ( shutdownInProgress ) if ( shutdownInProgress )
{ {
// Do not use locales anymore in shutdown // Do not use locales anymore in shutdown
*logStream << QDate::currentDate().day() << "." logStream << QDate::currentDate().day() << "."
<< QDate::currentDate().month() << "." << QDate::currentDate().month() << "."
<< QDate::currentDate().year() << " - " << QDate::currentDate().year() << " - "
<< QTime::currentTime().hour() << ":" << QTime::currentTime().hour() << ":"
@@ -121,14 +93,14 @@ log( const char *msg, unsigned int debugLevel, bool toDisk = true )
} }
else else
{ {
*logStream << QDate::currentDate().toString().toUtf8().data() logStream << QDate::currentDate().toString().toUtf8().data()
<< " - " << " - "
<< QTime::currentTime().toString().toUtf8().data() << QTime::currentTime().toString().toUtf8().data()
<< " [" << QString::number( debugLevel ).toUtf8().data() << "]: " << " [" << QString::number( debugLevel ).toUtf8().data() << "]: "
<< msg << endl; << msg << endl;
} }
logStream->flush(); logStream.flush();
} }
if ( debugLevel <= LOGEXTRA || (int)debugLevel <= s_threshold ) if ( debugLevel <= LOGEXTRA || (int)debugLevel <= s_threshold )
@@ -137,20 +109,20 @@ log( const char *msg, unsigned int debugLevel, bool toDisk = true )
if ( shutdownInProgress ) if ( shutdownInProgress )
{ {
cout << QTime::currentTime().hour() << ":" wcout << QTime::currentTime().hour() << ":"
<< QTime::currentTime().minute() << ":" << QTime::currentTime().minute() << ":"
<< QTime::currentTime().second() << QTime::currentTime().second()
<< " [" << QString::number( debugLevel ).toUtf8().data() << "]: " << " [" << QString::number( debugLevel ).toStdWString().c_str() << "]: "
<< msg << endl; << msg << endl;
} }
else else
{ {
cout << QTime::currentTime().toString().toUtf8().data() wcout << QTime::currentTime().toString().toUtf8().data()
<< " [" << QString::number( debugLevel ).toUtf8().data() << "]: " << " [" << QString::number( debugLevel ).toStdWString().c_str() << "]: "
<< msg << endl; << msg << endl;
} }
cout.flush(); wcout.flush();
} }
} }
@@ -216,7 +188,11 @@ setupLogfile( QFile& f )
} }
} }
logStream = QSharedPointer<ostream>( 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 ) #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
qInstallMessageHandler( TomahawkLogHandler ); qInstallMessageHandler( TomahawkLogHandler );