c++boost-logboost-unit-test-framework

Can I access the stream instance used by BOOST_LOG_TRIVIAL?


Is it possible to access the underlying stream instance used by BOOST_LOG_TRIVIAL?

I'm trying to have BOOST unit test framework write output using BOOST_LOG_TRIVIAL (which I have configured to write in a file and std::clog)

auto& log_stream = ??? // BOOST_LOG_TRIVIAL stream instance boost::unit_test::unit_test_log.instance().set_stream(log_stream);


Solution

  • Is it possible to access the underlying stream instance used by BOOST_LOG_TRIVIAL?

    No, it's not. Internally, the default sink in Boost.Log, which is used by BOOST_LOG_TRIVIAL unless you configured your own sink, does not even use a stream.

    I think, the best way to integrate Boost.Test with Boost.Log is to implement your own stream buffer (a class derived from std::streambuf). The buffer would have to convert the output from Boost.Test into separate log records (e.g. by splitting it at newline characters) and pass the records to Boost.Log via BOOST_LOG_TRIVIAL or other means. You can then create a std::ostream object referring to your stream buffer and pass it to Boost.Test into set_stream.