diff --git a/src/crashreporter/main.cpp b/src/crashreporter/main.cpp index 8923c8c72..4a2ee20d4 100644 --- a/src/crashreporter/main.cpp +++ b/src/crashreporter/main.cpp @@ -98,9 +98,9 @@ int main( int argc, char* argv[] ) ; // send log - QFile logFile( Logger::logFile() ); + QFile logFile( TomahawkUtils::logFilePath() ); logFile.open( QFile::ReadOnly ); - reporter.setReportData( "upload_file_tomahawklog", qCompress( logFile.readAll() ), "application/x-gzip", QFileInfo( Logger::logFile() ).fileName().toUtf8()); + reporter.setReportData( "upload_file_tomahawklog", qCompress( logFile.readAll() ), "application/x-gzip", QFileInfo( logFile ).fileName().toUtf8()); logFile.close(); reporter.show(); diff --git a/src/libtomahawk/utils/Logger.cpp b/src/libtomahawk/utils/Logger.cpp index aa01a0cd9..1d67e2218 100644 --- a/src/libtomahawk/utils/Logger.cpp +++ b/src/libtomahawk/utils/Logger.cpp @@ -38,7 +38,7 @@ using namespace std; -ofstream logfile; +QTextStream logStream; static int s_threshold = -1; QMutex s_mutex; bool shutdownInProgress = false; @@ -75,13 +75,13 @@ log( const char *msg, unsigned int debugLevel, bool toDisk = true ) #ifdef LOG_SQL_QUERIES if ( debugLevel == LOGSQL ) - logfile << "TSQLQUERY: "; + logStream << "TSQLQUERY: "; #endif if ( shutdownInProgress ) { // Do not use locales anymore in shutdown - logfile << QDate::currentDate().day() << "." + logStream << QDate::currentDate().day() << "." << QDate::currentDate().month() << "." << QDate::currentDate().year() << " - " << QTime::currentTime().hour() << ":" @@ -92,14 +92,14 @@ log( const char *msg, unsigned int debugLevel, bool toDisk = true ) } else { - logfile << QDate::currentDate().toString().toUtf8().data() + logStream << QDate::currentDate().toString().toUtf8().data() << " - " << QTime::currentTime().toString().toUtf8().data() << " [" << QString::number( debugLevel ).toUtf8().data() << "]: " << msg << endl; } - logfile.flush(); + logStream.flush(); } if ( debugLevel <= LOGEXTRA || (int)debugLevel <= s_threshold ) @@ -164,38 +164,32 @@ TomahawkLogHandler( QtMsgType type, const char* msg ) } -QString -logFile() -{ - return TomahawkUtils::appLogDir().filePath( "Tomahawk.log" ); -} - void -setupLogfile() +setupLogfile(QFile& f) { - if ( QFileInfo( logFile() ).size() > LOGFILE_SIZE ) + if ( QFileInfo( f ).size() > LOGFILE_SIZE ) { QByteArray lc; { - QFile f( logFile() ); f.open( QIODevice::ReadOnly | QIODevice::Text ); f.seek( f.size() - ( LOGFILE_SIZE - ( LOGFILE_SIZE / 4 ) ) ); lc = f.readAll(); f.close(); } - QFile::remove( logFile() ); + f.remove(); { - QFile f( logFile() ); f.open( QIODevice::WriteOnly | QIODevice::Text ); f.write( lc ); f.close(); } } - logfile.open( logFile().toUtf8().constData(), ios::app ); + f.open(QIODevice::Append | QIODevice::Text); + logStream.setDevice(&f); + #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 ) qInstallMessageHandler( TomahawkLogHandler ); #else diff --git a/src/libtomahawk/utils/Logger.h b/src/libtomahawk/utils/Logger.h index 14e90d920..c5cc89096 100644 --- a/src/libtomahawk/utils/Logger.h +++ b/src/libtomahawk/utils/Logger.h @@ -20,6 +20,7 @@ #define TOMAHAWK_LOGGER_H #include +#include #include "DllMacro.h" #include "config.h" @@ -61,8 +62,7 @@ namespace Logger }; DLLEXPORT void TomahawkLogHandler( QtMsgType type, const char* msg ); - DLLEXPORT void setupLogfile(); - DLLEXPORT QString logFile(); + DLLEXPORT void setupLogfile(QFile& f); } #define tLog Logger::TLog diff --git a/src/libtomahawk/utils/TomahawkUtils.cpp b/src/libtomahawk/utils/TomahawkUtils.cpp index c5ea302d9..4f2e34473 100644 --- a/src/libtomahawk/utils/TomahawkUtils.cpp +++ b/src/libtomahawk/utils/TomahawkUtils.cpp @@ -208,6 +208,12 @@ appLogDir() } +const QString +logFilePath() +{ + return TomahawkUtils::appLogDir().filePath( "Tomahawk.log" ); +} + QString timeToString( int seconds ) { diff --git a/src/libtomahawk/utils/TomahawkUtils.h b/src/libtomahawk/utils/TomahawkUtils.h index 482da0b75..00e14db8d 100644 --- a/src/libtomahawk/utils/TomahawkUtils.h +++ b/src/libtomahawk/utils/TomahawkUtils.h @@ -147,6 +147,7 @@ namespace TomahawkUtils DLLEXPORT QDir appConfigDir(); DLLEXPORT QDir appDataDir(); DLLEXPORT QDir appLogDir(); + DLLEXPORT const QString logFilePath(); DLLEXPORT void installTranslator( QObject* parent ); diff --git a/src/tomahawk/TomahawkApp.cpp b/src/tomahawk/TomahawkApp.cpp index 133932abf..61fc9271b 100644 --- a/src/tomahawk/TomahawkApp.cpp +++ b/src/tomahawk/TomahawkApp.cpp @@ -164,7 +164,9 @@ void TomahawkApp::init() { qDebug() << "TomahawkApp thread:" << thread(); - Logger::setupLogfile(); + m_logFile.setFileName(TomahawkUtils::logFilePath()); + Logger::setupLogfile(m_logFile); + qsrand( QTime( 0, 0, 0 ).secsTo( QTime::currentTime() ) ); tLog() << "Starting Tomahawk..."; diff --git a/src/tomahawk/TomahawkApp.h b/src/tomahawk/TomahawkApp.h index 6ad541363..98288cded 100644 --- a/src/tomahawk/TomahawkApp.h +++ b/src/tomahawk/TomahawkApp.h @@ -141,6 +141,7 @@ private: QPointer< Tomahawk::Accounts::AccountManager > m_accountManager; bool m_scrubFriendlyName; QString m_queuedUrl; + QFile m_logFile; #ifdef LIBLASTFM_FOUND Scrobbler* m_scrobbler; diff --git a/src/tomahawk/TomahawkWindow.cpp b/src/tomahawk/TomahawkWindow.cpp index aadf50e9b..1d44bfca1 100644 --- a/src/tomahawk/TomahawkWindow.cpp +++ b/src/tomahawk/TomahawkWindow.cpp @@ -1084,9 +1084,9 @@ void TomahawkWindow::openLogfile() { #ifdef WIN32 - ShellExecuteW( 0, 0, (LPCWSTR)Logger::logFile().utf16(), 0, 0, SW_SHOWNORMAL ); + ShellExecuteW( 0, 0, (LPCWSTR)TomahawkUtils::logFilePath().utf16(), 0, 0, SW_SHOWNORMAL ); #else - QDesktopServices::openUrl( QUrl::fromLocalFile( Logger::logFile() ) ); + QDesktopServices::openUrl( QUrl::fromLocalFile( TomahawkUtils::logFilePath() ) ); #endif } diff --git a/src/tomahawk/dialogs/DiagnosticsDialog.cpp b/src/tomahawk/dialogs/DiagnosticsDialog.cpp index 53db0f2dc..51a79bbf3 100644 --- a/src/tomahawk/dialogs/DiagnosticsDialog.cpp +++ b/src/tomahawk/dialogs/DiagnosticsDialog.cpp @@ -137,7 +137,7 @@ DiagnosticsDialog::copyToClipboard() void DiagnosticsDialog::openLogfile() { - TomahawkUtils::openUrl( Logger::logFile() ); + TomahawkUtils::openUrl( TomahawkUtils::logFilePath() ); }