Skip to content

Commit

Permalink
LogWindow: Remove output, add "go to log folder" button #6475
Browse files Browse the repository at this point in the history
  • Loading branch information
ckamm committed Apr 9, 2019
1 parent 2b0f32c commit fcf2af3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 198 deletions.
Binary file modified docs/modules/ROOT/assets/images/log_output_window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions docs/modules/ROOT/pages/troubleshooting.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ The Log Output window opens.
+
image:log_output_window.png[image]
+
. Enable the btn:[Permanently save logs] checkbox.
. Look at its tooltip and take note of the directory the logs will be saved to.
. Navigate to this directory.
. Enable the btn:[Enable logging to temporary folder] checkbox.
. Later, to find the log files, click the btn:[Open folder] button.
. Select the logs for the time frame in which the issue occurred.

NOTE: That the choice to enable logging will be persist across client restarts.
Expand Down
188 changes: 34 additions & 154 deletions src/gui/logbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@
#include <iostream>

#include <QDialogButtonBox>
#include <QTextDocument>
#include <QLayout>
#include <QPushButton>
#include <QLabel>
#include <QFileDialog>
#include <QDir>
#include <QTextStream>
#include <QMessageBox>
#include <QCoreApplication>
#include <QSettings>
#include <QAction>
#include <QDesktopServices>

