1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-12 09:04:33 +02:00

Fix logging on windows for unicode usernames

This commit is contained in:
Dominik Schmidt
2014-10-16 03:19:28 +02:00
parent 1acc58a671
commit a97446fd2e
9 changed files with 29 additions and 25 deletions

View File

@@ -98,9 +98,9 @@ int main( int argc, char* argv[] )
; ;
// send log // send log
QFile logFile( Logger::logFile() ); QFile logFile( TomahawkUtils::logFilePath() );
logFile.open( QFile::ReadOnly ); 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(); logFile.close();
reporter.show(); reporter.show();

View File

@@ -38,7 +38,7 @@
using namespace std; using namespace std;
ofstream logfile; QTextStream logStream;
static int s_threshold = -1; static int s_threshold = -1;
QMutex s_mutex; QMutex s_mutex;
bool shutdownInProgress = false; bool shutdownInProgress = false;
@@ -75,13 +75,13 @@ log( const char *msg, unsigned int debugLevel, bool toDisk = true )
#ifdef LOG_SQL_QUERIES #ifdef LOG_SQL_QUERIES
if ( debugLevel == LOGSQL ) if ( debugLevel == LOGSQL )
logfile << "TSQLQUERY: "; logStream << "TSQLQUERY: ";
#endif #endif
if ( shutdownInProgress ) if ( shutdownInProgress )
{ {
// Do not use locales anymore in shutdown // Do not use locales anymore in shutdown
logfile << 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() << ":"
@@ -92,14 +92,14 @@ log( const char *msg, unsigned int debugLevel, bool toDisk = true )
} }
else else
{ {
logfile << 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;
} }
logfile.flush(); logStream.flush();
} }
if ( debugLevel <= LOGEXTRA || (int)debugLevel <= s_threshold ) 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 void
setupLogfile() setupLogfile(QFile& f)
{ {
if ( QFileInfo( logFile() ).size() > LOGFILE_SIZE ) if ( QFileInfo( f ).size() > LOGFILE_SIZE )
{ {
QByteArray lc; QByteArray lc;
{ {
QFile f( logFile() );
f.open( QIODevice::ReadOnly | QIODevice::Text ); f.open( QIODevice::ReadOnly | QIODevice::Text );
f.seek( f.size() - ( LOGFILE_SIZE - ( LOGFILE_SIZE / 4 ) ) ); f.seek( f.size() - ( LOGFILE_SIZE - ( LOGFILE_SIZE / 4 ) ) );
lc = f.readAll(); lc = f.readAll();
f.close(); f.close();
} }
QFile::remove( logFile() ); f.remove();
{ {
QFile f( logFile() );
f.open( QIODevice::WriteOnly | QIODevice::Text ); f.open( QIODevice::WriteOnly | QIODevice::Text );
f.write( lc ); f.write( lc );
f.close(); 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 ) #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
qInstallMessageHandler( TomahawkLogHandler ); qInstallMessageHandler( TomahawkLogHandler );
#else #else

View File

@@ -20,6 +20,7 @@
#define TOMAHAWK_LOGGER_H #define TOMAHAWK_LOGGER_H
#include <QDebug> #include <QDebug>
#include <QFile>
#include "DllMacro.h" #include "DllMacro.h"
#include "config.h" #include "config.h"
@@ -61,8 +62,7 @@ namespace Logger
}; };
DLLEXPORT void TomahawkLogHandler( QtMsgType type, const char* msg ); DLLEXPORT void TomahawkLogHandler( QtMsgType type, const char* msg );
DLLEXPORT void setupLogfile(); DLLEXPORT void setupLogfile(QFile& f);
DLLEXPORT QString logFile();
} }
#define tLog Logger::TLog #define tLog Logger::TLog

View File

@@ -208,6 +208,12 @@ appLogDir()
} }
const QString
logFilePath()
{
return TomahawkUtils::appLogDir().filePath( "Tomahawk.log" );
}
QString QString
timeToString( int seconds ) timeToString( int seconds )
{ {

View File

@@ -147,6 +147,7 @@ namespace TomahawkUtils
DLLEXPORT QDir appConfigDir(); DLLEXPORT QDir appConfigDir();
DLLEXPORT QDir appDataDir(); DLLEXPORT QDir appDataDir();
DLLEXPORT QDir appLogDir(); DLLEXPORT QDir appLogDir();
DLLEXPORT const QString logFilePath();
DLLEXPORT void installTranslator( QObject* parent ); DLLEXPORT void installTranslator( QObject* parent );

View File

@@ -164,7 +164,9 @@ void
TomahawkApp::init() TomahawkApp::init()
{ {
qDebug() << "TomahawkApp thread:" << thread(); qDebug() << "TomahawkApp thread:" << thread();
Logger::setupLogfile(); m_logFile.setFileName(TomahawkUtils::logFilePath());
Logger::setupLogfile(m_logFile);
qsrand( QTime( 0, 0, 0 ).secsTo( QTime::currentTime() ) ); qsrand( QTime( 0, 0, 0 ).secsTo( QTime::currentTime() ) );
tLog() << "Starting Tomahawk..."; tLog() << "Starting Tomahawk...";

View File

@@ -141,6 +141,7 @@ private:
QPointer< Tomahawk::Accounts::AccountManager > m_accountManager; QPointer< Tomahawk::Accounts::AccountManager > m_accountManager;
bool m_scrubFriendlyName; bool m_scrubFriendlyName;
QString m_queuedUrl; QString m_queuedUrl;
QFile m_logFile;
#ifdef LIBLASTFM_FOUND #ifdef LIBLASTFM_FOUND
Scrobbler* m_scrobbler; Scrobbler* m_scrobbler;

View File

@@ -1084,9 +1084,9 @@ void
TomahawkWindow::openLogfile() TomahawkWindow::openLogfile()
{ {
#ifdef WIN32 #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 #else
QDesktopServices::openUrl( QUrl::fromLocalFile( Logger::logFile() ) ); QDesktopServices::openUrl( QUrl::fromLocalFile( TomahawkUtils::logFilePath() ) );
#endif #endif
} }

View File

@@ -137,7 +137,7 @@ DiagnosticsDialog::copyToClipboard()
void void
DiagnosticsDialog::openLogfile() DiagnosticsDialog::openLogfile()
{ {
TomahawkUtils::openUrl( Logger::logFile() ); TomahawkUtils::openUrl( TomahawkUtils::logFilePath() );
} }