1 // (C) Copyright Gennadiy Rozental 2001.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/test for the library home page.
10 // Version : $Revision$
12 // Description : implemets Unit Test Log
13 // ***************************************************************************
15 #ifndef BOOST_TEST_UNIT_TEST_LOG_IPP_012205GER
16 #define BOOST_TEST_UNIT_TEST_LOG_IPP_012205GER
19 #include <boost/test/unit_test_log.hpp>
20 #include <boost/test/unit_test_log_formatter.hpp>
21 #include <boost/test/execution_monitor.hpp>
22 #include <boost/test/framework.hpp>
23 #include <boost/test/unit_test_parameters.hpp>
25 #include <boost/test/utils/basic_cstring/compare.hpp>
26 #include <boost/test/utils/foreach.hpp>
28 #include <boost/test/output/compiler_log_formatter.hpp>
29 #include <boost/test/output/xml_log_formatter.hpp>
30 #include <boost/test/output/junit_log_formatter.hpp>
33 #include <boost/shared_ptr.hpp>
34 #include <boost/io/ios_state.hpp>
35 typedef ::boost::io::ios_base_all_saver io_saver_type;
37 #include <boost/test/detail/suppress_warnings.hpp>
39 //____________________________________________________________________________//
44 // ************************************************************************** //
45 // ************** entry_value_collector ************** //
46 // ************************************************************************** //
50 entry_value_collector const&
51 entry_value_collector::operator<<( lazy_ostream const& v ) const
58 //____________________________________________________________________________//
60 entry_value_collector const&
61 entry_value_collector::operator<<( const_string v ) const
68 //____________________________________________________________________________//
70 entry_value_collector::~entry_value_collector()
73 unit_test_log << log::end();
76 //____________________________________________________________________________//
78 } // namespace ut_detail
80 // ************************************************************************** //
81 // ************** unit_test_log ************** //
82 // ************************************************************************** //
87 struct unit_test_log_data_helper_impl {
88 typedef boost::shared_ptr<unit_test_log_formatter> formatter_ptr;
89 typedef boost::shared_ptr<io_saver_type> saver_ptr;
92 output_format m_format;
93 std::ostream* m_stream;
94 saver_ptr m_stream_state_saver;
95 formatter_ptr m_log_formatter;
96 bool m_entry_in_progress;
98 unit_test_log_data_helper_impl(unit_test_log_formatter* p_log_formatter, output_format format, bool enabled = false)
99 : m_enabled( enabled )
101 , m_stream( &std::cout )
102 , m_stream_state_saver( new io_saver_type( std::cout ) )
104 , m_entry_in_progress( false )
106 m_log_formatter.reset(p_log_formatter);
107 m_log_formatter->set_log_level(log_all_errors);
111 std::ostream& stream()
116 log_level get_log_level() const
118 return m_log_formatter->get_log_level();
122 struct unit_test_log_impl {
126 m_log_formatter_data.push_back( unit_test_log_data_helper_impl(new output::compiler_log_formatter, OF_CLF, true) ); // only this one is active by default,
127 m_log_formatter_data.push_back( unit_test_log_data_helper_impl(new output::xml_log_formatter, OF_XML, false) );
128 m_log_formatter_data.push_back( unit_test_log_data_helper_impl(new output::junit_log_formatter, OF_JUNIT, false) );
131 typedef std::vector<unit_test_log_data_helper_impl> v_formatter_data_t;
132 v_formatter_data_t m_log_formatter_data;
135 log_entry_data m_entry_data;
137 bool has_entry_in_progress() const {
138 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl const&, current_logger_data, m_log_formatter_data ) {
139 if( current_logger_data.m_entry_in_progress )
146 log_checkpoint_data m_checkpoint_data;
148 void set_checkpoint( const_string file, std::size_t line_num, const_string msg )
150 assign_op( m_checkpoint_data.m_message, msg, 0 );
151 m_checkpoint_data.m_file_name = file;
152 m_checkpoint_data.m_line_num = line_num;
156 unit_test_log_impl& s_log_impl() { static unit_test_log_impl the_inst; return the_inst; }
160 //____________________________________________________________________________//
162 BOOST_TEST_SINGLETON_CONS_IMPL( unit_test_log_t )
164 //____________________________________________________________________________//
167 unit_test_log_t::test_start( counter_t test_cases_amount )
169 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
170 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() == log_nothing )
173 current_logger_data.m_log_formatter->log_start( current_logger_data.stream(), test_cases_amount );
175 current_logger_data.m_log_formatter->log_build_info(
176 current_logger_data.stream(),
177 runtime_config::get<bool>( runtime_config::btrt_build_info ));
179 //current_logger_data.stream().flush();
181 current_logger_data.m_entry_in_progress = false;
185 //____________________________________________________________________________//
188 unit_test_log_t::test_finish()
190 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
191 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() == log_nothing )
194 current_logger_data.m_log_formatter->log_finish( current_logger_data.stream() );
196 current_logger_data.stream().flush();
200 //____________________________________________________________________________//
203 unit_test_log_t::test_aborted()
205 BOOST_TEST_LOG_ENTRY( log_messages ) << "Test is aborted";
208 //____________________________________________________________________________//
211 unit_test_log_t::test_unit_start( test_unit const& tu )
213 if( s_log_impl().has_entry_in_progress() )
215 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
216 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() > log_test_units )
218 current_logger_data.m_log_formatter->test_unit_start( current_logger_data.stream(), tu );
222 //____________________________________________________________________________//
225 unit_test_log_t::test_unit_finish( test_unit const& tu, unsigned long elapsed )
227 s_log_impl().m_checkpoint_data.clear();
229 if( s_log_impl().has_entry_in_progress() )
232 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
234 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() > log_test_units )
237 current_logger_data.m_log_formatter->test_unit_finish( current_logger_data.stream(), tu, elapsed );
241 //____________________________________________________________________________//
244 unit_test_log_t::test_unit_skipped( test_unit const& tu, const_string reason )
246 if( s_log_impl().has_entry_in_progress() )
249 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
250 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() > log_test_units )
253 current_logger_data.m_log_formatter->test_unit_skipped( current_logger_data.stream(), tu, reason );
258 unit_test_log_t::test_unit_aborted( test_unit const& tu )
260 if( s_log_impl().has_entry_in_progress() )
263 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
264 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() > log_test_units )
267 current_logger_data.m_log_formatter->test_unit_aborted(current_logger_data.stream(), tu );
272 unit_test_log_t::test_unit_timed_out( test_unit const& tu )
274 if( s_log_impl().has_entry_in_progress() )
277 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
278 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() > log_test_units )
281 current_logger_data.m_log_formatter->test_unit_timed_out(current_logger_data.stream(), tu );
285 //____________________________________________________________________________//
288 unit_test_log_t::exception_caught( execution_exception const& ex )
291 ex.code() <= execution_exception::cpp_exception_error ? log_cpp_exception_errors :
292 (ex.code() <= execution_exception::timeout_error ? log_system_errors
293 : log_fatal_errors );
295 if( s_log_impl().has_entry_in_progress() )
298 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
300 if( current_logger_data.m_enabled && l >= current_logger_data.get_log_level() ) {
302 current_logger_data.m_log_formatter->log_exception_start( current_logger_data.stream(), s_log_impl().m_checkpoint_data, ex );
304 log_entry_context( l );
306 current_logger_data.m_log_formatter->log_exception_finish( current_logger_data.stream() );
309 clear_entry_context();
312 //____________________________________________________________________________//
315 unit_test_log_t::set_checkpoint( const_string file, std::size_t line_num, const_string msg )
317 s_log_impl().set_checkpoint( file, line_num, msg );
320 //____________________________________________________________________________//
323 set_unix_slash( char in )
325 return in == '\\' ? '/' : in;
329 unit_test_log_t::operator<<( log::begin const& b )
331 if( s_log_impl().has_entry_in_progress() )
334 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
335 if( current_logger_data.m_enabled ) {
336 current_logger_data.m_stream_state_saver->restore();
340 s_log_impl().m_entry_data.clear();
342 assign_op( s_log_impl().m_entry_data.m_file_name, b.m_file_name, 0 );
344 // normalize file name
345 std::transform( s_log_impl().m_entry_data.m_file_name.begin(), s_log_impl().m_entry_data.m_file_name.end(),
346 s_log_impl().m_entry_data.m_file_name.begin(),
349 s_log_impl().m_entry_data.m_line_num = b.m_line_num;
354 //____________________________________________________________________________//
357 unit_test_log_t::operator<<( log::end const& )
359 if( s_log_impl().has_entry_in_progress() ) {
360 log_entry_context( s_log_impl().m_entry_data.m_level );
362 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
363 if( current_logger_data.m_enabled && current_logger_data.m_entry_in_progress ) {
364 current_logger_data.m_log_formatter->log_entry_finish( current_logger_data.stream() );
366 current_logger_data.m_entry_in_progress = false;
370 clear_entry_context();
375 //____________________________________________________________________________//
378 unit_test_log_t::operator<<( log_level l )
380 s_log_impl().m_entry_data.m_level = l;
385 //____________________________________________________________________________//
387 ut_detail::entry_value_collector
388 unit_test_log_t::operator()( log_level l )
392 return ut_detail::entry_value_collector();
395 //____________________________________________________________________________//
398 unit_test_log_t::log_entry_start(output_format log_format)
400 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
402 if( current_logger_data.m_format != log_format )
405 if( current_logger_data.m_entry_in_progress )
408 if( !current_logger_data.m_enabled )
411 switch( s_log_impl().m_entry_data.m_level ) {
412 case log_successful_tests:
413 current_logger_data.m_log_formatter->log_entry_start( current_logger_data.stream(), s_log_impl().m_entry_data,
414 unit_test_log_formatter::BOOST_UTL_ET_INFO );
417 current_logger_data.m_log_formatter->log_entry_start( current_logger_data.stream(), s_log_impl().m_entry_data,
418 unit_test_log_formatter::BOOST_UTL_ET_MESSAGE );
421 current_logger_data.m_log_formatter->log_entry_start( current_logger_data.stream(), s_log_impl().m_entry_data,
422 unit_test_log_formatter::BOOST_UTL_ET_WARNING );
425 case log_cpp_exception_errors:
426 case log_system_errors:
427 current_logger_data.m_log_formatter->log_entry_start( current_logger_data.stream(), s_log_impl().m_entry_data,
428 unit_test_log_formatter::BOOST_UTL_ET_ERROR );
430 case log_fatal_errors:
431 current_logger_data.m_log_formatter->log_entry_start( current_logger_data.stream(), s_log_impl().m_entry_data,
432 unit_test_log_formatter::BOOST_UTL_ET_FATAL_ERROR );
436 case invalid_log_level:
440 current_logger_data.m_entry_in_progress = true;
447 //____________________________________________________________________________//
450 unit_test_log_t::operator<<( const_string value )
452 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
453 if( current_logger_data.m_enabled && s_log_impl().m_entry_data.m_level >= current_logger_data.get_log_level() && !value.empty() && log_entry_start(current_logger_data.m_format) )
454 current_logger_data.m_log_formatter->log_entry_value( current_logger_data.stream(), value );
460 //____________________________________________________________________________//
463 unit_test_log_t::operator<<( lazy_ostream const& value )
465 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
466 if( current_logger_data.m_enabled && s_log_impl().m_entry_data.m_level >= current_logger_data.get_log_level() && !value.empty() ) {
467 if( log_entry_start(current_logger_data.m_format) ) {
468 current_logger_data.m_log_formatter->log_entry_value( current_logger_data.stream(), value );
475 //____________________________________________________________________________//
478 unit_test_log_t::log_entry_context( log_level l )
480 framework::context_generator const& context = framework::get_context();
481 if( context.is_empty() )
486 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
487 if( current_logger_data.m_enabled ) {
488 current_logger_data.m_log_formatter->entry_context_start( current_logger_data.stream(), l );
492 while( !(frame=context.next()).is_empty() )
494 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
495 if( current_logger_data.m_enabled ) {
496 current_logger_data.m_log_formatter->log_entry_context( current_logger_data.stream(), l, frame );
501 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
502 if( current_logger_data.m_enabled ) {
503 current_logger_data.m_log_formatter->entry_context_finish( current_logger_data.stream(), l );
508 //____________________________________________________________________________//
511 unit_test_log_t::clear_entry_context()
513 framework::clear_context();
516 //____________________________________________________________________________//
519 unit_test_log_t::set_stream( std::ostream& str )
521 if( s_log_impl().has_entry_in_progress() )
524 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
525 current_logger_data.m_stream = &str;
526 current_logger_data.m_stream_state_saver.reset( new io_saver_type( str ) );
530 //____________________________________________________________________________//
533 unit_test_log_t::set_stream( output_format log_format, std::ostream& str )
535 if( s_log_impl().has_entry_in_progress() )
538 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
539 if( current_logger_data.m_format == log_format) {
540 current_logger_data.m_stream = &str;
541 current_logger_data.m_stream_state_saver.reset( new io_saver_type( str ) );
548 unit_test_log_t::get_stream( output_format log_format ) const
550 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
551 if( current_logger_data.m_format == log_format) {
552 return current_logger_data.m_stream;
558 //____________________________________________________________________________//
561 unit_test_log_t::set_threshold_level( log_level lev )
563 if( s_log_impl().has_entry_in_progress() || lev == invalid_log_level )
566 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
567 current_logger_data.m_log_formatter->set_log_level( lev );
571 //____________________________________________________________________________//
574 unit_test_log_t::set_threshold_level( output_format log_format, log_level lev )
576 if( s_log_impl().has_entry_in_progress() || lev == invalid_log_level )
579 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
580 if( current_logger_data.m_format == log_format) {
581 current_logger_data.m_log_formatter->set_log_level( lev );
587 //____________________________________________________________________________//
590 unit_test_log_t::set_format( output_format log_format )
592 if( s_log_impl().has_entry_in_progress() )
595 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
596 current_logger_data.m_enabled = current_logger_data.m_format == log_format;
600 //____________________________________________________________________________//
603 unit_test_log_t::add_format( output_format log_format )
605 if( s_log_impl().has_entry_in_progress() )
608 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
609 if( current_logger_data.m_format == log_format) {
610 current_logger_data.m_enabled = true;
616 //____________________________________________________________________________//
618 unit_test_log_formatter*
619 unit_test_log_t::get_formatter( output_format log_format ) {
620 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
621 if( current_logger_data.m_format == log_format) {
622 return current_logger_data.m_log_formatter.get();
630 unit_test_log_t::add_formatter( unit_test_log_formatter* the_formatter )
632 // remove only user defined logger
633 for(unit_test_log_impl::v_formatter_data_t::iterator it(s_log_impl().m_log_formatter_data.begin()),
634 ite(s_log_impl().m_log_formatter_data.end());
638 if( it->m_format == OF_CUSTOM_LOGGER) {
639 s_log_impl().m_log_formatter_data.erase(it);
644 if( the_formatter ) {
645 s_log_impl().m_log_formatter_data.push_back( unit_test_log_data_helper_impl(the_formatter, OF_CUSTOM_LOGGER, true) );
650 unit_test_log_t::set_formatter( unit_test_log_formatter* the_formatter )
652 // remove only user defined logger
653 log_level current_level = invalid_log_level;
654 std::ostream *current_stream = 0;
655 output_format previous_format = OF_INVALID;
656 for(unit_test_log_impl::v_formatter_data_t::iterator it(s_log_impl().m_log_formatter_data.begin()),
657 ite(s_log_impl().m_log_formatter_data.end());
661 if( it->m_enabled ) {
662 if( current_level == invalid_log_level || it->m_format < previous_format || it->m_format == OF_CUSTOM_LOGGER) {
663 current_level = it->get_log_level();
664 current_stream = &(it->stream());
665 previous_format = it->m_format;
670 if( the_formatter ) {
671 add_formatter(the_formatter);
672 set_format(OF_CUSTOM_LOGGER);
673 set_threshold_level(OF_CUSTOM_LOGGER, current_level);
674 set_stream(OF_CUSTOM_LOGGER, *current_stream);
678 //____________________________________________________________________________//
680 // ************************************************************************** //
681 // ************** unit_test_log_formatter ************** //
682 // ************************************************************************** //
685 unit_test_log_formatter::log_entry_value( std::ostream& ostr, lazy_ostream const& value )
687 log_entry_value( ostr, (wrap_stringstream().ref() << value).str() );
691 unit_test_log_formatter::set_log_level(log_level new_log_level)
693 m_log_level = new_log_level;
697 unit_test_log_formatter::get_log_level() const
702 //____________________________________________________________________________//
704 } // namespace unit_test
707 #include <boost/test/detail/enable_warnings.hpp>
709 #endif // BOOST_TEST_UNIT_TEST_LOG_IPP_012205GER