mardi 3 août 2021

Boost 1.71.0: How to get process output?

I had code like this that worked fine on Ubuntu 18.04 and with Boost 1.65.0:

// See https://www.boost.org/doc/libs/1_65_0/doc/html/boost_process/tutorial.html
std::pair<int, std::string> runCommandAndGetOutput(const std::string & cmd, const std::vector<std::string> & args)
{
  namespace bp = boost::process;
  boost::asio::io_service ios;
  std::future<std::string> data;
  bp::child c(cmd, bp::args(args), bp::std_in.close(), bp::std_out > data, bp::std_err > bp::null, ios);
  ios.run();
  if (c.exit_code()) {
    std::cerr << "Command '" << cmd << "' failed with return value: " << c.exit_code();
  }
  return { c.exit_code(), data.get() };
}

However, after upgrading to Ubuntu 20.04 and Boost 1.71.0 this doesn't compile anymore because it seems that boost::asio::io_service is deprecated and doesn't exist anymore.

After googling I realized that I must use boost::asio::io_context instead.

Ok:

std::pair<int, std::string> runCommandAndGetOutput(const std::string & cmd, const std::vector<std::string> & args)
{
  namespace bp = boost::process;
  boost::asio::io_context ioc;
  std::future<std::string> data;
  bp::child c(cmd, bp::args(args), bp::std_in.close(), bp::std_out > data, bp::std_err > bp::null, ioc);
  ioc.run();
  if (c.exit_code()) {
    std::cerr << "Command '" << cmd << "' failed with return value: " << c.exit_code();
  }
  return { c.exit_code(), data.get() };
}

This compiles, but just doesn't work. Trying to run e.g. /usr/bin/free returns exit code 383 that doesn't make any sense.

What makes this difficult is that the documentation of Boost 1.71.0 is still using io_service:

https://www.boost.org/doc/libs/1_71_0/doc/html/boost_process/tutorial.html

Does anyone know how this should be done right?

Aucun commentaire:

Enregistrer un commentaire