From 1438cd7fb0710fb860e968be7ff6eb44f1314dda Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jan=20Kundr=C3=A1t?= Date: Thu, 16 Nov 2017 19:37:07 +0100 Subject: [PATCH] refactor: Untie "logging" away from its IMAP-specific roots Change-Id: I550c3da7590831f65db730258116ddecd670f17e --- src/Common/FileLogger.cpp | 6 +++--- src/Common/FileLogger.h | 4 ++-- src/Gui/ProtocolLoggerWidget.cpp | 22 +++++++++++----------- src/Gui/ProtocolLoggerWidget.h | 10 +++++----- src/Gui/Window.cpp | 2 +- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Common/FileLogger.cpp b/src/Common/FileLogger.cpp index bbd0a9d5..752bb727 100644 --- a/src/Common/FileLogger.cpp +++ b/src/Common/FileLogger.cpp @@ -65,12 +65,12 @@ void FileLogger::escapeCrLf(QString &s) .replace(QLatin1Char('\n'), 0x240a /* SYMBOL FOR LINE FEED */); } -void FileLogger::slotImapLogged(uint parser, Common::LogMessage message) +void FileLogger::log(uint connectionId, Common::LogMessage message) { if (!m_fileLog && !m_consoleLog) return; - QString formatted = formatMessage(parser, message); + QString formatted = formatMessage(connectionId, message); escapeCrLf(formatted); if (m_fileLog) { @@ -85,7 +85,7 @@ void FileLogger::slotImapLogged(uint parser, Common::LogMessage message) // Got to reformat the message message.truncatedBytes = message.message.size() - CUTOFF; message.message = message.message.left(CUTOFF); - formatted = formatMessage(parser, message); + formatted = formatMessage(connectionId, message); escapeCrLf(formatted); } qDebug() << formatted.toUtf8().constData() << "\n"; diff --git a/src/Common/FileLogger.h b/src/Common/FileLogger.h index 48b15cb7..f2f882ef 100644 --- a/src/Common/FileLogger.h +++ b/src/Common/FileLogger.h @@ -39,8 +39,8 @@ public: virtual ~FileLogger(); public slots: - /** @short An IMAP model wants to log something */ - void slotImapLogged(uint parser, Common::LogMessage message); + /** @short A connection handler wants to log something */ + void log(uint connectionId, Common::LogMessage message); /** @short Enable/disable persistent logging */ void setFileLogging(const bool enabled, const QString &fileName); diff --git a/src/Gui/ProtocolLoggerWidget.cpp b/src/Gui/ProtocolLoggerWidget.cpp index 4657843d..cf20cfdc 100644 --- a/src/Gui/ProtocolLoggerWidget.cpp +++ b/src/Gui/ProtocolLoggerWidget.cpp @@ -79,9 +79,9 @@ ProtocolLoggerWidget::~ProtocolLoggerWidget() { } -QPlainTextEdit *ProtocolLoggerWidget::getLogger(const uint parserId) +QPlainTextEdit *ProtocolLoggerWidget::getLogger(const uint connectionId) { - QPlainTextEdit *res = logs[parserId].widget; + QPlainTextEdit *res = logs[connectionId].widget; if (!res) { res = new QPlainTextEdit(); res->setLineWrapMode(QPlainTextEdit::NoWrap); @@ -95,8 +95,8 @@ QPlainTextEdit *ProtocolLoggerWidget::getLogger(const uint parserId) // to the very first value we throw at it, which might be a // grey one. res->appendHtml(QStringLiteral("

 

")); - tabs->addTab(res, tr("Connection %1").arg(parserId)); - logs[parserId].widget = res; + tabs->addTab(res, tr("Connection %1").arg(connectionId)); + logs[connectionId].widget = res; } return res; } @@ -142,12 +142,12 @@ void ProtocolLoggerWidget::hideEvent(QHideEvent *e) QWidget::hideEvent(e); } -void ProtocolLoggerWidget::slotImapLogged(uint parserId, Common::LogMessage message) +void ProtocolLoggerWidget::log(uint connectionId, Common::LogMessage message) { using namespace Common; if (m_fileLogger) { - m_fileLogger->slotImapLogged(parserId, message); + m_fileLogger->log(connectionId, message); } enum {CUTOFF=200}; if (message.message.size() > CUTOFF) { @@ -155,16 +155,16 @@ void ProtocolLoggerWidget::slotImapLogged(uint parserId, Common::LogMessage mess message.message = message.message.left(CUTOFF); } // we rely on the default constructor and QMap's behavior of operator[] to call it here - logs[parserId].buffer.append(message); + logs[connectionId].buffer.append(message); if (loggingActive && !delayedDisplay->isActive()) delayedDisplay->start(); } -void ProtocolLoggerWidget::flushToWidget(const uint parserId, Common::RingBuffer &buf) +void ProtocolLoggerWidget::flushToWidget(const uint connectionId, Common::RingBuffer &buf) { using namespace Common; - QPlainTextEdit *w = getLogger(parserId); + QPlainTextEdit *w = getLogger(connectionId); if (buf.skippedCount()) { w->appendHtml(tr("

%n message(s) were skipped because this widget was hidden.

", @@ -231,12 +231,12 @@ void ProtocolLoggerWidget::slotShowLogs() } /** @short Check whether some of the logs need cleaning */ -void ProtocolLoggerWidget::onConnectionClosed(uint parserId, Imap::ConnectionState state) +void ProtocolLoggerWidget::onConnectionClosed(uint connectionId, Imap::ConnectionState state) { if (state == Imap::CONN_STATE_LOGOUT) { auto now = QDateTime::currentMSecsSinceEpoch(); auto cutoff = now - 3 * 60 * 1000; // upon each disconnect, trash logs older than three minutes - auto it = logs.find(parserId); + auto it = logs.find(connectionId); if (it != logs.end()) { it->closedTime = now; } diff --git a/src/Gui/ProtocolLoggerWidget.h b/src/Gui/ProtocolLoggerWidget.h index 1028a541..11346c85 100644 --- a/src/Gui/ProtocolLoggerWidget.h +++ b/src/Gui/ProtocolLoggerWidget.h @@ -62,10 +62,10 @@ public: virtual ~ProtocolLoggerWidget(); public slots: - /** @short An IMAP model wants to log something */ - void slotImapLogged(uint parserId, Common::LogMessage message); + /** @short A protocol handler wants to log something */ + void log(uint connectionId, Common::LogMessage message); - void onConnectionClosed(uint parserId, Imap::ConnectionState state); + void onConnectionClosed(uint connectionId, Imap::ConnectionState state); /** @short Enable/disable persistent logging */ void slotSetPersistentLogging(const bool enabled); @@ -91,10 +91,10 @@ private: Common::FileLogger *m_fileLogger; /** @short Return (possibly newly created) logger widget for a given parser */ - QPlainTextEdit *getLogger(const uint parserId); + QPlainTextEdit *getLogger(const uint connectionId); /** @short Dump the log bufer contents to the GUI widget */ - void flushToWidget(const uint parserId, Common::RingBuffer &buf); + void flushToWidget(const uint connectionId, Common::RingBuffer &buf); virtual void showEvent(QShowEvent *e); virtual void hideEvent(QHideEvent *e); diff --git a/src/Gui/Window.cpp b/src/Gui/Window.cpp index d1e79ba6..3c38577a 100644 --- a/src/Gui/Window.cpp +++ b/src/Gui/Window.cpp @@ -918,7 +918,7 @@ void MainWindow::setupModels() connect(imapModel(), &Imap::Mailbox::Model::mailboxCreationFailed, this, &MainWindow::slotMailboxCreateFailed); connect(imapModel(), &Imap::Mailbox::Model::mailboxSyncFailed, this, &MainWindow::slotMailboxSyncFailed); - connect(imapModel(), &Imap::Mailbox::Model::logged, imapLogger, &ProtocolLoggerWidget::slotImapLogged); + connect(imapModel(), &Imap::Mailbox::Model::logged, imapLogger, &ProtocolLoggerWidget::log); connect(imapModel(), &Imap::Mailbox::Model::connectionStateChanged, imapLogger, &ProtocolLoggerWidget::onConnectionClosed); auto nw = qobject_cast(m_imapAccess->networkWatcher()); -- 2.11.4.GIT