From cf058bc537ff926f2c0c03be40fcff978d570c70 Mon Sep 17 00:00:00 2001 From: Jocelyn Turcotte Date: Tue, 9 May 2017 17:04:04 +0200 Subject: [PATCH] Add the possiblility to enable debug logging categories Add a checkbox in the log window as well as a --logdebug command-line option that should have the same effect. Issue #5647 --- src/gui/application.cpp | 5 +++++ src/gui/application.h | 1 + src/gui/logbrowser.cpp | 16 ++++++++++++++++ src/gui/logbrowser.h | 4 ++++ src/libsync/logger.cpp | 8 +++++++- src/libsync/logger.h | 4 ++++ 6 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/gui/application.cpp b/src/gui/application.cpp index 863298304..c9f8f173a 100644 --- a/src/gui/application.cpp +++ b/src/gui/application.cpp @@ -73,6 +73,7 @@ static const char optionsC[] = " --logexpire : removes logs older than hours.\n" " (to be used with --logdir)\n" " --logflush : flush the log file after every write.\n" + " --logdebug : also output debug-level messages in the log (equivalent to setting the env var QT_LOGGING_RULES=\"qt.*=true;*.debug=true\").\n" " --confdir : Use the given configuration folder.\n" ; @@ -105,6 +106,7 @@ Application::Application(int &argc, char **argv) : _showLogWindow(false), _logExpire(0), _logFlush(false), + _logDebug(false), _userTriggeredConnect(false), _debugMode(false) { @@ -358,6 +360,7 @@ void Application::setupLogging() Logger::instance()->setLogDir(_logDir); Logger::instance()->setLogExpire(_logExpire); Logger::instance()->setLogFlush(_logFlush); + Logger::instance()->setLogDebug(_logDebug); Logger::instance()->enterNextLogFile(); @@ -426,6 +429,8 @@ void Application::parseOptions(const QStringList &options) } } else if (option == QLatin1String("--logflush")) { _logFlush = true; + } else if (option == QLatin1String("--logdebug")) { + _logDebug = true; } else if (option == QLatin1String("--confdir")) { if (it.hasNext() && !it.peekNext().startsWith(QLatin1String("--"))) { QString confDir = it.next(); diff --git a/src/gui/application.h b/src/gui/application.h index b7166c56a..0228ecafc 100644 --- a/src/gui/application.h +++ b/src/gui/application.h @@ -111,6 +111,7 @@ private: QString _logDir; int _logExpire; bool _logFlush; + bool _logDebug; bool _userTriggeredConnect; bool _debugMode; diff --git a/src/gui/logbrowser.cpp b/src/gui/logbrowser.cpp index a5cf44c24..b4971bf45 100644 --- a/src/gui/logbrowser.cpp +++ b/src/gui/logbrowser.cpp @@ -85,6 +85,11 @@ LogBrowser::LogBrowser(QWidget *parent) : toolLayout->addWidget( _statusLabel ); toolLayout->addStretch(5); + // Debug logging + _logDebugCheckBox = new QCheckBox(tr("&Capture debug messages") + " "); + connect(_logDebugCheckBox, SIGNAL(stateChanged(int)), SLOT(slotDebugCheckStateChanged(int))); + toolLayout->addWidget( _logDebugCheckBox ); + QDialogButtonBox *btnbox = new QDialogButtonBox; QPushButton *closeBtn = btnbox->addButton( QDialogButtonBox::Close ); connect(closeBtn,SIGNAL(clicked()),this,SLOT(close())); @@ -129,6 +134,12 @@ LogBrowser::~LogBrowser() { } +void LogBrowser::showEvent(QShowEvent *) +{ + // This could have been changed through the --logdebug argument passed through the single application. + _logDebugCheckBox->setCheckState(Logger::instance()->logDebug() ? Qt::Checked : Qt::Unchecked); +} + void LogBrowser::closeEvent(QCloseEvent *) { ConfigFile cfg; @@ -153,6 +164,11 @@ void LogBrowser::slotFind() search( searchText ); } +void LogBrowser::slotDebugCheckStateChanged(int checkState) +{ + Logger::instance()->setLogDebug(checkState == Qt::Checked); +} + void LogBrowser::search( const QString& str ) { QList extraSelections; diff --git a/src/gui/logbrowser.h b/src/gui/logbrowser.h index 719591a7c..940e7a927 100644 --- a/src/gui/logbrowser.h +++ b/src/gui/logbrowser.h @@ -15,6 +15,7 @@ #ifndef LOGBROWSER_H #define LOGBROWSER_H +#include #include #include #include @@ -56,11 +57,13 @@ public: void setLogFile(const QString& , bool ); protected: + void showEvent(QShowEvent *) Q_DECL_OVERRIDE; void closeEvent(QCloseEvent *) Q_DECL_OVERRIDE; protected slots: void slotNewLog( const QString &msg ); void slotFind(); + void slotDebugCheckStateChanged(int); void search( const QString& ); void slotSave(); void slotClearLog(); @@ -68,6 +71,7 @@ protected slots: private: LogWidget *_logWidget; QLineEdit *_findTermEdit; + QCheckBox *_logDebugCheckBox; QPushButton *_saveBtn; QPushButton *_clearBtn; QLabel *_statusLabel; diff --git a/src/libsync/logger.cpp b/src/libsync/logger.cpp index ed31b6110..47b731eba 100644 --- a/src/libsync/logger.cpp +++ b/src/libsync/logger.cpp @@ -81,7 +81,7 @@ Logger *Logger::instance() } Logger::Logger( QObject* parent) : QObject(parent), - _showTime(true), _logWindowActivated(false), _doFileFlush(false), _logExpire(0) + _showTime(true), _logWindowActivated(false), _doFileFlush(false), _logExpire(0), _logDebug(false) { #if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) qSetMessagePattern("%{time MM-dd hh:mm:ss:zzz} [ %{type} %{category} ]%{if-debug}\t[ %{function} ]%{endif}:\t%{message}"); @@ -235,6 +235,12 @@ void Logger::setLogFlush( bool flush ) _doFileFlush = flush; } +void Logger::setLogDebug( bool debug ) +{ + QLoggingCategory::setFilterRules(debug ? QStringLiteral("qt.*=true\n*.debug=true") : QString()); + _logDebug = debug; +} + void Logger::enterNextLogFile() { if (!_logDirectory.isEmpty()) { diff --git a/src/libsync/logger.h b/src/libsync/logger.h index f4c767adf..926bb09e4 100644 --- a/src/libsync/logger.h +++ b/src/libsync/logger.h @@ -66,6 +66,9 @@ public: void setLogDir( const QString& dir ); void setLogFlush( bool flush ); + bool logDebug() const { return _logDebug; } + void setLogDebug( bool debug ); + signals: void logWindowLog(const QString&); @@ -85,6 +88,7 @@ private: QFile _logFile; bool _doFileFlush; int _logExpire; + bool _logDebug; QScopedPointer _logstream; QMutex _mutex; QString _logDirectory; -- 2.30.2