[pim/trojita] src: Check that QFile is open before writing
Espen Sandøy Hustad <[email protected]>
| Newsgroups | gmane.comp.kde.cvs |
|---|---|
| Message-ID | <[email protected]> |
Git commit 7ce96cc4b667aa53d7d92b385cb715361e2a1997 by Espen Sandøy Hustad.
Committed on 03/08/2026 at 19:02.
Pushed by ehustad into branch 'master'.
Check that QFile is open before writing
Show an error message if opening failed, and reset
the menu checkbox.
Fix src/Common/FileLogger.cpp:44:9: warning: ignoring return
value of function declared with 'nodiscard' attribute [-Wunused-result]
M +9 -1 src/Common/FileLogger.cpp
M +14 -7 src/Gui/ProtocolLoggerWidget.cpp
https://invent.kde.org/pim/trojita/-/commit/7ce96cc4b667aa53d7d92b385cb715361e2a1997
diff --git a/src/Common/FileLogger.cpp b/src/Common/FileLogger.cpp
index 5490f8742..6d45a7571 100644
--- a/src/Common/FileLogger.cpp
+++ b/src/Common/FileLogger.cpp
@@ -23,6 +23,7 @@
#include <QDateTime>
#include <QDebug>
#include <QFile>
+#include <stdexcept>
#include "FileLogger.h"
namespace Common
@@ -45,7 +46,14 @@ void FileLogger::setFileLogging(const bool enabled, const QString &fileName)
}
auto logFile = std::make_unique<QFile>(fileName, nullptr);
- logFile->open(QIODevice::Truncate | QIODevice::WriteOnly);
+ if (!logFile->open(QIODevice::Truncate | QIODevice::WriteOnly)) {
+ //: Translators: %1 is the filename of the logfile
+ //: %2 is the detailed error description from Qt, ready for human consumption
+ throw std::runtime_error(tr("Failed to open logfile \"%1\" for writing: %2")
+ .arg(fileName, logFile->errorString()).toStdString());
+ return;
+ }
+
m_fileLog = LogFilePtr(new QTextStream(logFile.release()));
}
diff --git a/src/Gui/ProtocolLoggerWidget.cpp b/src/Gui/ProtocolLoggerWidget.cpp
index c470ac22d..a65237f7c 100644
--- a/src/Gui/ProtocolLoggerWidget.cpp
+++ b/src/Gui/ProtocolLoggerWidget.cpp
@@ -29,6 +29,7 @@
#include <QTimer>
#include <QVBoxLayout>
#include "ProtocolLoggerWidget.h"
+#include "Util.h"
#include "Common/FileLogger.h"
#include "Imap/Model/Utils.h"
@@ -62,15 +63,21 @@ void ProtocolLoggerWidget::slotSetPersistentLogging(const bool enabled)
{
if (enabled == !!m_fileLogger)
return;
-
- if (enabled) {
- Q_ASSERT(!m_fileLogger);
- m_fileLogger.reset(new Common::FileLogger(nullptr));
- m_fileLogger->setFileLogging(true, Imap::Mailbox::persistentLogFileName());
- m_fileLogger->setAutoFlush(true);
- } else {
+ try {
+ if (enabled) {
+ Q_ASSERT(!m_fileLogger);
+ m_fileLogger.reset(new Common::FileLogger(nullptr));
+ m_fileLogger->setFileLogging(true, Imap::Mailbox::persistentLogFileName());
+ m_fileLogger->setAutoFlush(true);
+ } else {
+ m_fileLogger.reset();
+ }
+ }
+ catch (std::runtime_error &err) {
+ Util::messageBoxCritical(this, tr("Failed to open file"), QString::fromStdString(err.what()));
m_fileLogger.reset();
}
+
emit persistentLoggingChanged(!!m_fileLogger);
}