From 7316ac676c693af6c5653ee021abee04e1487457 Mon Sep 17 00:00:00 2001 From: sawinglogz <3787776-sawinglogz@users.noreply.gitlab.com> Date: Tue, 21 Jul 2020 13:21:29 -0400 Subject: [PATCH] Add a menu item to create a zip of all diagnostic logs. Also fix a buffering issue in XmlRecorder that left devices.xml empty in the zip. --- oscar/SleepLib/xmlreplay.cpp | 9 +++++++ oscar/SleepLib/xmlreplay.h | 1 + oscar/mainwindow.cpp | 50 ++++++++++++++++++++++++++++++++++++ oscar/mainwindow.h | 2 ++ oscar/mainwindow.ui | 6 +++++ 5 files changed, 68 insertions(+) diff --git a/oscar/SleepLib/xmlreplay.cpp b/oscar/SleepLib/xmlreplay.cpp index 6782d018..cd121dcd 100644 --- a/oscar/SleepLib/xmlreplay.cpp +++ b/oscar/SleepLib/xmlreplay.cpp @@ -116,6 +116,14 @@ void XmlRecorder::epilogue() m_xml->writeEndElement(); // close enclosing tag } +void XmlRecorder::flush() +{ + if (m_file) { + if (!m_file->flush()) { + qWarning().noquote() << "Unable to flush XML to" << m_file->fileName(); + } + } +} XmlReplay::XmlReplay(QFile* file, const QString & tag) : m_tag(tag), m_file(file), m_pendingSignal(nullptr), m_parent(nullptr) @@ -416,6 +424,7 @@ void XmlReplayEvent::record(XmlRecorder* writer) const if (writer != nullptr) { writer->lock(); writer->xml() << *this; + writer->flush(); writer->unlock(); } } diff --git a/oscar/SleepLib/xmlreplay.h b/oscar/SleepLib/xmlreplay.h index 81fd12a9..5c784cd9 100644 --- a/oscar/SleepLib/xmlreplay.h +++ b/oscar/SleepLib/xmlreplay.h @@ -54,6 +54,7 @@ public: inline QXmlStreamWriter & xml() { return *m_xml; } inline void lock() { m_mutex.lock(); } inline void unlock() { m_mutex.unlock(); } + void flush(); protected: XmlRecorder(XmlRecorder* parent, const QString & id, const QString & tag); // constructor used by substreams diff --git a/oscar/mainwindow.cpp b/oscar/mainwindow.cpp index b33b291b..c330304d 100644 --- a/oscar/mainwindow.cpp +++ b/oscar/mainwindow.cpp @@ -2727,6 +2727,56 @@ void MainWindow::on_actionCreate_Card_zip_triggered() } } + +void MainWindow::on_actionCreate_Log_zip_triggered() +{ + QString folder; + + // Note: macOS ignores this and points to OSCAR's most recently used directory for saving. + folder = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); + folder += "/OSCAR-logs.zip"; + QString filename = QFileDialog::getSaveFileName(this, tr("Choose where to save zip"), folder, tr("ZIP files (*.zip)")); + if (filename.isEmpty()) { + return; // aborted + } + if (!filename.toLower().endsWith(".zip")) { + filename += ".zip"; + } + + qDebug() << "Create zip of OSCAR diagnostic logs:" << filename; + + ZipFile z; + bool ok = z.Open(filename); + if (ok) { + ProgressDialog * prog = new ProgressDialog(this); + prog->setMessage(tr("Creating zip...")); + + // Build the list of files. + FileQueue files; + files.AddDirectory(GetLogDir(), "logs"); + + // Defer the current debug log to the end. + QString debugLog = logger->logFileName(); + QString debugLogZipName; + int exists = files.Remove(debugLog, &debugLogZipName); + if (exists) { + files.AddFile(debugLog, debugLogZipName); + } + + // Create the zip. + ok = z.AddFiles(files, prog); + z.Close(); + } else { + qWarning() << "Unable to open" << filename; + } + if (!ok) { + QMessageBox::warning(nullptr, STR_MessageBox_Error, + QObject::tr("Unable to create zip!"), + QMessageBox::Ok); + } +} + + void MainWindow::on_actionCreate_OSCAR_Data_zip_triggered() { QString folder; diff --git a/oscar/mainwindow.h b/oscar/mainwindow.h index 0f389833..93834d6b 100644 --- a/oscar/mainwindow.h +++ b/oscar/mainwindow.h @@ -344,6 +344,8 @@ class MainWindow : public QMainWindow void on_actionCreate_Card_zip_triggered(); + void on_actionCreate_Log_zip_triggered(); + void on_actionCreate_OSCAR_Data_zip_triggered(); void on_actionReport_a_Bug_triggered(); diff --git a/oscar/mainwindow.ui b/oscar/mainwindow.ui index df56f053..9b797bae 100644 --- a/oscar/mainwindow.ui +++ b/oscar/mainwindow.ui @@ -2866,6 +2866,7 @@ p, li { white-space: pre-wrap; } + @@ -3232,6 +3233,11 @@ p, li { white-space: pre-wrap; } Create zip of CPAP data card + + + Create zip of OSCAR diagnostic logs + + Create zip of all OSCAR data