[v2][Windows] Cannot redirect stdout to a file

Open
#412 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp

Research direction

Start by reproducing the provided execProc example on Windows with Boost 1.86, comparing process_stdio output to the basic_stream_file target and to a readable_pipe. Trace the process v2 stdout-redirection entry point used by process_stdio; done means the created output file contains the child process's stdout after the process exits.

Written by the indexing model from the issue text.

Description

I am using boost process v2 from the 1.86 version of the boost library.

The following code is intended to redirect stdout to a file. It creates the file but the contents of stdout are not written to it; the file size is zero. When I redirect stdout to a pipe, the pipe contains the expected result.

I guessed at how to write the code to redirect stdout to a file, as I could not find any example in the documentation or online. So I have probably not done it right.

#include <boost/process/v2.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/readable_pipe.hpp>
#include <boost/asio/basic_stream_file.hpp>

namespace proc   = boost::process::v2;
namespace asio   = boost::asio;

std::tuple<int, std::string> execProc(const std::string& exePath, const std::vector<std::string>& args,
    const std::string& outputFilePath)
{
    asio::io_context ctx;

    asio::basic_stream_file<asio::any_io_executor> outStream(ctx, outputFilePath.c_str(), 
        asio::file_base::write_only | asio::file_base::create | asio::file_base::exclusive);

    asio::readable_pipe errPipe{ctx};

    proc::process proc{ ctx, exePath, args, proc::process_stdio{{}, outStream, errPipe}};

    std::string error;
    boost::system::error_code ec;
    auto errLen = asio::read(errPipe, asio::dynamic_buffer(error), ec);
    auto exitCode = proc.wait();
    outStream.close();

    return std::make_tuple(exitCode, std::move(error));
}

Dominant language
C++
Stars
145
Forks
151
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/process

All issues in boostorg/process

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.