Files
telegraf-bcachefs-input/perf-collector/processframecapturer.cpp
T

55 lines
1.6 KiB
C++

#include "processframecapturer.h"
#include <QHttpServer>
#include <QDebug>
#include <QCoreApplication>
#include <unistd.h>
#include <sys/prctl.h>
#include <signal.h>
ProcessFrameCapturer::ProcessFrameCapturer(QHttpServer &server, const QString &pathPattern,
const QString &program, const QStringList &arguments,
QObject *parent) :
QObject{parent}
{
m_process.setProcessChannelMode(QProcess::MergedChannels);
m_process.setChildProcessModifier([] {
prctl(PR_SET_PDEATHSIG, SIGKILL);
});
connect(&m_process, &QProcess::started, this, &ProcessFrameCapturer::processStarted);
connect(&m_process, &QProcess::finished, this, &ProcessFrameCapturer::processFinished);
connect(&m_process, &QProcess::readyRead, this, &ProcessFrameCapturer::readyRead);
m_process.start(program, arguments);
server.route(pathPattern, this, &ProcessFrameCapturer::getProcessFrame);
}
void ProcessFrameCapturer::processStarted()
{
qDebug() << "started";
}
void ProcessFrameCapturer::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug() << "finished exitCode =" << exitCode << "exitStatus =" << exitStatus;
QCoreApplication::exit(exitCode);
}
void ProcessFrameCapturer::readyRead()
{
auto buf = m_process.readAll();
qDebug() << buf;
constexpr QByteArrayView search = "\x1B[2J\x1B[H";
const auto index = buf.lastIndexOf(search);
if (index != -1)
{
m_buffer.clear();
m_buffer.remove(0, index + search.size());
}
m_buffer.append(buf);
}