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 for the latest Boost documentation.
PrevUpHomeNext

Log output stream redirection

If you want to redirect the test log output stream into something different from std::cout use the following interface:

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

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 either.

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_GLOBAL_FIXTURE( 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(20): error in "test_case": check false failed
[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


PrevUpHomeNext