unit_test_log_formatter.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. virtual void log_build_info( std::ostream& os ) = 0;
  123. // @}
  124. // @name Test unit start/finish
  125. /// Invoked when test unit starts (either test suite or test case)
  126. ///
  127. /// @param[in] os output stream to write a messages into
  128. /// @param[in] tu test unit being started
  129. /// @see test_unit_finish
  130. virtual void test_unit_start( std::ostream& os, test_unit const& tu ) = 0;
  131. /// Invoked when test unit finishes
  132. ///
  133. /// @param[in] os output stream to write a messages into
  134. /// @param[in] tu test unit being finished
  135. /// @param[in] elapsed time in microseconds spend executing this test unit
  136. /// @see test_unit_start
  137. virtual void test_unit_finish( std::ostream& os, test_unit const& tu, unsigned long elapsed ) = 0;
  138. /// Invoked if test unit skipped for any reason
  139. ///
  140. /// @param[in] os output stream to write a messages into
  141. /// @param[in] tu skipped test unit
  142. /// @param[in] reason explanation why was it skipped
  143. virtual void test_unit_skipped( std::ostream& os, test_unit const& tu, const_string /* reason */)
  144. {
  145. test_unit_skipped( os, tu );
  146. }
  147. /// Deprecated version of this interface
  148. virtual void test_unit_skipped( std::ostream& /* os */, test_unit const& /* tu */) {}
  149. /// Invoked when a test unit is aborted
  150. virtual void test_unit_aborted( std::ostream& /* os */, test_unit const& /* tu */) {}
  151. // @}
  152. // @name Uncaught exception report
  153. /// Invoked when Unit Test Framework detects uncaught exception
  154. ///
  155. /// The framwork calls this function when an uncaught exception it detected.
  156. /// This call is followed by context information:
  157. /// - one call to @c entry_context_start,
  158. /// - as many calls to @c log_entry_context as there are context entries
  159. /// - one call to @c entry_context_finish
  160. ///
  161. /// The logging of the exception information is finilized by a call to @c log_exception_finish.
  162. ///
  163. /// @param[in] os output stream to write a messages into
  164. /// @param[in] lcd information about the last checkpoint before the exception was triggered
  165. /// @param[in] ex information about the caught exception
  166. /// @see log_exception_finish
  167. virtual void log_exception_start( std::ostream& os, log_checkpoint_data const& lcd, execution_exception const& ex ) = 0;
  168. /// Invoked when Unit Test Framework detects uncaught exception
  169. ///
  170. /// Call to this function finishes uncaught exception report.
  171. /// @param[in] os output stream to write a messages into
  172. /// @see log_exception_start
  173. virtual void log_exception_finish( std::ostream& os ) = 0;
  174. // @}
  175. // @name Regular log entry
  176. /// Invoked by Unit Test Framework to start new log entry
  177. /// 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.
  178. /// 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
  179. /// expressions in a form of "lazy" expression template lazy_ostream.
  180. /// @param[in] os output stream to write a messages into
  181. /// @param[in] led log entry attributes
  182. /// @param[in] let log entry type log_entry_finish
  183. /// @see log_entry_value, log_entry_finish
  184. ///
  185. /// @note call to this function may happen before any call to test_unit_start or all calls to test_unit_finish as the
  186. /// framework might log errors raised during global initialization/shutdown.
  187. virtual void log_entry_start( std::ostream& os, log_entry_data const& led, log_entry_types let ) = 0;
  188. /// Invoked by Unit Test Framework to report a log entry content
  189. ///
  190. /// This is one of two overloaded methods to report log entry content. This one is used to report plain string value.
  191. /// @param[in] os output stream to write a messages into.
  192. /// @param[in] value log entry string value
  193. /// @see log_entry_start, log_entry_finish
  194. virtual void log_entry_value( std::ostream& os, const_string value ) = 0;
  195. /// Invoked by Unit Test Framework to report a log entry content
  196. /// This is one of two overloaded methods to report log entry content. This one is used to report some complicated expression passed as
  197. /// an expression template lazy_ostream. In most cases default implementation provided by the framework should work as is (it just converts
  198. /// the lazy expression into a string.
  199. /// @param[in] os output stream to write a messages into
  200. /// @param[in] value log entry "lazy" value
  201. /// @see log_entry_start, log_entry_finish
  202. virtual void log_entry_value( std::ostream& os, lazy_ostream const& value ); // there is a default impl
  203. /// Invoked by Unit Test Framework to finish a log entry report
  204. /// @param[in] os output stream to write a messages into
  205. /// @see log_entry_start, log_entry_start
  206. virtual void log_entry_finish( std::ostream& os ) = 0;
  207. // @}
  208. // @name Log entry context report
  209. /// Invoked by Unit Test Framework to start log entry context report
  210. //
  211. /// Unit Test Framework logs for failed assertions and uncaught exceptions context if one was defined by a test module.
  212. /// Context consists of multiple "scopes" identified by description messages assigned by the test module using
  213. /// BOOST_TEST_INFO/BOOST_TEST_CONTEXT statements.
  214. /// @param[in] os output stream to write a messages into
  215. /// @param[in] l entry log_level, to be used to fine tune the message
  216. /// @see log_entry_context, entry_context_finish
  217. virtual void entry_context_start( std::ostream& os, log_level l ) = 0;
  218. /// Invoked by Unit Test Framework to report log entry context "scope" description
  219. //
  220. /// Each "scope" description is reported by separate call to log_entry_context.
  221. /// @param[in] os output stream to write a messages into
  222. /// @param[in] l entry log_level, to be used to fine tune the message
  223. /// @param[in] value context "scope" description
  224. /// @see log_entry_start, entry_context_finish
  225. virtual void log_entry_context( std::ostream& os, log_level l, const_string value ) = 0;
  226. /// Invoked by Unit Test Framework to finish log entry context report
  227. ///
  228. /// @param[in] os output stream to write a messages into
  229. /// @param[in] l entry log_level, to be used to fine tune the message
  230. /// @see log_entry_start, entry_context_context
  231. virtual void entry_context_finish( std::ostream& os, log_level l ) = 0;
  232. // @}
  233. // @name Log level management
  234. /// Sets the log level of the logger/formatter
  235. ///
  236. /// Some loggers need to manage the log level by their own. This
  237. /// member function let the implementation decide of that.
  238. /// @par Since Boost 1.62
  239. virtual void set_log_level(log_level new_log_level);
  240. /// Returns the log level of the logger/formatter
  241. /// @par Since Boost 1.62
  242. virtual log_level get_log_level() const;
  243. // @}
  244. // @name Stream management
  245. /// Returns a default stream for this logger.
  246. ///
  247. /// The returned string describes the stream as if it was passed from
  248. /// the command line @c "--log_sink" parameter. With that regards, @b stdout and @b stderr
  249. /// have special meaning indicating the standard output or error stream respectively.
  250. ///
  251. /// @par Since Boost 1.62
  252. virtual std::string get_default_stream_description() const
  253. {
  254. return "stdout";
  255. }
  256. // @}
  257. protected:
  258. log_level m_log_level;
  259. };
  260. } // namespace unit_test
  261. } // namespace boost
  262. //____________________________________________________________________________//
  263. #include <boost/test/detail/enable_warnings.hpp>
  264. #endif // BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER