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.
雖然很多測試日誌配置任務可以在運行時使用框架預定義的參數進行,但 UTF 同樣提供了編譯時接口。 通過接口可以日誌的內容、位置、方式。 接口通過單例類 boost::unit_test::unit_test_log_t 提供,並可在文件局部範圍內通過指向類 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:
如果想要將測試日誌輸出流重定向到非 std::cout 的其它地方,使用如下接口:

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