compiler_log_formatter.ipp 9.4 KB

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