unit_test_log_formatter.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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
  8. /// @brief Defines unit test log formatter interface
  9. ///
  10. /// You can define a class with implements this interface and use an instance of it
  11. /// as a Unit Test Framework log formatter
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER
  14. #define BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER
  15. // Boost.Test
  16. #include <boost/test/detail/global_typedef.hpp>
  17. #include <boost/test/detail/log_level.hpp>
  18. #include <boost/test/detail/fwd_decl.hpp>
  19. // STL
  20. #include <iosfwd>
  21. #include <string> // for std::string
  22. #include <iostream>
  23. #include <boost/test/detail/suppress_warnings.hpp>
  24. //____________________________________________________________________________//
  25. namespace boost {
  26. namespace unit_test {
  27. // ************************************************************************** //
  28. /// Collection of log entry attributes
  29. // ************************************************************************** //
  30. struct BOOST_TEST_DECL log_entry_data {
  31. log_entry_data()
  32. {
  33. m_file_name.reserve( 200 );
  34. }
  35. std::string m_file_name; ///< log entry file name
  36. std::size_t m_line_num; ///< log entry line number
  37. log_level m_level; ///< log entry level
  38. void clear()
  39. {
  40. m_file_name.erase();
  41. m_line_num = 0;
  42. m_level = log_nothing;
  43. }
  44. };
  45. // ************************************************************************** //
  46. /// Collection of log checkpoint attributes
  47. // ************************************************************************** //
  48. struct BOOST_TEST_DECL log_checkpoint_data
  49. {
  50. const_string m_file_name; ///< log checkpoint file name
  51. std::size_t m_line_num; ///< log checkpoint file name
  52. std::string m_message; ///< log checkpoint message
  53. void clear()
  54. {
  55. m_file_name.clear();
  56. m_line_num = 0;
  57. m_message = std::string();
  58. }
  59. };
  60. // ************************************************************************** //
  61. /// @brief Abstract Unit Test Framework log formatter interface
  62. ///
  63. /// During the test module execution Unit Test Framework can report messages about success
  64. /// or failure of assertions, which test suites are being run and more (specifically which
  65. /// messages are reported depends on log level threshold selected by the user).
  66. ///
  67. /// All these messages constitute Unit Test Framework log. There are many ways (formats) to present
  68. /// these messages to the user.
  69. ///
  70. /// Boost.Test comes with three formats:
  71. /// - Compiler-like log format: intended for human consumption/diagnostic
  72. /// - XML based log format: intended for processing by automated regression test systems.
  73. /// - JUNIT based log format: intended for processing by automated regression test systems.
  74. ///
  75. /// If you want to produce some other format you need to implement class with specific interface and use
  76. /// method @c unit_test_log_t::set_formatter during a test module initialization to set an active formatter.
  77. /// The class unit_test_log_formatter defines this interface.
  78. ///
  79. /// This interface requires you to format all possible messages being produced in the log.
  80. /// These includes error messages about failed assertions, messages about caught exceptions and
  81. /// information messages about test units being started/ended. All the methods in this interface takes
  82. /// a reference to standard stream as a first argument. This is where final messages needs to be directed
  83. /// to. Also you are given all the information necessary to produce a message.
  84. ///
  85. /// @par Since Boost 1.62:
  86. /// - Each formatter may indicate the default output stream. This is convenient for instance for streams intended
  87. /// for automated processing that indicate a file. See @c get_default_stream_description for more details.
  88. /// - Each formatter may manage its own log level through the getter/setter @c get_log_level and @c set_log_level .
  89. ///
  90. /// @see
  91. /// - boost::unit_test::test_observer for an indication of the calls of the test observer interface
  92. class BOOST_TEST_DECL unit_test_log_formatter {
  93. public:
  94. /// Types of log entries (messages written into a log)
  95. enum log_entry_types { BOOST_UTL_ET_INFO, ///< Information message from the framework
  96. BOOST_UTL_ET_MESSAGE, ///< Information message from the user
  97. BOOST_UTL_ET_WARNING, ///< Warning (non error) condition notification message
  98. BOOST_UTL_ET_ERROR, ///< Non fatal error notification message
  99. BOOST_UTL_ET_FATAL_ERROR ///< Fatal error notification message
  100. };
  101. //! Constructor
  102. unit_test_log_formatter()
  103. : m_log_level(log_all_errors)
  104. {}
  105. // Destructor
  106. virtual ~unit_test_log_formatter() {}
  107. // @name Test start/finish
  108. /// Invoked at the beginning of test module execution
  109. ///
  110. /// @param[in] os output stream to write a messages to
  111. /// @param[in] test_cases_amount total test case amount to be run
  112. /// @see log_finish
  113. virtual void log_start( std::ostream& os, counter_t test_cases_amount ) = 0;
  114. /// Invoked at the end of test module execution
  115. ///
  116. /// @param[in] os output stream to write a messages into
  117. /// @see log_start
  118. virtual void log_finish( std::ostream& os ) = 0;
  119. /// Invoked when Unit Test Framework build information is requested
  120. ///
  121. /// @param[in] os output stream to write a messages into
  122. /// @param[in] log_build_info indicates if build info should be logged or not
  123. virtual void log_build_info( std::ostream& os, bool log_build_info = true ) = 0;
  124. // @}
  125. // @name Test unit start/finish
  126. /// Invoked when test unit starts (either test suite or test case)
  127. ///
  128. /// @param[in] os output stream to write a messages into
  129. /// @param[in] tu test unit being started
  130. /// @see test_unit_finish
  131. virtual void test_unit_start( std::ostream& os, test_unit const& tu ) = 0;
  132. /// Invoked when test unit finishes
  133. ///
  134. /// @param[in] os output stream to write a messages into
  135. /// @param[in] tu test unit being finished
  136. /// @param[in] elapsed time in microseconds spend executing this test unit
  137. /// @see test_unit_start
  138. virtual void test_unit_finish( std::ostream& os, test_unit const& tu, unsigned long elapsed ) = 0;
  139. /// Invoked if test unit skipped for any reason
  140. ///
  141. /// @param[in] os output stream to write a messages into
  142. /// @param[in] tu skipped test unit
  143. /// @param[in] reason explanation why was it skipped
  144. virtual void test_unit_skipped( std::ostream& os, test_unit const& tu, const_string /* reason */)
  145. {
  146. test_unit_skipped( os, tu );
  147. }
  148. /// Deprecated version of this interface
  149. /// @deprecated
  150. virtual void test_unit_skipped( std::ostream& /* os */, test_unit const& /* tu */) {}
  151. /// Invoked when a test unit is aborted
  152. virtual void test_unit_aborted( std::ostream& /* os */, test_unit const& /* tu */) {}
  153. /// Invoked when a test unit times-out
  154. virtual void test_unit_timed_out( std::ostream& /* os */, test_unit const& /* tu */) {}
  155. // @}
  156. // @name Uncaught exception report
  157. /// Invoked when Unit Test Framework detects uncaught exception
  158. ///
  159. /// The framwork calls this function when an uncaught exception it detected.
  160. /// This call is followed by context information:
  161. /// - one call to @c entry_context_start,
  162. /// - as many calls to @c log_entry_context as there are context entries
  163. /// - one call to @c entry_context_finish
  164. ///
  165. /// The logging of the exception information is finilized by a call to @c log_exception_finish.
  166. ///
  167. /// @param[in] os output stream to write a messages into
  168. /// @param[in] lcd information about the last checkpoint before the exception was triggered
  169. /// @param[in] ex information about the caught exception
  170. /// @see log_exception_finish
  171. virtual void log_exception_start( std::ostream& os, log_checkpoint_data const& lcd, execution_exception const& ex ) = 0;
  172. /// Invoked when Unit Test Framework detects uncaught exception
  173. ///
  174. /// Call to this function finishes uncaught exception report.
  175. /// @param[in] os output stream to write a messages into
  176. /// @see log_exception_start
  177. virtual void log_exception_finish( std::ostream& os ) = 0;
  178. // @}
  179. // @name Regular log entry
  180. /// Invoked by Unit Test Framework to start new log entry
  181. /// Call to this function starts new log entry. It is followed by series of log_entry_value calls and finally call to log_entry_finish.
  182. /// A log entry may consist of one or more values being reported. Some of these values will be plain strings, while others can be complicated
  183. /// expressions in a form of "lazy" expression template lazy_ostream.
  184. /// @param[in] os output stream to write a messages into
  185. /// @param[in] led log entry attributes
  186. /// @param[in] let log entry type log_entry_finish
  187. /// @see log_entry_value, log_entry_finish
  188. ///
  189. /// @note call to this function may happen before any call to test_unit_start or all calls to test_unit_finish as the
  190. /// framework might log errors raised during global initialization/shutdown.
  191. virtual void log_entry_start( std::ostream& os, log_entry_data const& led, log_entry_types let ) = 0;
  192. /// Invoked by Unit Test Framework to report a log entry content
  193. ///
  194. /// This is one of two overloaded methods to report log entry content. This one is used to report plain string value.
  195. /// @param[in] os output stream to write a messages into.
  196. /// @param[in] value log entry string value
  197. /// @see log_entry_start, log_entry_finish
  198. virtual void log_entry_value( std::ostream& os, const_string value ) = 0;
  199. /// Invoked by Unit Test Framework to report a log entry content
  200. /// This is one of two overloaded methods to report log entry content. This one is used to report some complicated expression passed as
  201. /// an expression template lazy_ostream. In most cases default implementation provided by the framework should work as is (it just converts
  202. /// the lazy expression into a string.
  203. /// @param[in] os output stream to write a messages into
  204. /// @param[in] value log entry "lazy" value
  205. /// @see log_entry_start, log_entry_finish
  206. virtual void log_entry_value( std::ostream& os, lazy_ostream const& value ); // there is a default impl
  207. /// Invoked by Unit Test Framework to finish a log entry report
  208. /// @param[in] os output stream to write a messages into
  209. /// @see log_entry_start, log_entry_start
  210. virtual void log_entry_finish( std::ostream& os ) = 0;
  211. // @}
  212. // @name Log entry context report
  213. /// Invoked by Unit Test Framework to start log entry context report
  214. //
  215. /// Unit Test Framework logs for failed assertions and uncaught exceptions context if one was defined by a test module.
  216. /// Context consists of multiple "scopes" identified by description messages assigned by the test module using
  217. /// BOOST_TEST_INFO/BOOST_TEST_CONTEXT statements.
  218. /// @param[in] os output stream to write a messages into
  219. /// @param[in] l entry log_level, to be used to fine tune the message
  220. /// @see log_entry_context, entry_context_finish
  221. virtual void entry_context_start( std::ostream& os, log_level l ) = 0;
  222. /// Invoked by Unit Test Framework to report log entry context "scope" description
  223. //
  224. /// Each "scope" description is reported by separate call to log_entry_context.
  225. /// @param[in] os output stream to write a messages into
  226. /// @param[in] l entry log_level, to be used to fine tune the message
  227. /// @param[in] value context "scope" description
  228. /// @see log_entry_start, entry_context_finish
  229. virtual void log_entry_context( std::ostream& os, log_level l, const_string value ) = 0;
  230. /// Invoked by Unit Test Framework to finish log entry context report
  231. ///
  232. /// @param[in] os output stream to write a messages into
  233. /// @param[in] l entry log_level, to be used to fine tune the message
  234. /// @see log_entry_start, entry_context_context
  235. virtual void entry_context_finish( std::ostream& os, log_level l ) = 0;
  236. // @}
  237. // @name Log level management
  238. /// Sets the log level of the logger/formatter
  239. ///
  240. /// Some loggers need to manage the log level by their own. This
  241. /// member function let the implementation decide of that.
  242. /// @par Since Boost 1.62
  243. virtual void set_log_level(log_level new_log_level);
  244. /// Returns the log level of the logger/formatter
  245. /// @par Since Boost 1.62
  246. virtual log_level get_log_level() const;
  247. // @}
  248. // @name Stream management
  249. /// Returns a default stream for this logger.
  250. ///
  251. /// The returned string describes the stream as if it was passed from
  252. /// the command line @c "--log_sink" parameter. With that regards, @b stdout and @b stderr
  253. /// have special meaning indicating the standard output or error stream respectively.
  254. ///
  255. /// @par Since Boost 1.62
  256. virtual std::string get_default_stream_description() const
  257. {
  258. return "stdout";
  259. }
  260. // @}
  261. protected:
  262. log_level m_log_level;
  263. };
  264. } // namespace unit_test
  265. } // namespace boost
  266. //____________________________________________________________________________//
  267. #include <boost/test/detail/enable_warnings.hpp>
  268. #endif // BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER