title: How to capture standard out and print to both the console and a file during process runtime (C++/Boost.Process)
body: <p>I am working on an application that starts child processes using Boost's C++ Process library
  (<a href="http://www.highscore.de/boost/process0.5" rel="nofollow">http://www.highscore.de/boost/process0.5</a>)
  and redirects the standard out of that process to a file using the below code:
      <code>boost::process::initializers::bind_stdout(boost::iostreams::file_descriptor_sink</code> goes here <code>)</code></p>
  
  <p>The above solution and code worked well.</p>
  
  <p>However, now, I need to be able to print the child process's standard out to both a file and the console.</p>
  
  <p>I have found the code below which seems that it should do the job:</p>
  
  <pre><code>#include &lt;boost/process.hpp&gt;
  #include &lt;boost/iostreams/device/file_descriptor.hpp&gt;
  #include &lt;boost/iostreams/stream.hpp&gt;
  #include &lt;iostream&gt;
  
  using namespace boost::process;
  using namespace boost::process::initializers;
  using namespace boost::iostreams;
  
  int main()
  {
      boost::process::pipe p = create_pipe();
      {
          file_descriptor_sink sink(p.sink, close_handle);
          execute(
              run_exe("/bin/echo"),
              set_cmd_line("echo hello"),
              bind_stdout(sink)
          );
      }
      file_descriptor_source source(p.source, close_handle);
      stream&lt;file_descriptor_source&gt; is(source);
      std::string s;
      while (std::getline(is, s))
      {
              std::cout &lt;&lt; s &lt;&lt; std::endl;
              //will write line by line to file here
      }
  }
  </code></pre>
  
  <p>I have tried running the above code and indeed I was able to print the output to the console and a file.
  But there is one major issue with the above code (at least in my case),
  the while loop (<code>while (std::getline(is, s))</code>) only starts after the invoked process has exited, which means that all the output that was accumulated during the process run is printed at once once the process finishes.</p>
  
  <p>Now,
  What I need is to be able to get the output from the invoked process <strong>during it's runtime</strong> and print it to both the console and the file <strong>as it comes</strong> (rather than only at the end).</p>
  
  <p>(Since the processes that I start are out of my control and can take even a few good minutes to run, and currently the screen console is just empty until the process exits).</p>
  
  <p>I have went over a lot of websites trying to find a solution for this one, as well as playing a lot with the code, but I can't seem to nail the solution yet.</p>
  
  <p>How can this be accomplished? (Is it even possible with Boost.Process?)
  How would I be able to make that string/stream populate with the std out of the child process during it's runtime rather that only when it's finished? </p>