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)
6 // See http://www.boost.org/libs/test for the library home page.
9 /// @brief Defines unit test log formatter interface
11 /// You can define a class with implements this interface and use an instance of it
12 /// as a Unit Test Framework log formatter
13 // ***************************************************************************
15 #ifndef BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER
16 #define BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER
19 #include <boost/test/detail/global_typedef.hpp>
20 #include <boost/test/detail/log_level.hpp>
21 #include <boost/test/detail/fwd_decl.hpp>
25 #include <string> // for std::string
28 #include <boost/test/detail/suppress_warnings.hpp>
30 //____________________________________________________________________________//
35 // ************************************************************************** //
36 /// Collection of log entry attributes
37 // ************************************************************************** //
39 struct BOOST_TEST_DECL log_entry_data {
42 m_file_name.reserve( 200 );
45 std::string m_file_name; ///< log entry file name
46 std::size_t m_line_num; ///< log entry line number
47 log_level m_level; ///< log entry level
53 m_level = log_nothing;
57 // ************************************************************************** //
58 /// Collection of log checkpoint attributes
59 // ************************************************************************** //
61 struct BOOST_TEST_DECL log_checkpoint_data
63 const_string m_file_name; ///< log checkpoint file name
64 std::size_t m_line_num; ///< log checkpoint file name
65 std::string m_message; ///< log checkpoint message
71 m_message = std::string();
75 // ************************************************************************** //
76 /// @brief Abstract Unit Test Framework log formatter interface
78 /// During the test module execution Unit Test Framework can report messages about success
79 /// or failure of assertions, which test suites are being run and more (specifically which
80 /// messages are reported depends on log level threshold selected by the user).
82 /// All these messages constitute Unit Test Framework log. There are many ways (formats) to present
83 /// these messages to the user.
85 /// Boost.Test comes with three formats:
86 /// - Compiler-like log format: intended for human consumption/diagnostic
87 /// - XML based log format: intended for processing by automated regression test systems.
88 /// - JUNIT based log format: intended for processing by automated regression test systems.
90 /// If you want to produce some other format you need to implement class with specific interface and use
91 /// method @c unit_test_log_t::set_formatter during a test module initialization to set an active formatter.
92 /// The class unit_test_log_formatter defines this interface.
94 /// This interface requires you to format all possible messages being produced in the log.
95 /// These includes error messages about failed assertions, messages about caught exceptions and
96 /// information messages about test units being started/ended. All the methods in this interface takes
97 /// a reference to standard stream as a first argument. This is where final messages needs to be directed
98 /// to. Also you are given all the information necessary to produce a message.
100 /// @par Since Boost 1.62:
101 /// - Each formatter may indicate the default output stream. This is convenient for instance for streams intended
102 /// for automated processing that indicate a file. See @c get_default_stream_description for more details.
103 /// - Each formatter may manage its own log level through the getter/setter @c get_log_level and @c set_log_level .
106 /// - boost::unit_test::test_observer for an indication of the calls of the test observer interface
107 class BOOST_TEST_DECL unit_test_log_formatter {
109 /// Types of log entries (messages written into a log)
110 enum log_entry_types { BOOST_UTL_ET_INFO, ///< Information message from the framework
111 BOOST_UTL_ET_MESSAGE, ///< Information message from the user
112 BOOST_UTL_ET_WARNING, ///< Warning (non error) condition notification message
113 BOOST_UTL_ET_ERROR, ///< Non fatal error notification message
114 BOOST_UTL_ET_FATAL_ERROR ///< Fatal error notification message
118 unit_test_log_formatter()
119 : m_log_level(log_all_errors)
123 virtual ~unit_test_log_formatter() {}
125 // @name Test start/finish
127 /// Invoked at the beginning of test module execution
129 /// @param[in] os output stream to write a messages to
130 /// @param[in] test_cases_amount total test case amount to be run
132 virtual void log_start( std::ostream& os, counter_t test_cases_amount ) = 0;
134 /// Invoked at the end of test module execution
136 /// @param[in] os output stream to write a messages into
138 virtual void log_finish( std::ostream& os ) = 0;
140 /// Invoked when Unit Test Framework build information is requested
142 /// @param[in] os output stream to write a messages into
143 virtual void log_build_info( std::ostream& os ) = 0;
146 // @name Test unit start/finish
148 /// Invoked when test unit starts (either test suite or test case)
150 /// @param[in] os output stream to write a messages into
151 /// @param[in] tu test unit being started
152 /// @see test_unit_finish
153 virtual void test_unit_start( std::ostream& os, test_unit const& tu ) = 0;
155 /// Invoked when test unit finishes
157 /// @param[in] os output stream to write a messages into
158 /// @param[in] tu test unit being finished
159 /// @param[in] elapsed time in microseconds spend executing this test unit
160 /// @see test_unit_start
161 virtual void test_unit_finish( std::ostream& os, test_unit const& tu, unsigned long elapsed ) = 0;
163 /// Invoked if test unit skipped for any reason
165 /// @param[in] os output stream to write a messages into
166 /// @param[in] tu skipped test unit
167 /// @param[in] reason explanation why was it skipped
168 virtual void test_unit_skipped( std::ostream& os, test_unit const& tu, const_string /* reason */)
170 test_unit_skipped( os, tu );
173 /// Deprecated version of this interface
174 virtual void test_unit_skipped( std::ostream& /* os */, test_unit const& /* tu */) {}
176 /// Invoked when a test unit is aborted
177 virtual void test_unit_aborted( std::ostream& /* os */, test_unit const& /* tu */) {}
181 // @name Uncaught exception report
183 /// Invoked when Unit Test Framework detects uncaught exception
185 /// The framwork calls this function when an uncaught exception it detected.
186 /// This call is followed by context information:
187 /// - one call to @c entry_context_start,
188 /// - as many calls to @c log_entry_context as there are context entries
189 /// - one call to @c entry_context_finish
191 /// The logging of the exception information is finilized by a call to @c log_exception_finish.
193 /// @param[in] os output stream to write a messages into
194 /// @param[in] lcd information about the last checkpoint before the exception was triggered
195 /// @param[in] ex information about the caught exception
196 /// @see log_exception_finish
197 virtual void log_exception_start( std::ostream& os, log_checkpoint_data const& lcd, execution_exception const& ex ) = 0;
199 /// Invoked when Unit Test Framework detects uncaught exception
201 /// Call to this function finishes uncaught exception report.
202 /// @param[in] os output stream to write a messages into
203 /// @see log_exception_start
204 virtual void log_exception_finish( std::ostream& os ) = 0;
207 // @name Regular log entry
209 /// Invoked by Unit Test Framework to start new log entry
211 /// 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.
212 /// 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
213 /// expressions in a form of "lazy" expression template lazy_ostream.
214 /// @param[in] os output stream to write a messages into
215 /// @param[in] led log entry attributes
216 /// @param[in] let log entry type log_entry_finish
217 /// @see log_entry_value, log_entry_finish
219 /// @note call to this function may happen before any call to test_unit_start or all calls to test_unit_finish as the
220 /// framework might log errors raised during global initialization/shutdown.
221 virtual void log_entry_start( std::ostream& os, log_entry_data const& led, log_entry_types let ) = 0;
223 /// Invoked by Unit Test Framework to report a log entry content
225 /// This is one of two overloaded methods to report log entry content. This one is used to report plain string value.
226 /// @param[in] os output stream to write a messages into.
227 /// @param[in] value log entry string value
228 /// @see log_entry_start, log_entry_finish
229 virtual void log_entry_value( std::ostream& os, const_string value ) = 0;
231 /// Invoked by Unit Test Framework to report a log entry content
233 /// This is one of two overloaded methods to report log entry content. This one is used to report some complicated expression passed as
234 /// an expression template lazy_ostream. In most cases default implementation provided by the framework should work as is (it just converts
235 /// the lazy expression into a string.
236 /// @param[in] os output stream to write a messages into
237 /// @param[in] value log entry "lazy" value
238 /// @see log_entry_start, log_entry_finish
239 virtual void log_entry_value( std::ostream& os, lazy_ostream const& value ); // there is a default impl
241 /// Invoked by Unit Test Framework to finish a log entry report
243 /// @param[in] os output stream to write a messages into
244 /// @see log_entry_start, log_entry_start
245 virtual void log_entry_finish( std::ostream& os ) = 0;
248 // @name Log entry context report
250 /// Invoked by Unit Test Framework to start log entry context report
252 /// Unit Test Framework logs for failed assertions and uncaught exceptions context if one was defined by a test module.
253 /// Context consists of multiple "scopes" identified by description messages assigned by the test module using
254 /// BOOST_TEST_INFO/BOOST_TEST_CONTEXT statements.
255 /// @param[in] os output stream to write a messages into
256 /// @param[in] l entry log_level, to be used to fine tune the message
257 /// @see log_entry_context, entry_context_finish
258 virtual void entry_context_start( std::ostream& os, log_level l ) = 0;
260 /// Invoked by Unit Test Framework to report log entry context "scope" description
262 /// Each "scope" description is reported by separate call to log_entry_context.
263 /// @param[in] os output stream to write a messages into
264 /// @param[in] l entry log_level, to be used to fine tune the message
265 /// @param[in] value context "scope" description
266 /// @see log_entry_start, entry_context_finish
267 virtual void log_entry_context( std::ostream& os, log_level l, const_string value ) = 0;
269 /// Invoked by Unit Test Framework to finish log entry context report
271 /// @param[in] os output stream to write a messages into
272 /// @param[in] l entry log_level, to be used to fine tune the message
273 /// @see log_entry_start, entry_context_context
274 virtual void entry_context_finish( std::ostream& os, log_level l ) = 0;
277 // @name Log level management
279 /// Sets the log level of the logger/formatter
281 /// Some loggers need to manage the log level by their own. This
282 /// member function let the implementation decide of that.
283 /// @par Since Boost 1.62
284 virtual void set_log_level(log_level new_log_level);
286 /// Returns the log level of the logger/formatter
287 /// @par Since Boost 1.62
288 virtual log_level get_log_level() const;
292 // @name Stream management
294 /// Returns a default stream for this logger.
296 /// The returned string describes the stream as if it was passed from
297 /// the command line @c "--log_sink" parameter. With that regards, @b stdout and @b stderr
298 /// have special meaning indicating the standard output or error stream respectively.
300 /// @par Since Boost 1.62
301 virtual std::string get_default_stream_description() const
310 log_level m_log_level;
314 } // namespace unit_test
317 //____________________________________________________________________________//
319 #include <boost/test/detail/enable_warnings.hpp>
321 #endif // BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER