compiler_log_formatter.ipp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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, bool log_build_info )
  61. {
  62. if(log_build_info) {
  63. output << "Platform: " << BOOST_PLATFORM << '\n'
  64. << "Compiler: " << BOOST_COMPILER << '\n'
  65. << "STL : " << BOOST_STDLIB << '\n'
  66. << "Boost : " << BOOST_VERSION/100000 << "."
  67. << BOOST_VERSION/100 % 1000 << "."
  68. << BOOST_VERSION % 100 << std::endl;
  69. }
  70. }
  71. //____________________________________________________________________________//
  72. void
  73. compiler_log_formatter::test_unit_start( std::ostream& output, test_unit const& tu )
  74. {
  75. BOOST_TEST_SCOPE_SETCOLOR( m_color_output, output, term_attr::BRIGHT, term_color::BLUE );
  76. print_prefix( output, tu.p_file_name, tu.p_line_num );
  77. output << "Entering test " << tu.p_type_name << " \"" << tu.p_name << "\"" << std::endl;
  78. }
  79. //____________________________________________________________________________//
  80. void
  81. compiler_log_formatter::test_unit_finish( std::ostream& output, test_unit const& tu, unsigned long elapsed )
  82. {
  83. BOOST_TEST_SCOPE_SETCOLOR( m_color_output, output, term_attr::BRIGHT, term_color::BLUE );
  84. print_prefix( output, tu.p_file_name, tu.p_line_num );
  85. output << "Leaving test " << tu.p_type_name << " \"" << tu.p_name << "\"";
  86. if( elapsed > 0 ) {
  87. output << "; testing time: ";
  88. if( elapsed % 1000 == 0 )
  89. output << elapsed/1000 << "ms";
  90. else
  91. output << elapsed << "us";
  92. }
  93. output << std::endl;
  94. }
  95. //____________________________________________________________________________//
  96. void
  97. compiler_log_formatter::test_unit_skipped( std::ostream& output, test_unit const& tu, const_string reason )
  98. {
  99. BOOST_TEST_SCOPE_SETCOLOR( m_color_output, output, term_attr::BRIGHT, term_color::YELLOW );
  100. print_prefix( output, tu.p_file_name, tu.p_line_num );
  101. output << "Test " << tu.p_type_name << " \"" << tu.full_name() << "\"" << " is skipped because " << reason << std::endl;
  102. }
  103. //____________________________________________________________________________//
  104. void
  105. compiler_log_formatter::log_exception_start( std::ostream& output, log_checkpoint_data const& checkpoint_data, execution_exception const& ex )
  106. {
  107. execution_exception::location const& loc = ex.where();
  108. print_prefix( output, loc.m_file_name, loc.m_line_num );
  109. {
  110. BOOST_TEST_SCOPE_SETCOLOR( m_color_output, output, term_attr::UNDERLINE, term_color::RED );
  111. output << "fatal error: in \"" << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function ) << "\": "
  112. << ex.what();
  113. }
  114. if( !checkpoint_data.m_file_name.is_empty() ) {
  115. output << '\n';
  116. print_prefix( output, checkpoint_data.m_file_name, checkpoint_data.m_line_num );
  117. BOOST_TEST_SCOPE_SETCOLOR( m_color_output, output, term_attr::BRIGHT, term_color::CYAN );
  118. output << "last checkpoint";
  119. if( !checkpoint_data.m_message.empty() )
  120. output << ": " << checkpoint_data.m_message;
  121. }
  122. }
  123. //____________________________________________________________________________//
  124. void
  125. compiler_log_formatter::log_exception_finish( std::ostream& output )
  126. {
  127. output << std::endl;
  128. }
  129. //____________________________________________________________________________//
  130. void
  131. compiler_log_formatter::log_entry_start( std::ostream& output, log_entry_data const& entry_data, log_entry_types let )
  132. {
  133. using namespace utils;
  134. switch( let ) {
  135. case BOOST_UTL_ET_INFO:
  136. print_prefix( output, entry_data.m_file_name, entry_data.m_line_num );
  137. output << setcolor( m_color_output, term_attr::BRIGHT, term_color::GREEN );
  138. output << "info: ";
  139. break;
  140. case BOOST_UTL_ET_MESSAGE:
  141. output << setcolor( m_color_output, 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. output << setcolor( m_color_output, term_attr::BRIGHT, term_color::YELLOW );
  146. output << "warning: in \"" << test_phase_identifier() << "\": ";
  147. break;
  148. case BOOST_UTL_ET_ERROR:
  149. print_prefix( output, entry_data.m_file_name, entry_data.m_line_num );
  150. output << setcolor( m_color_output, term_attr::BRIGHT, term_color::RED );
  151. output << "error: in \"" << test_phase_identifier() << "\": ";
  152. break;
  153. case BOOST_UTL_ET_FATAL_ERROR:
  154. print_prefix( output, entry_data.m_file_name, entry_data.m_line_num );
  155. output << setcolor( m_color_output, term_attr::UNDERLINE, term_color::RED );
  156. output << "fatal error: in \"" << test_phase_identifier() << "\": ";
  157. break;
  158. }
  159. }
  160. //____________________________________________________________________________//
  161. void
  162. compiler_log_formatter::log_entry_value( std::ostream& output, const_string value )
  163. {
  164. output << value;
  165. }
  166. //____________________________________________________________________________//
  167. void
  168. compiler_log_formatter::log_entry_value( std::ostream& output, lazy_ostream const& value )
  169. {
  170. output << value;
  171. }
  172. //____________________________________________________________________________//
  173. void
  174. compiler_log_formatter::log_entry_finish( std::ostream& output )
  175. {
  176. if( m_color_output )
  177. output << utils::setcolor(m_color_output);
  178. output << std::endl;
  179. }
  180. //____________________________________________________________________________//
  181. void
  182. compiler_log_formatter::print_prefix( std::ostream& output, const_string file_name, std::size_t line_num )
  183. {
  184. if( !file_name.empty() ) {
  185. #ifdef __APPLE_CC__
  186. // Xcode-compatible logging format, idea by Richard Dingwall at
  187. // <http://richarddingwall.name/2008/06/01/using-the-boost-unit-test-framework-with-xcode-3/>.
  188. output << file_name << ':' << line_num << ": ";
  189. #else
  190. output << file_name << '(' << line_num << "): ";
  191. #endif
  192. }
  193. }
  194. //____________________________________________________________________________//
  195. void
  196. compiler_log_formatter::entry_context_start( std::ostream& output, log_level l )
  197. {
  198. if( l == log_messages ) {
  199. output << "\n[context:";
  200. }
  201. else {
  202. output << (l == log_successful_tests ? "\nAssertion" : "\nFailure" ) << " occurred in a following context:";
  203. }
  204. }
  205. //____________________________________________________________________________//
  206. void
  207. compiler_log_formatter::entry_context_finish( std::ostream& output, log_level l )
  208. {
  209. if( l == log_messages ) {
  210. output << "]";
  211. }
  212. output.flush();
  213. }
  214. //____________________________________________________________________________//
  215. void
  216. compiler_log_formatter::log_entry_context( std::ostream& output, log_level /*l*/, const_string context_descr )
  217. {
  218. output << "\n " << context_descr;
  219. }
  220. //____________________________________________________________________________//
  221. } // namespace output
  222. } // namespace unit_test
  223. } // namespace boost
  224. #include <boost/test/detail/enable_warnings.hpp>
  225. #endif // BOOST_TEST_COMPILER_LOG_FORMATTER_IPP_020105GER