compiler_log_formatter.ipp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision$
  10. //
  11. // Description : implements compiler like Log formatter
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_COMPILER_LOG_FORMATTER_IPP_020105GER
  14. #define BOOST_TEST_COMPILER_LOG_FORMATTER_IPP_020105GER
  15. // Boost.Test
  16. #include <boost/test/output/compiler_log_formatter.hpp>
  17. #include <boost/test/framework.hpp>
  18. #include <boost/test/execution_monitor.hpp>
  19. #include <boost/test/unit_test_parameters.hpp>
  20. #include <boost/test/tree/test_unit.hpp>
  21. #include <boost/test/utils/basic_cstring/io.hpp>
  22. #include <boost/test/utils/lazy_ostream.hpp>
  23. // Boost
  24. #include <boost/version.hpp>
  25. // STL
  26. #include <iostream>
  27. #include <boost/test/detail/suppress_warnings.hpp>
  28. //____________________________________________________________________________//
  29. namespace boost {
  30. namespace unit_test {
  31. namespace output {
  32. // ************************************************************************** //
  33. // ************** compiler_log_formatter ************** //
  34. // ************************************************************************** //
  35. namespace {
  36. std::string
  37. test_phase_identifier()
  38. {
  39. return framework::test_in_progress() ? framework::current_test_unit().full_name() : std::string( "Test setup" );
  40. }
  41. } // local namespace
  42. //____________________________________________________________________________//
  43. void
  44. compiler_log_formatter::log_start( std::ostream& output, counter_t test_cases_amount )
  45. {
  46. m_color_output = runtime_config::get<bool>( runtime_config::btrt_color_output );
  47. if( test_cases_amount > 0 )
  48. output << "Running " << test_cases_amount << " test "
  49. << (test_cases_amount > 1 ? "cases" : "case") << "...\n";
  50. }
  51. //____________________________________________________________________________//
  52. void
  53. compiler_log_formatter::log_finish( std::ostream& ostr )
  54. {
  55. ostr.flush();
  56. }
  57. //____________________________________________________________________________//
  58. void
  59. compiler_log_formatter::log_build_info( std::ostream& output, bool log_build_info )
  60. {
  61. if(log_build_info) {
  62. output << "Platform: " << BOOST_PLATFORM << '\n'
  63. << "Compiler: " << BOOST_COMPILER << '\n'
  64. << "STL : " << BOOST_STDLIB << '\n'
  65. << "Boost : " << BOOST_VERSION/100000 << "."
  66. << BOOST_VERSION/100 % 1000 << "."
  67. << BOOST_VERSION % 100 << std::endl;
  68. }
  69. }
  70. //____________________________________________________________________________//
  71. void
  72. compiler_log_formatter::test_unit_start( std::ostream& output, test_unit const& tu )
  73. {
  74. BOOST_TEST_SCOPE_SETCOLOR( m_color_output, output, term_attr::BRIGHT, term_color::BLUE );
  75. print_prefix( output, tu.p_file_name, tu.p_line_num );
  76. output << "Entering test " << tu.p_type_name << " \"" << tu.p_name << "\"" << std::endl;
  77. }
  78. //____________________________________________________________________________//
  79. void
  80. compiler_log_formatter::test_unit_finish( std::ostream& output, test_unit const& tu, unsigned long elapsed )
  81. {
  82. BOOST_TEST_SCOPE_SETCOLOR( m_color_output, output, term_attr::BRIGHT, term_color::BLUE );
  83. print_prefix( output, tu.p_file_name, tu.p_line_num );
  84. output << "Leaving test " << tu.p_type_name << " \"" << tu.p_name << "\"";
  85. if( elapsed > 0 ) {
  86. output << "; testing time: ";
  87. if( elapsed % 1000 == 0 )
  88. output << elapsed/1000 << "ms";
  89. else
  90. output << elapsed << "us";
  91. }
  92. output << std::endl;
  93. }
  94. //____________________________________________________________________________//
  95. void
  96. compiler_log_formatter::test_unit_skipped( std::ostream& output, test_unit const& tu, const_string reason )
  97. {
  98. BOOST_TEST_SCOPE_SETCOLOR( m_color_output, output, term_attr::BRIGHT, term_color::YELLOW );
  99. print_prefix( output, tu.p_file_name, tu.p_line_num );
  100. output << "Test " << tu.p_type_name << " \"" << tu.full_name() << "\"" << " is skipped because " << reason << std::endl;
  101. }
  102. //____________________________________________________________________________//
  103. void
  104. compiler_log_formatter::log_exception_start( std::ostream& output, log_checkpoint_data const& checkpoint_data, execution_exception const& ex )
  105. {
  106. execution_exception::location const& loc = ex.where();
  107. print_prefix( output, loc.m_file_name, loc.m_line_num );
  108. {
  109. BOOST_TEST_SCOPE_SETCOLOR( m_color_output, output, term_attr::UNDERLINE, term_color::RED );
  110. output << "fatal error: in \"" << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function ) << "\": "
  111. << ex.what();
  112. }
  113. if( !checkpoint_data.m_file_name.is_empty() ) {
  114. output << '\n';
  115. print_prefix( output, checkpoint_data.m_file_name, checkpoint_data.m_line_num );
  116. BOOST_TEST_SCOPE_SETCOLOR( m_color_output, output, term_attr::BRIGHT, term_color::CYAN );
  117. output << "last checkpoint";
  118. if( !checkpoint_data.m_message.empty() )
  119. output << ": " << checkpoint_data.m_message;
  120. }
  121. }
  122. //____________________________________________________________________________//
  123. void
  124. compiler_log_formatter::log_exception_finish( std::ostream& output )
  125. {
  126. output << std::endl;
  127. }
  128. //____________________________________________________________________________//
  129. void
  130. compiler_log_formatter::log_entry_start( std::ostream& output, log_entry_data const& entry_data, log_entry_types let )
  131. {
  132. using namespace utils;
  133. switch( let ) {
  134. case BOOST_UTL_ET_INFO:
  135. print_prefix( output, entry_data.m_file_name, entry_data.m_line_num );
  136. output << setcolor( m_color_output, term_attr::BRIGHT, term_color::GREEN, term_color::ORIGINAL, &m_color_state);
  137. output << "info: ";
  138. break;
  139. case BOOST_UTL_ET_MESSAGE:
  140. output << setcolor( m_color_output, term_attr::BRIGHT, term_color::CYAN, term_color::ORIGINAL, &m_color_state);
  141. break;
  142. case BOOST_UTL_ET_WARNING:
  143. print_prefix( output, entry_data.m_file_name, entry_data.m_line_num );
  144. output << setcolor( m_color_output, term_attr::BRIGHT, term_color::YELLOW, term_color::ORIGINAL, &m_color_state);
  145. output << "warning: in \"" << test_phase_identifier() << "\": ";
  146. break;
  147. case BOOST_UTL_ET_ERROR:
  148. print_prefix( output, entry_data.m_file_name, entry_data.m_line_num );
  149. output << setcolor( m_color_output, term_attr::BRIGHT, term_color::RED, term_color::ORIGINAL, &m_color_state);
  150. output << "error: in \"" << test_phase_identifier() << "\": ";
  151. break;
  152. case BOOST_UTL_ET_FATAL_ERROR:
  153. print_prefix( output, entry_data.m_file_name, entry_data.m_line_num );
  154. output << setcolor( m_color_output, term_attr::UNDERLINE, term_color::RED, term_color::ORIGINAL, &m_color_state);
  155. output << "fatal error: in \"" << test_phase_identifier() << "\": ";
  156. break;
  157. }
  158. }
  159. //____________________________________________________________________________//
  160. void
  161. compiler_log_formatter::log_entry_value( std::ostream& output, const_string value )
  162. {
  163. output << value;
  164. }
  165. //____________________________________________________________________________//
  166. void
  167. compiler_log_formatter::log_entry_value( std::ostream& output, lazy_ostream const& value )
  168. {
  169. output << value;
  170. }
  171. //____________________________________________________________________________//
  172. void
  173. compiler_log_formatter::log_entry_finish( std::ostream& output )
  174. {
  175. if( m_color_output )
  176. output << utils::setcolor(m_color_output, &m_color_state);
  177. output << std::endl;
  178. }
  179. //____________________________________________________________________________//
  180. void
  181. compiler_log_formatter::print_prefix( std::ostream& output, const_string file_name, std::size_t line_num )
  182. {
  183. if( !file_name.empty() ) {
  184. #ifdef __APPLE_CC__
  185. // Xcode-compatible logging format, idea by Richard Dingwall at
  186. // <http://richarddingwall.name/2008/06/01/using-the-boost-unit-test-framework-with-xcode-3/>.
  187. output << file_name << ':' << line_num << ": ";
  188. #else
  189. output << file_name << '(' << line_num << "): ";
  190. #endif
  191. }
  192. }
  193. //____________________________________________________________________________//
  194. void
  195. compiler_log_formatter::entry_context_start( std::ostream& output, log_level l )
  196. {
  197. if( l == log_messages ) {
  198. output << "\n[context:";
  199. }
  200. else {
  201. output << (l == log_successful_tests ? "\nAssertion" : "\nFailure" ) << " occurred in a following context:";
  202. }
  203. }
  204. //____________________________________________________________________________//
  205. void
  206. compiler_log_formatter::entry_context_finish( std::ostream& output, log_level l )
  207. {
  208. if( l == log_messages ) {
  209. output << "]";
  210. }
  211. output.flush();
  212. }
  213. //____________________________________________________________________________//
  214. void
  215. compiler_log_formatter::log_entry_context( std::ostream& output, log_level /*l*/, const_string context_descr )
  216. {
  217. output << "\n " << context_descr;
  218. }
  219. //____________________________________________________________________________//
  220. } // namespace output
  221. } // namespace unit_test
  222. } // namespace boost
  223. #include <boost/test/detail/enable_warnings.hpp>
  224. #endif // BOOST_TEST_COMPILER_LOG_FORMATTER_IPP_020105GER