Use async_pipe for stderr reading

This commit is contained in:
badaix 2020-01-05 22:15:12 +01:00
parent ad05e87f6d
commit 32ca5144a5
2 changed files with 5 additions and 6 deletions

View file

@ -35,7 +35,8 @@ namespace streamreader
static constexpr auto LOG_TAG = "ProcessStream"; static constexpr auto LOG_TAG = "ProcessStream";
ProcessStream::ProcessStream(PcmListener* pcmListener, boost::asio::io_context& ioc, const StreamUri& uri) : PosixStream(pcmListener, ioc, uri) ProcessStream::ProcessStream(PcmListener* pcmListener, boost::asio::io_context& ioc, const StreamUri& uri)
: PosixStream(pcmListener, ioc, uri), pipe_stderr_(ioc)
{ {
params_ = uri_.getQuery("params"); params_ = uri_.getQuery("params");
wd_timeout_sec_ = cpt::stoul(uri_.getQuery("wd_timeout", "0")); wd_timeout_sec_ = cpt::stoul(uri_.getQuery("wd_timeout", "0"));
@ -104,14 +105,13 @@ void ProcessStream::do_connect()
LOG(DEBUG, LOG_TAG) << "Launching: '" << path_ + exe_ << "', with params: '" << params_ << "', in path: '" << path_ << "'\n"; LOG(DEBUG, LOG_TAG) << "Launching: '" << path_ + exe_ << "', with params: '" << params_ << "', in path: '" << path_ << "'\n";
pipe_stdout_ = bp::pipe(); pipe_stdout_ = bp::pipe();
pipe_stderr_ = bp::pipe(); pipe_stderr_ = bp::async_pipe(ioc_);
// stdout pipe should not block // stdout pipe should not block
int flags = fcntl(pipe_stdout_.native_source(), F_GETFL, 0); int flags = fcntl(pipe_stdout_.native_source(), F_GETFL, 0);
fcntl(pipe_stdout_.native_source(), F_SETFL, flags | O_NONBLOCK); fcntl(pipe_stdout_.native_source(), F_SETFL, flags | O_NONBLOCK);
process_ = bp::child(path_ + exe_ + " " + params_, bp::std_out > pipe_stdout_, bp::std_err > pipe_stderr_, bp::start_dir = path_); process_ = bp::child(path_ + exe_ + " " + params_, bp::std_out > pipe_stdout_, bp::std_err > pipe_stderr_, bp::start_dir = path_);
stream_ = make_unique<stream_descriptor>(ioc_, pipe_stdout_.native_source()); stream_ = make_unique<stream_descriptor>(ioc_, pipe_stdout_.native_source());
stream_stderr_ = make_unique<stream_descriptor>(ioc_, pipe_stderr_.native_source());
on_connect(); on_connect();
if (wd_timeout_sec_ > 0) if (wd_timeout_sec_ > 0)
{ {
@ -147,7 +147,7 @@ void ProcessStream::stderrReadLine()
const std::string delimiter = "\n"; const std::string delimiter = "\n";
auto self(shared_from_this()); auto self(shared_from_this());
boost::asio::async_read_until( boost::asio::async_read_until(
*stream_stderr_, streambuf_stderr_, delimiter, [this, self, delimiter](const std::error_code& ec, std::size_t bytes_transferred) { pipe_stderr_, streambuf_stderr_, delimiter, [this, self, delimiter](const std::error_code& ec, std::size_t bytes_transferred) {
if (ec) if (ec)
{ {
LOG(ERROR, LOG_TAG) << "Error while reading from stderr: " << ec.message() << "\n"; LOG(ERROR, LOG_TAG) << "Error while reading from stderr: " << ec.message() << "\n";

View file

@ -54,12 +54,11 @@ protected:
std::string path_; std::string path_;
std::string params_; std::string params_;
bp::pipe pipe_stdout_; bp::pipe pipe_stdout_;
bp::pipe pipe_stderr_; bp::async_pipe pipe_stderr_;
bp::child process_; bp::child process_;
bool logStderr_; bool logStderr_;
boost::asio::streambuf streambuf_stderr_; boost::asio::streambuf streambuf_stderr_;
std::unique_ptr<stream_descriptor> stream_stderr_;
// void worker() override; // void worker() override;
virtual void stderrReadLine(); virtual void stderrReadLine();