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
This commit is contained in:
Jocelyn Turcotte 2017-05-09 17:04:04 +02:00
parent cc9680c1e8
commit cf058bc537
6 changed files with 37 additions and 1 deletions

View File

@ -73,6 +73,7 @@ static const char optionsC[] =
" --logexpire <hours> : removes logs older than <hours> 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 <dirname> : 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();

View File

@ -111,6 +111,7 @@ private:
QString _logDir;
int _logExpire;
bool _logFlush;
bool _logDebug;
bool _userTriggeredConnect;
bool _debugMode;

View File

@ -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<QTextEdit::ExtraSelection> extraSelections;

View File

@ -15,6 +15,7 @@
#ifndef LOGBROWSER_H
#define LOGBROWSER_H
#include <QCheckBox>
#include <QPlainTextEdit>
#include <QTextStream>
#include <QFile>
@ -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;

View File

@ -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()) {

View File

@ -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<QTextStream> _logstream;
QMutex _mutex;
QString _logDirectory;