LogWindow: Remove output, add "go to log folder" button #6475
authorChristian Kamm <mail@ckamm.de>
Fri, 15 Mar 2019 11:12:13 +0000 (12:12 +0100)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:58:47 +0000 (10:58 +0100)
src/gui/logbrowser.cpp
src/gui/logbrowser.h
src/libsync/logger.cpp
src/libsync/logger.h

index 34dc8891c1e6c1396c4db75b9b5bd0a7b876337b..8ad0410e8a2572188e0489bca40919954d5040bb 100644 (file)
 #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"
@@ -37,197 +36,78 @@ 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);
 
-    auto *mainLayout = new QVBoxLayout;
-    // mainLayout->setMargin(0);
-
-    mainLayout->addWidget(_logWidget);
-
-    auto *toolLayout = new QHBoxLayout;
-    mainLayout->addLayout(toolLayout);
+    auto mainLayout = new QVBoxLayout;
+
+    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);
 
-    // Search input field
-    auto *lab = new QLabel(tr("&Search:") + " ");
-    _findTermEdit = new QLineEdit;
-    lab->setBuddy(_findTermEdit);
-    toolLayout->addWidget(lab);
-    toolLayout->addWidget(_findTermEdit);
-
-    // find button
-    auto *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);
 
     auto *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);
-
-    auto *showLogWindow = new QAction(this);
+    auto 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() = default;
 
-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);
index 7694b6311d563d612fc152a5618777ad8889f186..c49e26b73604e7dc4517ca1cd03c55db018d626f 100644 (file)
 
 namespace OCC {
 
-/**
- * @brief The LogWidget class
- * @ingroup gui
- */
-class LogWidget : public QPlainTextEdit
-{
-    Q_OBJECT
-public:
-    explicit LogWidget(QWidget *parent = nullptr);
-
-signals:
-};
-
 /**
  * @brief The LogBrowser class
  * @ingroup gui
@@ -53,29 +40,11 @@ public:
     explicit LogBrowser(QWidget *parent = nullptr);
     ~LogBrowser();
 
-    void setLogFile(const QString &, bool);
-
 protected:
-    void showEvent(QShowEvent *) override;
     void closeEvent(QCloseEvent *) 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
index a9df83a635f0859224d24512f326735b3a4da484..1b7b8791a6ab5b8a60f4602dfa2935283482210f 100644 (file)
@@ -121,7 +121,7 @@ void Logger::log(Log log)
 bool Logger::isNoop() const
 {
     QMutexLocker lock(&_mutex);
-    return !_logstream && !_logWindowActivated;
+    return !_logstream;
 }
 
 bool Logger::isLoggingToFile() const
@@ -163,12 +163,6 @@ void Logger::mirallLog(const QString &message)
     Logger::instance()->log(log_);
 }
 
-void Logger::setLogWindowActivated(bool activated)
-{
-    QMutexLocker locker(&_mutex);
-    _logWindowActivated = activated;
-}
-
 QString Logger::logFile() const
 {
     return _logFile.fileName();
index c8ff3bce845b10502396194eade1eaff04cebc93..4963e40fc06b39c91dab1901bac8e757d66c64b3 100644 (file)
@@ -58,8 +58,6 @@ public:
     void postOptionalGuiLog(const QString &title, const QString &message);
     void postGuiMessage(const QString &title, const QString &message);
 
-    void setLogWindowActivated(bool activated);
-
     QString logFile() const;
     void setLogFile(const QString &name);
 
@@ -104,7 +102,6 @@ private:
     ~Logger();
     QList<Log> _logs;
     bool _showTime = true;
-    bool _logWindowActivated = false;
     QFile _logFile;
     bool _doFileFlush = false;
     int _logExpire = 0;