From 7c1f91abdd4c6fcc69c1858592a51081f9d12739 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 3 Jun 2013 16:23:29 +0200 Subject: [PATCH] Add the --logdir commandline option To log to a different file for every sync. This also changed a QueuedConnection to AutoConnection, that way we directly save the log from the main thread in the file without waiting for the event loop (so if it crashes before, the log has been writen) --- src/mirall/application.cpp | 39 +++++++++++++++++++++++++++++++++++++- src/mirall/application.h | 2 ++ src/mirall/logbrowser.cpp | 12 ++++++------ src/mirall/logbrowser.h | 2 +- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/mirall/application.cpp b/src/mirall/application.cpp index 4648000a3..c497770b1 100644 --- a/src/mirall/application.cpp +++ b/src/mirall/application.cpp @@ -526,7 +526,9 @@ void Application::setupLogBrowser() _logBrowser = new LogBrowser; qInstallMsgHandler( mirallLogCatcher ); // ## TODO: allow new log name maybe? - if (!_logFile.isEmpty()) { + if (!_logDirectory.isEmpty()) { + enterNextLogFile(); + } else if (!_logFile.isEmpty()) { qDebug() << "Logging into logfile: " << _logFile << " with flush " << _logFlush; _logBrowser->setLogFile( _logFile, _logFlush ); } @@ -542,6 +544,30 @@ void Application::setupLogBrowser() } +void Application::enterNextLogFile() +{ + if (_logBrowser && !_logDirectory.isEmpty()) { + QDir dir(_logDirectory); + if (!dir.exists()) { + dir.mkpath("."); + } + + // Find out what is the file with the highest nymber if any + QStringList files = dir.entryList(QStringList("owncloud.log.*"), + QDir::Files); + QRegExp rx("owncloud.log.(\\d+)"); + uint maxNumber = 0; + foreach(const QString &s, files) { + if (rx.exactMatch(s)) { + maxNumber = qMax(maxNumber, rx.cap(1).toUInt()); + } + } + + QString filename = _logDirectory + "/owncloud.log." + QString::number(maxNumber+1); + _logBrowser->setLogFile(filename , _logFlush); + } +} + QNetworkProxy proxyFromConfig(const MirallConfigFile& cfg) { QNetworkProxy proxy; @@ -839,6 +865,10 @@ void Application::slotSyncStateChange( const QString& alias ) computeOverallSyncStatus(); qDebug() << "Sync state changed for folder " << alias << ": " << result.statusString(); + + if (result.status() == SyncResult::Success || result.status() == SyncResult::Error) { + enterNextLogFile(); + } } void Application::parseOptions(const QStringList &options) @@ -862,6 +892,12 @@ void Application::parseOptions(const QStringList &options) } else { setHelp(); } + } else if (option == QLatin1String("--logdir")) { + if (it.hasNext() && !it.peekNext().startsWith(QLatin1String("--"))) { + _logDirectory = it.next(); + } else { + setHelp(); + } } else if (option == QLatin1String("--logflush")) { _logFlush = true; } else if (option == QLatin1String("--monoicons")) { @@ -974,6 +1010,7 @@ setHelp(); std::cout << " -h --help : show this help screen." << std::endl; std::cout << " --logwindow : open a window to show log output." << std::endl; std::cout << " --logfile : write log output to file ." << std::endl; + std::cout << " --logdir : write each sync log output in a different file in directory ." << std::endl; std::cout << " --logflush : flush the log file after every write." << std::endl; std::cout << " --monoicons : Use black/white pictograms for systray." << std::endl; std::cout << " --confdir : Use the given configuration directory." << std::endl; diff --git a/src/mirall/application.h b/src/mirall/application.h index 12a30edfa..de2d76e1b 100644 --- a/src/mirall/application.h +++ b/src/mirall/application.h @@ -79,6 +79,7 @@ protected: void setupContextMenu(); void setupLogBrowser(); void setupProxy(); + void enterNextLogFile(); //folders have to be disabled while making config changes void computeOverallSyncStatus(); @@ -133,6 +134,7 @@ private: QMap _overallStatusStrings; LogBrowser *_logBrowser; QString _logFile; + QString _logDirectory; bool _showLogWindow; bool _logFlush; bool _helpOnly; diff --git a/src/mirall/logbrowser.cpp b/src/mirall/logbrowser.cpp index 252d178e4..e2b7ad00b 100644 --- a/src/mirall/logbrowser.cpp +++ b/src/mirall/logbrowser.cpp @@ -113,15 +113,12 @@ LogBrowser::LogBrowser(QWidget *parent) : setModal(false); - // needs to be a queued connection as logs from other threads come in - connect(Logger::instance(), SIGNAL(newLog(QString)),this,SLOT(slotNewLog(QString)), Qt::QueuedConnection); + // Direct connection for log comming from this thread, and queued for the one in a different thread + connect(Logger::instance(), SIGNAL(newLog(QString)),this,SLOT(slotNewLog(QString)), Qt::AutoConnection); } LogBrowser::~LogBrowser() { - if( _logstream ) { - _logFile.close(); - } } void LogBrowser::slotNewLog( const QString& msg ) @@ -138,6 +135,9 @@ void LogBrowser::slotNewLog( const QString& msg ) void LogBrowser::setLogFile( const QString & name, bool flush ) { + if( _logstream ) { + _logFile.close(); + } _logFile.setFileName( name ); if(!_logFile.open(QIODevice::WriteOnly)) { @@ -150,7 +150,7 @@ void LogBrowser::setLogFile( const QString & name, bool flush ) return; } _doFileFlush = flush; - _logstream = new QTextStream( &_logFile ); + _logstream.reset(new QTextStream( &_logFile )); } void LogBrowser::slotFind() diff --git a/src/mirall/logbrowser.h b/src/mirall/logbrowser.h index a0a74c3d9..5f61fbe4e 100644 --- a/src/mirall/logbrowser.h +++ b/src/mirall/logbrowser.h @@ -67,7 +67,7 @@ private: QFile _logFile; bool _doFileFlush; - QTextStream *_logstream; + QScopedPointer _logstream; }; } // namespace