mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-04 13:17:34 +02:00
Make logger less crash prone
This commit is contained in:
@@ -38,35 +38,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
# if defined (_WIN32) && defined (__GLIBCXX__)
|
||||
#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;
|
||||
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<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 )
|
||||
qInstallMessageHandler( TomahawkLogHandler );
|
||||
|
Reference in New Issue
Block a user