unit_test_log.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // (C) Copyright Gennadiy Rozental 2001-2008.
  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: 54633 $
  10. //
  11. // Description : defines singleton class unit_test_log and all manipulators.
  12. // unit_test_log has output stream like interface. It's implementation is
  13. // completely hidden with pimple idiom
  14. // ***************************************************************************
  15. #ifndef BOOST_TEST_UNIT_TEST_LOG_HPP_071894GER
  16. #define BOOST_TEST_UNIT_TEST_LOG_HPP_071894GER
  17. // Boost.Test
  18. #include <boost/test/test_observer.hpp>
  19. #include <boost/test/detail/global_typedef.hpp>
  20. #include <boost/test/detail/log_level.hpp>
  21. #include <boost/test/detail/fwd_decl.hpp>
  22. #include <boost/test/utils/wrap_stringstream.hpp>
  23. #include <boost/test/utils/trivial_singleton.hpp>
  24. #include <boost/test/utils/lazy_ostream.hpp>
  25. // Boost
  26. #include <boost/utility.hpp>
  27. // STL
  28. #include <iosfwd> // for std::ostream&
  29. #include <boost/test/detail/suppress_warnings.hpp>
  30. //____________________________________________________________________________//
  31. namespace boost {
  32. namespace unit_test {
  33. // ************************************************************************** //
  34. // ************** log manipulators ************** //
  35. // ************************************************************************** //
  36. namespace log {
  37. struct BOOST_TEST_DECL begin {
  38. begin( const_string fn, std::size_t ln )
  39. : m_file_name( fn )
  40. , m_line_num( ln )
  41. {}
  42. const_string m_file_name;
  43. std::size_t m_line_num;
  44. };
  45. struct end {};
  46. } // namespace log
  47. // ************************************************************************** //
  48. // ************** entry_value_collector ************** //
  49. // ************************************************************************** //
  50. namespace ut_detail {
  51. class BOOST_TEST_DECL entry_value_collector {
  52. public:
  53. // Constructors
  54. entry_value_collector() : m_last( true ) {}
  55. entry_value_collector( entry_value_collector const& rhs ) : m_last( true ) { rhs.m_last = false; }
  56. ~entry_value_collector();
  57. // collection interface
  58. entry_value_collector const& operator<<( lazy_ostream const& ) const;
  59. entry_value_collector const& operator<<( const_string ) const;
  60. private:
  61. // Data members
  62. mutable bool m_last;
  63. };
  64. } // namespace ut_detail
  65. // ************************************************************************** //
  66. // ************** unit_test_log ************** //
  67. // ************************************************************************** //
  68. class BOOST_TEST_DECL unit_test_log_t : public test_observer, public singleton<unit_test_log_t> {
  69. public:
  70. // test_observer interface implementation
  71. void test_start( counter_t test_cases_amount );
  72. void test_finish();
  73. void test_aborted();
  74. void test_unit_start( test_unit const& );
  75. void test_unit_finish( test_unit const&, unsigned long elapsed );
  76. void test_unit_skipped( test_unit const& );
  77. void test_unit_aborted( test_unit const& );
  78. void assertion_result( bool passed );
  79. void exception_caught( execution_exception const& );
  80. virtual int priority() { return 1; }
  81. // log configuration methods
  82. void set_stream( std::ostream& );
  83. void set_threshold_level( log_level );
  84. void set_format( output_format );
  85. void set_formatter( unit_test_log_formatter* );
  86. // test progress logging
  87. void set_checkpoint( const_string file, std::size_t line_num, const_string msg = const_string() );
  88. // entry logging
  89. unit_test_log_t& operator<<( log::begin const& ); // begin entry
  90. unit_test_log_t& operator<<( log::end const& ); // end entry
  91. unit_test_log_t& operator<<( log_level ); // set entry level
  92. unit_test_log_t& operator<<( const_string ); // log entry value
  93. unit_test_log_t& operator<<( lazy_ostream const& ); // log entry value
  94. ut_detail::entry_value_collector operator()( log_level ); // initiate entry collection
  95. private:
  96. bool log_entry_start();
  97. BOOST_TEST_SINGLETON_CONS( unit_test_log_t );
  98. }; // unit_test_log_t
  99. BOOST_TEST_SINGLETON_INST( unit_test_log )
  100. // helper macros
  101. #define BOOST_TEST_LOG_ENTRY( ll ) \
  102. (::boost::unit_test::unit_test_log \
  103. << ::boost::unit_test::log::begin( BOOST_TEST_L(__FILE__), __LINE__ ))(ll) \
  104. /**/
  105. } // namespace unit_test
  106. } // namespace boost
  107. // ************************************************************************** //
  108. // ************** Unit test log interface helpers ************** //
  109. // ************************************************************************** //
  110. #define BOOST_TEST_MESSAGE( M ) \
  111. BOOST_TEST_LOG_ENTRY( ::boost::unit_test::log_messages ) \
  112. << (::boost::unit_test::lazy_ostream::instance() << M) \
  113. /**/
  114. //____________________________________________________________________________//
  115. #define BOOST_TEST_PASSPOINT() \
  116. ::boost::unit_test::unit_test_log.set_checkpoint( \
  117. BOOST_TEST_L(__FILE__), \
  118. static_cast<std::size_t>(__LINE__) ) \
  119. /**/
  120. //____________________________________________________________________________//
  121. #define BOOST_TEST_CHECKPOINT( M ) \
  122. ::boost::unit_test::unit_test_log.set_checkpoint( \
  123. BOOST_TEST_L(__FILE__), \
  124. static_cast<std::size_t>(__LINE__), \
  125. (::boost::wrap_stringstream().ref() << M).str() ) \
  126. /**/
  127. //____________________________________________________________________________//
  128. #include <boost/test/detail/enable_warnings.hpp>
  129. #endif // BOOST_TEST_UNIT_TEST_LOG_HPP_071894GER