#include "configfile.h"
#include "logger.h"
Expand All @@ -37,199 +36,80 @@ namespace OCC {

// ==============================================================================

LogWidget::LogWidget(QWidget *parent)
: QPlainTextEdit(parent)
{
setReadOnly(true);
QFont font;
font.setFamily(QLatin1String("Courier New"));
font.setFixedPitch(true);
document()->setDefaultFont(font);
}

// ==============================================================================

LogBrowser::LogBrowser(QWidget *parent)
: QDialog(parent)
, _logWidget(new LogWidget(parent))
{
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
setObjectName("LogBrowser"); // for save/restoreGeometry()
setWindowTitle(tr("Log Output"));
setMinimumWidth(600);

QVBoxLayout *mainLayout = new QVBoxLayout;
// mainLayout->setMargin(0);

mainLayout->addWidget(_logWidget);

QHBoxLayout *toolLayout = new QHBoxLayout;
mainLayout->addLayout(toolLayout);

// Search input field
QLabel *lab = new QLabel(tr("&Search:") + " ");
_findTermEdit = new QLineEdit;
lab->setBuddy(_findTermEdit);
toolLayout->addWidget(lab);
toolLayout->addWidget(_findTermEdit);
auto label = new QLabel(
tr("The client can write debug logs to a temporary folder. "
"These logs are very helpful for diagnosing problems.\n"
"Since log files can get large, the client will start a new one for each sync "
"run and compress older ones. It will also delete log files after a couple "
"of hours to avoid consuming too much disk space.\n"
"If enabled, logs will be written to %1")
.arg(Logger::instance()->temporaryFolderLogDirPath()));
label->setWordWrap(true);
label->setTextInteractionFlags(Qt::TextSelectableByMouse);
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
mainLayout->addWidget(label);

// find button
QPushButton *findBtn = new QPushButton;
findBtn->setText(tr("&Find"));
connect(findBtn, &QAbstractButton::clicked, this, &LogBrowser::slotFind);
toolLayout->addWidget(findBtn);

// stretch
toolLayout->addStretch(1);
_statusLabel = new QLabel;
toolLayout->addWidget(_statusLabel);
toolLayout->addStretch(5);

// Debug logging
_logDebugCheckBox = new QCheckBox(tr("&Capture debug messages") + " ");
connect(_logDebugCheckBox, &QCheckBox::stateChanged, this, &LogBrowser::slotDebugCheckStateChanged);
toolLayout->addWidget(_logDebugCheckBox);
// button to permanently save logs
auto enableLoggingButton = new QCheckBox;
enableLoggingButton->setText(tr("Enable logging to temporary folder"));
enableLoggingButton->setChecked(ConfigFile().automaticLogDir());
connect(enableLoggingButton, &QCheckBox::toggled, this, &LogBrowser::togglePermanentLogging);
mainLayout->addWidget(enableLoggingButton);

label = new QLabel(
tr("This setting persists across client restarts.\n"
"Note that using any logging command line options will override this setting."));
label->setWordWrap(true);
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
mainLayout->addWidget(label);

auto openFolderButton = new QPushButton;
openFolderButton->setText(tr("Open folder"));
connect(openFolderButton, &QPushButton::clicked, this, []() {
QDesktopServices::openUrl(Logger::instance()->temporaryFolderLogDirPath());
});
mainLayout->addWidget(openFolderButton);

QDialogButtonBox *btnbox = new QDialogButtonBox;
QPushButton *closeBtn = btnbox->addButton(QDialogButtonBox::Close);
connect(closeBtn, &QAbstractButton::clicked, this, &QWidget::close);

mainLayout->addStretch();
mainLayout->addWidget(btnbox);

// button to permanently save logs
_permanentLogging = new QCheckBox;
_permanentLogging->setText(tr("Permanently save logs"));
_permanentLogging->setToolTip(
tr("When this option is enabled and no other logging is configured, "
"logs will be written to a temporary folder and expire after a few hours. "
"This setting persists across client restarts.\n"
"\n"
"Logs will be written to %1")
.arg(Logger::instance()->temporaryFolderLogDirPath()));
_permanentLogging->setChecked(ConfigFile().automaticLogDir());
btnbox->addButton(_permanentLogging, QDialogButtonBox::ActionRole);
connect(_permanentLogging, &QCheckBox::toggled, this, &LogBrowser::togglePermanentLogging);

// clear button
_clearBtn = new QPushButton;
_clearBtn->setText(tr("Clear"));
_clearBtn->setToolTip(tr("Clear the log display."));
btnbox->addButton(_clearBtn, QDialogButtonBox::ActionRole);
connect(_clearBtn, &QAbstractButton::clicked, this, &LogBrowser::slotClearLog);

// save Button
_saveBtn = new QPushButton;
_saveBtn->setText(tr("S&ave"));
_saveBtn->setToolTip(tr("Save the log file to a file on disk for debugging."));
btnbox->addButton(_saveBtn, QDialogButtonBox::ActionRole);
connect(_saveBtn, &QAbstractButton::clicked, this, &LogBrowser::slotSave);

setLayout(mainLayout);

setModal(false);

Logger::instance()->setLogWindowActivated(true);
// Direct connection for log coming from this thread, and queued for the one in a different thread
connect(Logger::instance(), &Logger::logWindowLog, this, &LogBrowser::slotNewLog, Qt::AutoConnection);

QAction *showLogWindow = new QAction(this);
showLogWindow->setShortcut(QKeySequence("F12"));
connect(showLogWindow, &QAction::triggered, this, &QWidget::close);
addAction(showLogWindow);

ConfigFile cfg;
cfg.restoreGeometry(this);
int lines = cfg.maxLogLines();
_logWidget->document()->setMaximumBlockCount(lines);
}

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;
cfg.saveGeometry(this);
}


void LogBrowser::slotNewLog(const QString &msg)
{
if (_logWidget->isVisible()) {
_logWidget->appendPlainText(msg);
}
}


void LogBrowser::slotFind()
{
QString searchText = _findTermEdit->text();

if (searchText.isEmpty())
return;

search(searchText);
}

void LogBrowser::slotDebugCheckStateChanged(int checkState)
{
Logger::instance()->setLogDebug(checkState == Qt::Checked);
}

