Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ "hello world" Boost tee example program

The Boost C++ library has Function Template tee

The class templates tee_filter and tee_device provide two ways to split an output sequence so that all data is directed simultaneously to two different locations.

I am looking for a complete C++ example using Boost tee to output to standard out and to a file like "sample.txt".

like image 252
CW Holeman II Avatar asked Jun 16 '09 00:06

CW Holeman II


1 Answers

Here's an example using tee_filter I'm currently using to tee my Boost.Test output:

{ // init code, use static streams to keep them alive until test run process end

    using namespace boost::iostreams;
    static ofstream ofs("boost_test_output.log.xml"); // log file
    static tee_filter<ostream>  fileFilt(ofs); // tee all passed data to logfile

    // note derives from `boost::iostreams::output_filter`
    static text_xml_readability_filter xmlFilt; // filter all passed data, making the XML output readable

    static filtering_ostream filter; // master filter

    filter.push(fileFilt); // 1st, tee off any data to the file (raw boost XML)
    filter.push(xmlFilt);  // 2nd make the xml data stream readable (linebreaks, etc.)
    filter.push(cout);     // 3rd output the readable XML to cout

    boost::unit_test::unit_test_log.set_stream( filter );
}
like image 110
Martin Ba Avatar answered Sep 21 '22 15:09

Martin Ba