Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

Log output stream redirection

If you want to redirect the test log output stream into something different from the logger default output stream (usually std::cout, std::cerr or a file), use the following interface:

boost::unit_test::unit_test_log.set_stream( std::ostream& );

or for a particular log format:

boost::unit_test::unit_test_log.set_stream( boost::unit_test::output_format, std::ostream& );
[Tip] Tip

See boost::unit_test::unit_test_log_t::set_stream and boost::unit_test::output_format for more details

You can reset the output stream at any time both during the test module initialization and from within test cases. There are no limitations on number of output stream resets neither.

[Warning] Warning

If you redirect test log output stream from global fixture setup, you are required to reset it back to std::cout during teardown to prevent dangling references access

Example: Compile-time log output redirection

Code

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
#include <fstream>

struct MyConfig {
  MyConfig() : test_log( "example.log" ) {
    boost::unit_test::unit_test_log.set_stream( test_log );
  }
  ~MyConfig() {
    boost::unit_test::unit_test_log.set_stream( std::cout );
  }
  std::ofstream test_log;
};

BOOST_TEST_GLOBAL_CONFIGURATION( MyConfig );

BOOST_AUTO_TEST_CASE( test_case ) {
  BOOST_TEST( false );
}

Output

> example

*** 1 failures is detected in test suite "example"
> cat example.log
Running 1 test case...
test.cpp(26): error in "test_case": check false failed

PrevUpHomeNext