mirror of
https://github.com/robbert-vdh/yabridge.git
synced 2026-05-14 20:40:03 +02:00
Encapsulate the STDOUT/STDERR capturing
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
// 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/>.
|
||||
|
||||
#include "group.h"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
StdIoCapture::StdIoCapture(boost::asio::io_context& io_context,
|
||||
int file_descriptor)
|
||||
: pipe(io_context),
|
||||
target_fd(file_descriptor),
|
||||
original_fd_copy(dup(file_descriptor)) {
|
||||
// We'll use the second element of these two file descriptors to reopen
|
||||
// `file_descriptor`, and the first one to read the captured contents from
|
||||
::pipe(pipe_fd);
|
||||
|
||||
// We've already created a copy of the original file descriptor, so we can
|
||||
// reopen it using the newly created pipe
|
||||
dup2(pipe_fd[1], target_fd);
|
||||
close(pipe_fd[1]);
|
||||
|
||||
pipe.assign(pipe_fd[0]);
|
||||
}
|
||||
|
||||
StdIoCapture::~StdIoCapture() {
|
||||
// Restore the original file descriptor and close the pipe. The other wend
|
||||
// was already closed in the constructor.
|
||||
dup2(original_fd_copy, target_fd);
|
||||
close(original_fd_copy);
|
||||
close(pipe_fd[0]);
|
||||
}
|
||||
Reference in New Issue
Block a user