void LogBrowser::search(const QString &str)
{
QList<QTextEdit::ExtraSelection> extraSelections;

_logWidget->moveCursor(QTextCursor::Start);
QColor color = QColor(Qt::gray).lighter(130);
_statusLabel->clear();

while (_logWidget->find(str)) {
QTextEdit::ExtraSelection extra;
extra.format.setBackground(color);

extra.cursor = _logWidget->textCursor();
extraSelections.append(extra);
}

QString stat = QString::fromLatin1("Search term %1 with %2 search results.").arg(str).arg(extraSelections.count());
_statusLabel->setText(stat);

_logWidget->setExtraSelections(extraSelections);
}

void LogBrowser::slotSave()
{
_saveBtn->setEnabled(false);

QString saveFile = QFileDialog::getSaveFileName(this, tr("Save log file"), QDir::homePath());

if (!saveFile.isEmpty()) {
QFile file(saveFile);

if (file.open(QIODevice::WriteOnly)) {
QTextStream stream(&file);
stream << _logWidget->toPlainText();
file.close();
} else {
QMessageBox::critical(this, tr("Error"), tr("Could not write to log file %1").arg(saveFile));
}
}
_saveBtn->setEnabled(true);
}

void LogBrowser::slotClearLog()
{
_logWidget->clear();
}

void LogBrowser::togglePermanentLogging(bool enabled)
{
ConfigFile().setAutomaticLogDir(enabled);
Expand Down
31 changes: 0 additions & 31 deletions src/gui/logbrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@

namespace OCC {

/**
* @brief The LogWidget class
* @ingroup gui
*/
class LogWidget : public QPlainTextEdit
{
Q_OBJECT
public:
explicit LogWidget(QWidget *parent = 0);

signals:
};

/**
* @brief The LogBrowser class
* @ingroup gui
Expand All @@ -53,29 +40,11 @@ class LogBrowser : public QDialog
explicit LogBrowser(QWidget *parent = 0);
~LogBrowser();

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();
void togglePermanentLogging(bool enabled);

private:
LogWidget *_logWidget;
QLineEdit *_findTermEdit;
QCheckBox *_logDebugCheckBox;
QCheckBox *_permanentLogging;
QPushButton *_saveBtn;
QPushButton *_clearBtn;
QLabel *_statusLabel;
};

} // namespace
Expand Down
9 changes: 1 addition & 8 deletions src/libsync/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ Logger *Logger::instance()
Logger::Logger(QObject *parent)
: QObject(parent)
, _showTime(true)
, _logWindowActivated(false)
, _doFileFlush(false)
, _logExpire(0)
, _logDebug(false)
Expand Down Expand Up @@ -114,7 +113,7 @@ void Logger::log(Log log)
bool Logger::isNoop() const
{
QMutexLocker lock(&_mutex);
return !_logstream && !_logWindowActivated;
return !_logstream;
}

bool Logger::isLoggingToFile() const
Expand Down Expand Up @@ -145,12 +144,6 @@ void Logger::mirallLog(const QString &message)
Logger::instance()->log(log_);
}

void Logger::setLogWindowActivated(bool activated)
{
QMutexLocker locker(&_mutex);
_logWindowActivated = activated;
}

void Logger::setLogFile(const QString &name)
{
QMutexLocker locker(&_mutex);
Expand Down
2 changes: 0 additions & 2 deletions src/libsync/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class OWNCLOUDSYNC_EXPORT Logger : public QObject
void postOptionalGuiLog(const QString &title, const QString &message);
void postGuiMessage(const QString &title, const QString &message);

void setLogWindowActivated(bool activated);
void setLogFile(const QString &name);
void setLogExpire(int expire);
void setLogDir(const QString &dir);
Expand Down Expand Up @@ -97,7 +96,6 @@ public slots:
~Logger();
QList<Log> _logs;
bool _showTime;
bool _logWindowActivated;
QFile _logFile;
bool _doFileFlush;
int _logExpire;
Expand Down

0 comments on commit fcf2af3

Please sign in to comment.