Slog2InfoRunner: Reuse TaskTree

Change-Id: I820814de90ddf0a3094ed35481c488d36b059c31
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: Marianne Yrjänä <marianne.yrjana@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Jarek Kobus
2022-11-01 16:39:06 +01:00
parent 6fb0bebba9
commit 46cfdbf285
2 changed files with 61 additions and 100 deletions

View File

@@ -27,40 +27,65 @@ Slog2InfoRunner::Slog2InfoRunner(RunControl *runControl)
// See QTCREATORBUG-10712 for details. // See QTCREATORBUG-10712 for details.
// We need to limit length of ApplicationId to 63 otherwise it would not match one in slog2info. // We need to limit length of ApplicationId to 63 otherwise it would not match one in slog2info.
m_applicationId.truncate(63); m_applicationId.truncate(63);
m_testProcess = new QtcProcess(this);
connect(m_testProcess, &QtcProcess::done, this, &Slog2InfoRunner::handleTestProcessCompleted);
m_launchDateTimeProcess = new QtcProcess(this);
connect(m_launchDateTimeProcess, &QtcProcess::done, this, &Slog2InfoRunner::launchSlog2Info);
m_logProcess = new QtcProcess(this);
connect(m_logProcess, &QtcProcess::readyReadStandardOutput, this, &Slog2InfoRunner::readLogStandardOutput);
connect(m_logProcess, &QtcProcess::readyReadStandardError, this, &Slog2InfoRunner::readLogStandardError);
connect(m_logProcess, &QtcProcess::done, this, &Slog2InfoRunner::handleLogDone);
}
void Slog2InfoRunner::printMissingWarning()
{
appendMessage(Tr::tr("Warning: \"slog2info\" is not found on the device, debug output not available."), ErrorMessageFormat);
} }
void Slog2InfoRunner::start() void Slog2InfoRunner::start()
{ {
m_testProcess->setCommand({device()->filePath("slog2info"), {}}); using namespace Utils::Tasking;
m_testProcess->start(); QTC_CHECK(!m_taskTree);
const auto testStartHandler = [this](QtcProcess &process) {
process.setCommand({device()->filePath("slog2info"), {}});
};
const auto testDoneHandler = [this](const QtcProcess &) {
m_found = true;
};
const auto testErrorHandler = [this](const QtcProcess &) {
QnxDevice::ConstPtr qnxDevice = device().dynamicCast<const QnxDevice>();
if (qnxDevice && qnxDevice->qnxVersion() > 0x060500) {
appendMessage(Tr::tr("Warning: \"slog2info\" is not found on the device, "
"debug output not available."), ErrorMessageFormat);
}
};
const auto launchTimeStartHandler = [this](QtcProcess &process) {
process.setCommand({device()->filePath("date"), "+\"%d %H:%M:%S\"", CommandLine::Raw});
};
const auto launchTimeDoneHandler = [this](const QtcProcess &process) {
QTC_CHECK(!m_applicationId.isEmpty());
QTC_CHECK(m_found);
m_launchDateTime = QDateTime::fromString(process.cleanedStdOut().trimmed(), "dd HH:mm:ss");
};
const auto logStartHandler = [this](QtcProcess &process) {
process.setCommand({device()->filePath("slog2info"), {"-w"}});
connect(&process, &QtcProcess::readyReadStandardOutput, this, [&] {
processLogInput(QString::fromLatin1(process.readAllStandardOutput()));
});
connect(&process, &QtcProcess::readyReadStandardError, this, [&] {
appendMessage(QString::fromLatin1(process.readAllStandardError()), StdErrFormat);
});
};
const auto logErrorHandler = [this](const QtcProcess &process) {
appendMessage(Tr::tr("Cannot show slog2info output. Error: %1").arg(process.errorString()),
StdErrFormat);
};
const Tasking::Group root {
Process(testStartHandler, testDoneHandler, testErrorHandler),
Process(launchTimeStartHandler, launchTimeDoneHandler),
Process(logStartHandler, {}, logErrorHandler)
};
m_taskTree.reset(new TaskTree(root));
m_taskTree->start();
reportStarted(); reportStarted();
} }
void Slog2InfoRunner::stop() void Slog2InfoRunner::stop()
{ {
if (m_testProcess->state() == QProcess::Running) m_taskTree.reset();
m_testProcess->kill(); processRemainingLogData();
if (m_logProcess->state() == QProcess::Running) {
m_logProcess->kill();
processLog(true);
}
reportStopped(); reportStopped();
} }
@@ -69,61 +94,21 @@ bool Slog2InfoRunner::commandFound() const
return m_found; return m_found;
} }
void Slog2InfoRunner::handleTestProcessCompleted() void Slog2InfoRunner::processRemainingLogData()
{ {
m_found = (m_testProcess->exitCode() == 0); if (!m_remainingData.isEmpty())
if (m_found) { processLogLine(m_remainingData);
readLaunchTime(); m_remainingData.clear();
} else {
QnxDevice::ConstPtr qnxDevice = device().dynamicCast<const QnxDevice>();
if (qnxDevice->qnxVersion() > 0x060500) {
printMissingWarning();
}
}
} }
void Slog2InfoRunner::readLaunchTime() void Slog2InfoRunner::processLogInput(const QString &input)
{ {
m_launchDateTimeProcess->setCommand({device()->filePath("date"),
"+\"%d %H:%M:%S\"", CommandLine::Raw});
m_launchDateTimeProcess->start();
}
void Slog2InfoRunner::launchSlog2Info()
{
QTC_CHECK(!m_applicationId.isEmpty());
QTC_CHECK(m_found);
if (m_logProcess->state() == QProcess::Running)
return;
if (m_launchDateTimeProcess->error() != QProcess::UnknownError)
return;
m_launchDateTime = QDateTime::fromString(QString::fromLatin1(m_launchDateTimeProcess->readAllStandardOutput()).trimmed(),
QString::fromLatin1("dd HH:mm:ss"));
m_logProcess->setCommand({device()->filePath("slog2info"), {"-w"}});
m_logProcess->start();
}
void Slog2InfoRunner::readLogStandardOutput()
{
processLog(false);
}
void Slog2InfoRunner::processLog(bool force)
{
QString input = QString::fromLatin1(m_logProcess->readAllStandardOutput());
QStringList lines = input.split(QLatin1Char('\n')); QStringList lines = input.split(QLatin1Char('\n'));
if (lines.isEmpty()) if (lines.isEmpty())
return; return;
lines.first().prepend(m_remainingData); lines.first().prepend(m_remainingData);
if (force)
m_remainingData.clear();
else
m_remainingData = lines.takeLast(); m_remainingData = lines.takeLast();
foreach (const QString &line, lines) for (const QString &line : std::as_const(lines))
processLogLine(line); processLogLine(line);
} }
@@ -166,18 +151,4 @@ void Slog2InfoRunner::processLogLine(const QString &line)
appendMessage(match.captured(6).trimmed() + '\n', StdOutFormat); appendMessage(match.captured(6).trimmed() + '\n', StdOutFormat);
} }
void Slog2InfoRunner::readLogStandardError()
{
appendMessage(QString::fromLatin1(m_logProcess->readAllStandardError()), StdErrFormat);
}
void Slog2InfoRunner::handleLogDone()
{
if (m_logProcess->error() == QProcess::UnknownError)
return;
appendMessage(Tr::tr("Cannot show slog2info output. Error: %1")
.arg(m_logProcess->errorString()), StdErrFormat);
}
} // Qnx::Internal } // Qnx::Internal

View File

@@ -7,7 +7,7 @@
#include <QDateTime> #include <QDateTime>
namespace Utils { class QtcProcess; } namespace Utils { class TaskTree; }
namespace Qnx::Internal { namespace Qnx::Internal {
@@ -22,16 +22,8 @@ public:
bool commandFound() const; bool commandFound() const;
private: private:
void handleTestProcessCompleted(); void processRemainingLogData();
void launchSlog2Info(); void processLogInput(const QString &input);
void readLogStandardOutput();
void readLogStandardError();
void handleLogDone();
void printMissingWarning();
void readLaunchTime();
void processLog(bool force);
void processLogLine(const QString &line); void processLogLine(const QString &line);
QString m_applicationId; QString m_applicationId;
@@ -41,9 +33,7 @@ private:
bool m_currentLogs = false; bool m_currentLogs = false;
QString m_remainingData; QString m_remainingData;
Utils::QtcProcess *m_launchDateTimeProcess = nullptr; std::unique_ptr<Utils::TaskTree> m_taskTree;
Utils::QtcProcess *m_testProcess = nullptr;
Utils::QtcProcess *m_logProcess = nullptr;
}; };
} // Qnx::Internal } // Qnx::Internal