diff --git a/src/app/filelogger.cpp b/src/app/filelogger.cpp index 4a44f57f6..45d160b9f 100644 --- a/src/app/filelogger.cpp +++ b/src/app/filelogger.cpp @@ -66,10 +66,9 @@ FileLogger::~FileLogger() void FileLogger::changePath(const QString &newPath) { - QString tmpPath = Utils::Fs::fromNativePath(newPath); - QDir dir(tmpPath); - dir.mkpath(tmpPath); - tmpPath = dir.absoluteFilePath("qbittorrent.log"); + const QDir dir(newPath); + dir.mkpath(newPath); + const QString tmpPath = dir.absoluteFilePath("qbittorrent.log"); if (tmpPath != m_path) { m_path = tmpPath; @@ -87,8 +86,10 @@ void FileLogger::deleteOld(const int age, const FileLogAgeType ageType) { const QDateTime date = QDateTime::currentDateTime(); const QDir dir(Utils::Fs::branchPath(m_path)); + const QFileInfoList fileList = dir.entryInfoList(QStringList("qbittorrent.log.bak*") + , (QDir::Files | QDir::Writable), (QDir::Time | QDir::Reversed)); - for (const QFileInfo &file : asConst(dir.entryInfoList(QStringList("qbittorrent.log.bak*"), QDir::Files | QDir::Writable, QDir::Time | QDir::Reversed))) { + for (const QFileInfo &file : fileList) { QDateTime modificationDate = file.lastModified(); switch (ageType) { case DAYS: @@ -106,7 +107,7 @@ void FileLogger::deleteOld(const int age, const FileLogAgeType ageType) } } -void FileLogger::setBackup(bool value) +void FileLogger::setBackup(const bool value) { m_backup = value; } @@ -168,7 +169,7 @@ void FileLogger::openLogFile() || !m_logFile->setPermissions(QFile::ReadOwner | QFile::WriteOwner)) { delete m_logFile; m_logFile = nullptr; - Logger::instance()->addMessage(tr("An error occurred while trying to open the log file. Logging to file is disabled."), Log::CRITICAL); + LogMsg(tr("An error occurred while trying to open the log file. Logging to file is disabled."), Log::CRITICAL); } } diff --git a/src/gui/executionlogwidget.cpp b/src/gui/executionlogwidget.cpp index 8e0980c67..45c845976 100644 --- a/src/gui/executionlogwidget.cpp +++ b/src/gui/executionlogwidget.cpp @@ -40,7 +40,7 @@ ExecutionLogWidget::ExecutionLogWidget(QWidget *parent, const Log::MsgTypes &types) : QWidget(parent) , m_ui(new Ui::ExecutionLogWidget) - , m_msgList(new LogListWidget(MAX_LOG_MESSAGES, Log::MsgTypes(types))) + , m_msgList(new LogListWidget(MAX_LOG_MESSAGES, types)) , m_peerList(new LogListWidget(MAX_LOG_MESSAGES)) { m_ui->setupUi(this); @@ -75,38 +75,35 @@ void ExecutionLogWidget::showMsgTypes(const Log::MsgTypes &types) void ExecutionLogWidget::addLogMessage(const Log::Msg &msg) { - QString text; - QDateTime time = QDateTime::fromMSecsSinceEpoch(msg.timestamp); - QColor color; - + QString colorName; switch (msg.type) { case Log::INFO: - color.setNamedColor("blue"); + colorName = QLatin1String("blue"); break; case Log::WARNING: - color.setNamedColor("orange"); + colorName = QLatin1String("orange"); break; case Log::CRITICAL: - color.setNamedColor("red"); + colorName = QLatin1String("red"); break; default: - color = QApplication::palette().color(QPalette::WindowText); + colorName = QApplication::palette().color(QPalette::WindowText).name(); } - text = "" + time.toString(Qt::SystemLocaleShortDate) + " - " + msg.message + ""; + const QDateTime time = QDateTime::fromMSecsSinceEpoch(msg.timestamp); + const QString text = QString(QLatin1String("%1 - %3")) + .arg(time.toString(Qt::SystemLocaleShortDate), colorName, msg.message); m_msgList->appendLine(text, msg.type); } void ExecutionLogWidget::addPeerMessage(const Log::Peer &peer) { - QString text; - QDateTime time = QDateTime::fromMSecsSinceEpoch(peer.timestamp); - - if (peer.blocked) - text = "" + time.toString(Qt::SystemLocaleShortDate) + " - " - + tr("%1 was blocked %2", "x.y.z.w was blocked").arg(peer.ip, peer.reason); - else - text = "" + time.toString(Qt::SystemLocaleShortDate) + " - " + tr("%1 was banned", "x.y.z.w was banned").arg(peer.ip); + const QDateTime time = QDateTime::fromMSecsSinceEpoch(peer.timestamp); + const QString msg = QString(QLatin1String("%1 - %2")) + .arg(time.toString(Qt::SystemLocaleShortDate), peer.ip); + const QString text = peer.blocked + ? tr("%1 was blocked %2", "0.0.0.0 was blocked due to reason").arg(msg, peer.reason) + : tr("%1 was banned", "0.0.0.0 was banned").arg(msg); m_peerList->appendLine(text, Log::NORMAL); } diff --git a/src/gui/executionlogwidget.ui b/src/gui/executionlogwidget.ui index 1814e19ac..9de7af044 100644 --- a/src/gui/executionlogwidget.ui +++ b/src/gui/executionlogwidget.ui @@ -10,9 +10,6 @@ 300 - - Form - 0 diff --git a/src/gui/loglistwidget.cpp b/src/gui/loglistwidget.cpp index 6f02e42e8..cdb8172a2 100644 --- a/src/gui/loglistwidget.cpp +++ b/src/gui/loglistwidget.cpp @@ -39,7 +39,7 @@ #include "base/global.h" #include "guiiconprovider.h" -LogListWidget::LogListWidget(int maxLines, const Log::MsgTypes &types, QWidget *parent) +LogListWidget::LogListWidget(const int maxLines, const Log::MsgTypes &types, QWidget *parent) : QListWidget(parent) , m_maxLines(maxLines) , m_types(types) @@ -47,8 +47,8 @@ LogListWidget::LogListWidget(int maxLines, const Log::MsgTypes &types, QWidget * // Allow multiple selections setSelectionMode(QAbstractItemView::ExtendedSelection); // Context menu - QAction *copyAct = new QAction(GuiIconProvider::instance()->getIcon("edit-copy"), tr("Copy"), this); - QAction *clearAct = new QAction(GuiIconProvider::instance()->getIcon("edit-clear"), tr("Clear"), this); + auto *copyAct = new QAction(GuiIconProvider::instance()->getIcon("edit-copy"), tr("Copy"), this); + auto *clearAct = new QAction(GuiIconProvider::instance()->getIcon("edit-clear"), tr("Clear"), this); connect(copyAct, &QAction::triggered, this, &LogListWidget::copySelection); connect(clearAct, &QAction::triggered, this, &LogListWidget::clear); addAction(copyAct); @@ -78,10 +78,12 @@ void LogListWidget::keyPressEvent(QKeyEvent *event) void LogListWidget::appendLine(const QString &line, const Log::MsgType &type) { - auto *item = new QListWidgetItem; // We need to use QLabel here to support rich text - QLabel *lbl = new QLabel(line); + auto *lbl = new QLabel(line); + lbl->setTextFormat(Qt::RichText); lbl->setContentsMargins(4, 2, 4, 2); + + auto *item = new QListWidgetItem; item->setSizeHint(lbl->sizeHint()); item->setData(Qt::UserRole, type); insertItem(0, item); @@ -96,9 +98,10 @@ void LogListWidget::appendLine(const QString &line, const Log::MsgType &type) void LogListWidget::copySelection() { - static const QRegularExpression htmlTag("<[^>]+>"); + const QRegularExpression htmlTag("<[^>]+>"); + QStringList strings; - for (QListWidgetItem* it : asConst(selectedItems())) + for (QListWidgetItem *it : asConst(selectedItems())) strings << static_cast(itemWidget(it))->text().remove(htmlTag); QApplication::clipboard()->setText(strings.join('\n')); diff --git a/src/gui/loglistwidget.h b/src/gui/loglistwidget.h index c289ccc30..6dcb21d26 100644 --- a/src/gui/loglistwidget.h +++ b/src/gui/loglistwidget.h @@ -53,7 +53,7 @@ protected: void keyPressEvent(QKeyEvent *event) override; private: - int m_maxLines; + const int m_maxLines; Log::MsgTypes m_types; };