]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
6 | // See http://www.boost.org/libs/test for the library home page. | |
7 | // | |
8 | /// @file | |
9 | /// @brief Defines unit test log formatter interface | |
10 | /// | |
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 | // *************************************************************************** | |
14 | ||
15 | #ifndef BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER | |
16 | #define BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER | |
17 | ||
18 | // Boost.Test | |
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 | ||
23 | // STL | |
24 | #include <iosfwd> | |
25 | #include <string> // for std::string | |
26 | #include <iostream> | |
27 | ||
28 | #include <boost/test/detail/suppress_warnings.hpp> | |
29 | ||
30 | //____________________________________________________________________________// | |
31 | ||
32 | namespace boost { | |
33 | namespace unit_test { | |
34 | ||
35 | // ************************************************************************** // | |
36 | /// Collection of log entry attributes | |
37 | // ************************************************************************** // | |
38 | ||
39 | struct BOOST_TEST_DECL log_entry_data { | |
40 | log_entry_data() | |
41 | { | |
42 | m_file_name.reserve( 200 ); | |
43 | } | |
44 | ||
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 | |
48 | ||
49 | void clear() | |
50 | { | |
51 | m_file_name.erase(); | |
52 | m_line_num = 0; | |
53 | m_level = log_nothing; | |
54 | } | |
55 | }; | |
56 | ||
57 | // ************************************************************************** // | |
58 | /// Collection of log checkpoint attributes | |
59 | // ************************************************************************** // | |
60 | ||
61 | struct BOOST_TEST_DECL log_checkpoint_data | |
62 | { | |
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 | |
66 | ||
67 | void clear() | |
68 | { | |
69 | m_file_name.clear(); | |
70 | m_line_num = 0; | |
71 | m_message = std::string(); | |
72 | } | |
73 | }; | |
74 | ||
75 | // ************************************************************************** // | |
76 | /// @brief Abstract Unit Test Framework log formatter interface | |
77 | /// | |
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). | |
81 | /// | |
82 | /// All these messages constitute Unit Test Framework log. There are many ways (formats) to present | |
83 | /// these messages to the user. | |
84 | /// | |
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. | |
89 | /// | |
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. | |
93 | /// | |
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. | |
99 | /// | |
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 . | |
104 | /// | |
105 | /// @see | |
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 { | |
108 | public: | |
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 | |
115 | }; | |
116 | ||
117 | //! Constructor | |
118 | unit_test_log_formatter() | |
119 | : m_log_level(log_all_errors) | |
120 | {} | |
121 | ||
122 | // Destructor | |
123 | virtual ~unit_test_log_formatter() {} | |
124 | ||
125 | // @name Test start/finish | |
126 | ||
127 | /// Invoked at the beginning of test module execution | |
128 | /// | |
129 | /// @param[in] os output stream to write a messages to | |
130 | /// @param[in] test_cases_amount total test case amount to be run | |
131 | /// @see log_finish | |
132 | virtual void log_start( std::ostream& os, counter_t test_cases_amount ) = 0; | |
133 | ||
134 | /// Invoked at the end of test module execution | |
135 | /// | |
136 | /// @param[in] os output stream to write a messages into | |
137 | /// @see log_start | |
138 | virtual void log_finish( std::ostream& os ) = 0; | |
139 | ||
140 | /// Invoked when Unit Test Framework build information is requested | |
141 | /// | |
142 | /// @param[in] os output stream to write a messages into | |
143 | virtual void log_build_info( std::ostream& os ) = 0; | |
144 | // @} | |
145 | ||
146 | // @name Test unit start/finish | |
147 | ||
148 | /// Invoked when test unit starts (either test suite or test case) | |
149 | /// | |
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; | |
154 | ||
155 | /// Invoked when test unit finishes | |
156 | /// | |
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; | |
162 | ||
163 | /// Invoked if test unit skipped for any reason | |
164 | /// | |
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 ) | |
169 | { | |
170 | test_unit_skipped( os, tu ); | |
171 | } | |
172 | ||
173 | /// Deprecated version of this interface | |
174 | virtual void test_unit_skipped( std::ostream& os, test_unit const& tu ) {} | |
175 | ||
176 | /// Invoked when a test unit is aborted | |
177 | virtual void test_unit_aborted( std::ostream& os, test_unit const& tu ) {} | |
178 | ||
179 | // @} | |
180 | ||
181 | // @name Uncaught exception report | |
182 | ||
183 | /// Invoked when Unit Test Framework detects uncaught exception | |
184 | /// | |
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 | |
190 | /// | |
191 | /// The logging of the exception information is finilized by a call to @c log_exception_finish. | |
192 | /// | |
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; | |
198 | ||
199 | /// Invoked when Unit Test Framework detects uncaught exception | |
200 | /// | |
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; | |
205 | // @} | |
206 | ||
207 | // @name Regular log entry | |
208 | ||
209 | /// Invoked by Unit Test Framework to start new log entry | |
210 | ||
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 | |
218 | /// | |
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; | |
222 | ||
223 | /// Invoked by Unit Test Framework to report a log entry content | |
224 | /// | |
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; | |
230 | ||
231 | /// Invoked by Unit Test Framework to report a log entry content | |
232 | ||
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 | |
240 | ||
241 | /// Invoked by Unit Test Framework to finish a log entry report | |
242 | ||
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; | |
246 | // @} | |
247 | ||
248 | // @name Log entry context report | |
249 | ||
250 | /// Invoked by Unit Test Framework to start log entry context report | |
251 | ||
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; | |
259 | ||
260 | /// Invoked by Unit Test Framework to report log entry context "scope" description | |
261 | ||
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] value context "scope" description | |
265 | /// @see log_entry_start, entry_context_finish | |
266 | virtual void log_entry_context( std::ostream& os, const_string value ) = 0; | |
267 | ||
268 | /// Invoked by Unit Test Framework to finish log entry context report | |
269 | ||
270 | /// @param[in] os output stream to write a messages into | |
271 | /// @see log_entry_start, entry_context_context | |
272 | virtual void entry_context_finish( std::ostream& os ) = 0; | |
273 | // @} | |
274 | ||
275 | // @name Log level management | |
276 | ||
277 | /// Sets the log level of the logger/formatter | |
278 | /// | |
279 | /// Some loggers need to manage the log level by their own. This | |
280 | /// member function let the implementation decide of that. | |
281 | /// @par Since Boost 1.62 | |
282 | virtual void set_log_level(log_level new_log_level); | |
283 | ||
284 | /// Returns the log level of the logger/formatter | |
285 | /// @par Since Boost 1.62 | |
286 | virtual log_level get_log_level() const; | |
287 | // @} | |
288 | ||
289 | ||
290 | // @name Stream management | |
291 | ||
292 | /// Returns a default stream for this logger. | |
293 | /// | |
294 | /// The returned string describes the stream as if it was passed from | |
295 | /// the command line @c "--log_sink" parameter. With that regards, @b stdout and @b stderr | |
296 | /// have special meaning indicating the standard output or error stream respectively. | |
297 | /// | |
298 | /// @par Since Boost 1.62 | |
299 | virtual std::string get_default_stream_description() const | |
300 | { | |
301 | return "stdout"; | |
302 | } | |
303 | ||
304 | // @} | |
305 | ||
306 | ||
307 | protected: | |
308 | log_level m_log_level; | |
309 | ||
310 | }; | |
311 | ||
312 | } // namespace unit_test | |
313 | } // namespace boost | |
314 | ||
315 | //____________________________________________________________________________// | |
316 | ||
317 | #include <boost/test/detail/enable_warnings.hpp> | |
318 | ||
319 | #endif // BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER | |
320 |