From b338d7aa3d1b8fda55b6e896828363f1f3236d14 Mon Sep 17 00:00:00 2001 From: Guy Scharf Date: Sun, 16 Aug 2020 12:46:36 -0700 Subject: [PATCH] Check for ability to write to data directory at startup Show critical error message and exit OSCAR if unable to write to data directory Improve qWarning message in logger.cpp Present warning dialog if logger cannot write to disk --- oscar/logger.cpp | 8 +++++--- oscar/logger.h | 2 +- oscar/main.cpp | 23 +++++++++++++++++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/oscar/logger.cpp b/oscar/logger.cpp index 0d06d0ba..6594f3e6 100644 --- a/oscar/logger.cpp +++ b/oscar/logger.cpp @@ -87,11 +87,11 @@ void LogThread::connectionReady() qDebug() << "Logging UI initialized"; } -void LogThread::logToFile() +bool LogThread::logToFile() { if (m_logStream) { qWarning().noquote() << "Already logging to" << m_logFile->fileName(); - return; + return true; } QString debugLog = GetLogDir() + "/debug.txt"; @@ -109,8 +109,10 @@ void LogThread::logToFile() if (m_logStream) { qDebug().noquote() << "Logging to" << debugLog; } else { - qWarning().noquote() << "Unable to open" << debugLog; + qWarning().noquote() << "Could not open" << debugLog << "error code" << m_logFile->error() << m_logFile->errorString(); + return false; } + return true; } LogThread::~LogThread() diff --git a/oscar/logger.h b/oscar/logger.h index 2f4a1803..267fe3f8 100644 --- a/oscar/logger.h +++ b/oscar/logger.h @@ -29,7 +29,7 @@ public: void appendClean(QString msg); bool isRunning() { return running; } void connectionReady(); - void logToFile(); + bool logToFile(); QString logFileName(); void quit(); diff --git a/oscar/main.cpp b/oscar/main.cpp index 58fb0e75..5c0941fd 100644 --- a/oscar/main.cpp +++ b/oscar/main.cpp @@ -379,7 +379,7 @@ int main(int argc, char *argv[]) { initializeLogger(); // After initializing the logger, any qDebug() messages will be queued but not written to console // until MainWindow is constructed below. In spite of that, we initialize the logger here so that - // the intervening messages to show up in the debug pane. + // the intervening messages show up in the debug pane. // // The only time this is really noticeable is when initTranslations() presents its language // selection QDialog, which waits indefinitely for user input before MainWindow is constructed. @@ -555,8 +555,27 @@ int main(int argc, char *argv[]) { return 0; } + // Make sure we can write to the data directory + QFile testFile(GetAppData()+"/testfile.txt"); + if (testFile.exists()) + testFile.remove(); + if (!testFile.open(QFile::ReadWrite)) { + QString errMsg = QObject::tr("Unable to write to OSCAR data directory") + "\n" + + GetAppData() + "\n" + + QObject::tr("Error code") + ": " + QString::number(testFile.error()) + " - " + testFile.errorString() + "\n\n" + + QObject::tr("OSCAR cannot continue and is exiting.") + "\n"; + qCritical() << errMsg; + QMessageBox::critical(nullptr, QObject::tr("Exiting"), errMsg); + return 0; + } + else + testFile.remove(); + // Begin logging to file now that there's a data folder. - logger->logToFile(); + if (!logger->logToFile()) { + QMessageBox::warning(nullptr, STR_MessageBox_Warning, + QObject::tr("Unable to write to debug log. You can still use the debug pane (Help/Troubleshooting/Show Debug Pane) but the debug log will not be written to disk.")); + } /////////////////////////////////////////////////////////////////////////////////////////// // Initialize preferences system (Don't use p_pref before this point!)