Home > The Unit Test Framework > User's guide > Test Output > Test log > Compile time configuration
PrevNext

Compile time configuration

While many test log configuration tasks can be performed at runtime using predefined framework parameters, the UTF provides a compile time interface as well. The interface gives you full power over what, where and how to log. The interface is provided by singleton class boost::unit_test::unit_test_log_t and is accessible through local file scope reference to single instance of this class: boost::unit_test::unit_test_log.

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 30. Test log output redirection

#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_CHECK( false );
}

//____________________________________________________________________________//

Source code | Show output
> example

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

Log level configuration

If you need to enforce specific log level from within your test module use the following interface:

boost::unit_test::unit_test_log.set_threshold_level( boost::unit_test::log_level );

In regular circumstances you shouldn't use this interface, since you not only override default log level, but also the one supplied at test execution time. Prefer to use runtime parameters for log level selection.

Example 31. Compile time log level configuration

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
#include <boost/test/detail/unit_test_parameters.hpp>
using namespace boost::unit_test;

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test_case0 )
{
    if( runtime_config::log_level() < log_warnings )
        unit_test_log.set_threshold_level( log_warnings );

    BOOST_WARN( sizeof(int) > 4 );
}

//____________________________________________________________________________//

Source code | Show output
> example
Running 1 test case...
test.cpp(13): warning in "test_case0": condition sizeof(int) > 4 is not satisfied

*** No errors detected

Predefined log format selection

The select at compile time the log format from the list of the formats supplied by the UTF

boost::unit_test::unit_test_log.set_format( boost::unit_test::output_format );

In regular circumstances you shouldn't use this interface. Prefer to use runtime parameters for predefined log format selection.

Example 32. Compile time log format selection

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
using namespace boost::unit_test;

//____________________________________________________________________________//

struct MyConfig {
    MyConfig()  { unit_test_log.set_format( XML ); }
    ~MyConfig() {}
};

//____________________________________________________________________________//

BOOST_GLOBAL_FIXTURE( MyConfig );

BOOST_AUTO_TEST_CASE( test_case0 )
{
    BOOST_CHECK( false );
}

//____________________________________________________________________________//

Source code | Show output
> example --report_level=no
<TestLog><Error file="test.cpp" line="18">check false failed</Error></TestLog>

Custom log format support

PrevUpHomeNext