Report failed sub-process exit statuses then exit gracefully

This commit is contained in:
Bill Somerville 2021-05-07 01:14:51 +01:00
parent 1d6835b859
commit 98d4f771c2
No known key found for this signature in database
GPG Key ID: D864B06D1E81618F
2 changed files with 38 additions and 19 deletions

View File

@ -1,6 +1,7 @@
//------------------------------------------------------------------ MainWindow
#include "mainwindow.h"
#include "revision_utils.hpp"
#include "widgets/MessageBox.hpp"
#include "ui_mainwindow.h"
#include "devsetup.h"
#include "plotter.h"
@ -101,14 +102,15 @@ MainWindow::MainWindow(QWidget *parent) :
SLOT(showStatusMessage(QString)));
createStatusBar();
connect(&proc_m65, SIGNAL(readyReadStandardOutput()),
this, SLOT(readFromStdout()));
connect(&proc_m65, SIGNAL(error(QProcess::ProcessError)),
this, SLOT(m65_error()));
connect(&proc_m65, SIGNAL(readyReadStandardError()),
this, SLOT(readFromStderr()));
connect(&proc_m65, SIGNAL(readyReadStandardOutput()), this, SLOT(readFromStdout()));
connect(&proc_m65, &QProcess::errorOccurred, this, &MainWindow::m65_error);
connect(&proc_m65, static_cast<void (QProcess::*) (int, QProcess::ExitStatus)> (&QProcess::finished),
[this] (int exitCode, QProcess::ExitStatus status) {
if (subProcessFailed (&proc_m65, exitCode, status))
{
QTimer::singleShot (0, this, SLOT (close ()));
}
});
connect(&proc_editor, SIGNAL(error(QProcess::ProcessError)),
this, SLOT(editor_error()));
@ -1341,11 +1343,33 @@ void MainWindow::decode() //decode()
decodeBusy(true);
}
void MainWindow::m65_error() //m65_error
bool MainWindow::subProcessFailed (QProcess * process, int exit_code, QProcess::ExitStatus status)
{
if (exit_code || QProcess::NormalExit != status)
{
QStringList arguments;
for (auto argument: process->arguments ())
{
if (argument.contains (' ')) argument = '"' + argument + '"';
arguments << argument;
}
MessageBox::critical_message (this, tr ("Subprocess Error")
, tr ("Subprocess failed with exit code %1")
.arg (exit_code)
, tr ("Running: %1\n%2")
.arg (process->program () + ' ' + arguments.join (' '))
.arg (QString {process->readAllStandardError()}));
return true;
}
return false;
}
void MainWindow::m65_error (QProcess::ProcessError)
{
if(!m_killAll) {
msgBox("Error starting or running\n" + m_appDir + "/m65 -s");
exit(1);
msgBox("Error starting or running\n" + m_appDir + "/m65 -s\n\n"
+ proc_m65.errorString ());
QTimer::singleShot (0, this, SLOT (close ()));
}
}
@ -1356,12 +1380,6 @@ void MainWindow::editor_error() //editor_error
}
}
void MainWindow::readFromStderr() //readFromStderr
{
QByteArray t=proc_m65.readAllStandardError();
msgBox(t);
}
void MainWindow::readFromStdout() //readFromStdout
{
while(proc_m65.canReadLine())

View File

@ -6,6 +6,7 @@
#include <QTimer>
#include <QDateTime>
#include <QHash>
#include <QProcess>
#include "getfile.h"
#include "soundin.h"
#include "soundout.h"
@ -39,8 +40,7 @@ public slots:
void diskWriteFinished();
void freezeDecode(int n);
void readFromStdout();
void readFromStderr();
void m65_error();
void m65_error (QProcess::ProcessError);
void editor_error();
void guiUpdate();
void doubleClickOnCall(QString hiscall, bool ctrl);
@ -277,6 +277,7 @@ private:
void ba2msg(QByteArray ba, char* message);
void msgtype(QString t, QLineEdit* tx);
void stub();
bool subProcessFailed (QProcess *, int exit_code, QProcess::ExitStatus);
};
extern void getfile(QString fname, bool xpol, int idInt);