mirror of
https://github.com/robbert-vdh/yabridge.git
synced 2026-05-09 20:29:10 +02:00
Encapsulate the STDOUT/STDERR capturing
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
// yabridge: a Wine VST bridge
|
||||
// Copyright (C) 2020 Robbert van der Helm
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../boost-fix.h"
|
||||
|
||||
#include <boost/asio/posix/stream_descriptor.hpp>
|
||||
|
||||
/**
|
||||
* Encapsulate capturing the STDOUT or STDERR stream by opening a pipe and
|
||||
* reopening the passed file descriptor as one of the ends of the newly opened
|
||||
* pipe. This allows all output sent to be read from that pipe. This is needed
|
||||
* to capture all (debug) output from Wine and the hosted plugins so we can
|
||||
* prefix it with a timestamp and a group identifier and potentially write it to
|
||||
* a log file. Since the host application is run independently of the yabridge
|
||||
* instance that spawned it, this can't simply be done by the caller like we're
|
||||
* doing for Wine output in individually hosted plugins.
|
||||
*/
|
||||
class StdIoCapture {
|
||||
public:
|
||||
/**
|
||||
* Redirect all output sent to a file descriptor (e.g. `STDOUT_FILENO` or
|
||||
* `STDERR_FILENO`) to a pipe. `StdIoCapture::pipe` can be used to read from
|
||||
* this pipe.
|
||||
*
|
||||
* @param io_context The IO context to create the captured pipe stream on.
|
||||
* @param file_descriptor The file descriptor to remap.
|
||||
*/
|
||||
StdIoCapture(boost::asio::io_context& io_context, int file_descriptor);
|
||||
|
||||
StdIoCapture(const StdIoCapture&) = delete;
|
||||
StdIoCapture& operator=(const StdIoCapture&) = delete;
|
||||
|
||||
/**
|
||||
* On cleanup, close the outgoing file descriptor from the pipe and restore
|
||||
* the original file descriptor for the captured stream.
|
||||
*/
|
||||
~StdIoCapture();
|
||||
|
||||
/**
|
||||
* The pipe endpoint where all output from the original file descriptor gets
|
||||
* redirected to. This can be read from like any other `Boost.Asio` stream.
|
||||
*/
|
||||
boost::asio::posix::stream_descriptor pipe;
|
||||
|
||||
private:
|
||||
/**
|
||||
* The file descriptor of the stream we're capturing.
|
||||
*/
|
||||
const int target_fd;
|
||||
|
||||
/**
|
||||
* A copy of the original file descriptor. Will be used to undo
|
||||
* the capture when this object gets destroyed.
|
||||
*/
|
||||
const int original_fd_copy;
|
||||
|
||||
/**
|
||||
* The two file descriptors generated by the `pipe()` function call.
|
||||
* `pipe_fd[1]` is used to reopen/capture the passed file descriptor, and
|
||||
* `pipe_fd[0]` can be used to read the captured output from.
|
||||
*/
|
||||
int pipe_fd[2];
|
||||
};
|
||||
Reference in New Issue
Block a user