Skip to content

Commit

Permalink
log frameserver stderr in viewer console
Browse files Browse the repository at this point in the history
  • Loading branch information
rectalogic committed Oct 8, 2023
1 parent a9afc88 commit a58d740
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
23 changes: 16 additions & 7 deletions vfxpipe/vfxpipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

namespace VfxPipe {

std::tuple<int, uint32_t> spawnProcess(int* pipeReadStdout, int* pipeWriteStdin, const std::string& url, ErrorHandler errorHandler)
std::tuple<int, uint32_t> spawnProcess(int* pipeReadStdout, int* pipeWriteStdin, int* pipeReadStderr, const std::string& url, ErrorHandler errorHandler)
{
int fdsToChildStdin[2];
int fdsFromChildStdout[2];
int fdsFromChildStderr[2];

if (pipe(fdsToChildStdin) == -1 || pipe(fdsFromChildStdout) == -1) {
if (pipe(fdsToChildStdin) == -1 || pipe(fdsFromChildStdout) == -1 || (pipeReadStderr && pipe(fdsFromChildStderr) == -1)) {
errorHandler(std::string("vfxpipe pipe failed: ") + strerror(errno));
return { -1, 0 };
}
Expand All @@ -37,7 +38,8 @@ std::tuple<int, uint32_t> spawnProcess(int* pipeReadStdout, int* pipeWriteStdin,
// In the child
if (pid == 0) {
if (dup2(fdsToChildStdin[0], STDIN_FILENO) == -1
|| dup2(fdsFromChildStdout[1], STDOUT_FILENO) == -1) {
|| dup2(fdsFromChildStdout[1], STDOUT_FILENO) == -1
|| (pipeReadStderr && dup2(fdsFromChildStderr[1], STDERR_FILENO) == -1)) {
errorHandler(std::string("vfxpipe dup2 failed: ") + strerror(errno));
exit(1);
}
Expand All @@ -46,6 +48,10 @@ std::tuple<int, uint32_t> spawnProcess(int* pipeReadStdout, int* pipeWriteStdin,
close(fdsFromChildStdout[1]);
close(fdsToChildStdin[0]);
close(fdsToChildStdin[1]);
if (pipeReadStderr) {
close(fdsFromChildStderr[0]);
close(fdsFromChildStderr[1]);
}

Dl_info info;
auto fptr = &spawnProcess;
Expand All @@ -72,10 +78,13 @@ std::tuple<int, uint32_t> spawnProcess(int* pipeReadStdout, int* pipeWriteStdin,
// In the parent

*pipeWriteStdin = fdsToChildStdin[1];
close(fdsToChildStdin[0]);
*pipeReadStdout = fdsFromChildStdout[0];

close(fdsFromChildStdout[1]);
close(fdsToChildStdin[0]);
if (pipeReadStderr) {
*pipeReadStderr = fdsFromChildStderr[0];
close(fdsFromChildStderr[1]);
}

uint32_t sinkCount = 0;
if (!dataIO(*pipeReadStdout, reinterpret_cast<std::byte*>(&sinkCount), sizeof(sinkCount), read, errorHandler)) {
Expand All @@ -101,10 +110,10 @@ FrameServer::~FrameServer()
close(pipeWriteStdin);
}

bool FrameServer::initialize(ErrorHandler errorHandler)
bool FrameServer::initialize(ErrorHandler errorHandler, int* pipeReadStderr)
{
if (!pid) {
std::tie(pid, sinkCount) = spawnProcess(&pipeReadStdout, &pipeWriteStdin, url, errorHandler);
std::tie(pid, sinkCount) = spawnProcess(&pipeReadStdout, &pipeWriteStdin, pipeReadStderr, url, errorHandler);
if (pid == -1) {
errorHandler("vfxpipe failed to spawn process");
return false;
Expand Down
2 changes: 1 addition & 1 deletion vfxpipe/vfxpipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class FrameServer {
public:
FrameServer(const std::string& url);
~FrameServer();
bool initialize(ErrorHandler errorHandler);
bool initialize(ErrorHandler errorHandler, int* pipeReadStderr = nullptr);
bool renderFrame(double time, const std::vector<SourceVideoFrame>& sourceFrames, RenderedVideoFrame& outputFrame, ErrorHandler errorHandler);
std::string& getUrl() { return url; }
uint32_t getSinkCount() { return sinkCount; }
Expand Down
22 changes: 21 additions & 1 deletion viewer/viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <QScrollArea> // for QScrollArea
#include <QSize> // for QSize, operator!=
#include <QSlider> // for QSlider
#include <QSocketNotifier>
#include <QSpinBox> // for QSpinBox
#include <QStatusBar> // for QStatusBar
#include <QStringBuilder> // for operator%, QConcatenable<>::type, QStringBuilder
Expand All @@ -32,6 +33,7 @@
#include <algorithm> // for max
#include <cstddef> /* IWYU pragma: keep */ /* IWYU pragma: no_include <ext/type_traits> */ // for byte
#include <string> // for string
#include <unistd.h>
#include <vector> // for vector

void errorHandler(std::string msg)
Expand All @@ -44,6 +46,7 @@ Viewer::Viewer()
, sizeLabel(0)
, timeSpinBox(0)
, frameServer(0)
, errorNotifier(0)
{
setupUi(this);

Expand Down Expand Up @@ -81,6 +84,16 @@ void Viewer::messageHandler(QtMsgType type, const QMessageLogContext& context, c
logTextEdit->appendPlainText(result);
}

void Viewer::onErrorReadyRead(QSocketDescriptor fd, QSocketNotifier::Type)
{
char data[2048];
ssize_t n = read(fd, data, sizeof(data) - 1);
if (n > 0) {
data[n] = 0;
logTextEdit->appendPlainText(QString(data));
}
}

void Viewer::setContentUIEnabled(bool enable)
{
timeSpinBox->setEnabled(enable);
Expand Down Expand Up @@ -162,7 +175,9 @@ void Viewer::handleResize()

void Viewer::on_timeSlider_valueChanged(int value)
{
timeSpinBox->blockSignals(true);
timeSpinBox->setValue(sliderTimeValue(value));
timeSpinBox->blockSignals(false);
renderContent();
}

Expand Down Expand Up @@ -223,8 +238,13 @@ void Viewer::createContent(const QString& fileName)

logTextEdit->clear();

if (frameServer->initialize(errorHandler))
delete errorNotifier;
int pipeReadStderr = 0;
if (frameServer->initialize(errorHandler, &pipeReadStderr)) {
errorNotifier = new QSocketNotifier(pipeReadStderr, QSocketNotifier::Read, this);
connect(errorNotifier, &QSocketNotifier::activated, this, &Viewer::onErrorReadyRead);
setContentUIEnabled(true);
}

setupImages(frameServer->getSinkCount(), scrollArea->widget()->size());
renderContent();
Expand Down
4 changes: 4 additions & 0 deletions viewer/viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#include <QMainWindow> // for QMainWindow
#include <QMessageLogContext> // for QtMsgType
#include <QObject> // for Q_OBJECT, slots
#include <QSocketNotifier>
#include <QString> // for QString
class QDoubleSpinBox;
class QLabel;
class QSize;

namespace VfxPipe {
class FrameServer;
}
Expand All @@ -34,6 +36,7 @@ private slots:
void on_addParameterButton_clicked();
void on_deleteParameterButton_clicked();
void onImageChanged();
void onErrorReadyRead(QSocketDescriptor, QSocketNotifier::Type);

private:
void createContent(const QString& fileName);
Expand All @@ -47,4 +50,5 @@ private slots:
QLabel* imageLabel;
QImage renderImage;
VfxPipe::FrameServer* frameServer;
QSocketNotifier* errorNotifier;
};

0 comments on commit a58d740

Please sign in to comment.