1 | // (C) Copyright Gennadiy Rozental 2005. |
---|
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 : $RCSfile: unit_test_log.ipp,v $ |
---|
9 | // |
---|
10 | // Version : $Revision: 1.11 $ |
---|
11 | // |
---|
12 | // Description : implemets Unit Test Log |
---|
13 | // *************************************************************************** |
---|
14 | |
---|
15 | #ifndef BOOST_TEST_UNIT_TEST_LOG_IPP_012205GER |
---|
16 | #define BOOST_TEST_UNIT_TEST_LOG_IPP_012205GER |
---|
17 | |
---|
18 | // Boost.Test |
---|
19 | #include <boost/test/unit_test_log.hpp> |
---|
20 | #include <boost/test/unit_test_log_formatter.hpp> |
---|
21 | #include <boost/test/unit_test_suite_impl.hpp> |
---|
22 | #include <boost/test/execution_monitor.hpp> |
---|
23 | |
---|
24 | #include <boost/test/detail/unit_test_parameters.hpp> |
---|
25 | #include <boost/test/detail/wrap_io_saver.hpp> |
---|
26 | |
---|
27 | #include <boost/test/utils/basic_cstring/compare.hpp> |
---|
28 | |
---|
29 | #include <boost/test/output/compiler_log_formatter.hpp> |
---|
30 | #include <boost/test/output/xml_log_formatter.hpp> |
---|
31 | |
---|
32 | // Boost |
---|
33 | #include <boost/scoped_ptr.hpp> |
---|
34 | |
---|
35 | // STL |
---|
36 | #include <iostream> |
---|
37 | |
---|
38 | #include <boost/test/detail/suppress_warnings.hpp> |
---|
39 | |
---|
40 | //____________________________________________________________________________// |
---|
41 | |
---|
42 | namespace boost { |
---|
43 | |
---|
44 | namespace unit_test { |
---|
45 | |
---|
46 | // ************************************************************************** // |
---|
47 | // ************** entry_value_collector ************** // |
---|
48 | // ************************************************************************** // |
---|
49 | |
---|
50 | namespace ut_detail { |
---|
51 | |
---|
52 | entry_value_collector |
---|
53 | entry_value_collector::operator<<( const_string v ) |
---|
54 | { |
---|
55 | unit_test_log << v; |
---|
56 | |
---|
57 | m_last = false; |
---|
58 | |
---|
59 | entry_value_collector res; |
---|
60 | return res; |
---|
61 | } |
---|
62 | |
---|
63 | //____________________________________________________________________________// |
---|
64 | |
---|
65 | entry_value_collector::~entry_value_collector() |
---|
66 | { |
---|
67 | if( m_last ) |
---|
68 | unit_test_log << log::end(); |
---|
69 | } |
---|
70 | |
---|
71 | //____________________________________________________________________________// |
---|
72 | |
---|
73 | } // namespace ut_detail |
---|
74 | |
---|
75 | // ************************************************************************** // |
---|
76 | // ************** unit_test_log ************** // |
---|
77 | // ************************************************************************** // |
---|
78 | |
---|
79 | namespace { |
---|
80 | |
---|
81 | struct unit_test_log_impl { |
---|
82 | // Constructor |
---|
83 | unit_test_log_impl() |
---|
84 | : m_stream( &std::cout ) |
---|
85 | , m_stream_state_saver( new io_saver_type( std::cout ) ) |
---|
86 | , m_threshold_level( log_all_errors ) |
---|
87 | , m_log_formatter( new output::compiler_log_formatter ) |
---|
88 | { |
---|
89 | } |
---|
90 | |
---|
91 | // log data |
---|
92 | typedef scoped_ptr<unit_test_log_formatter> formatter_ptr; |
---|
93 | typedef scoped_ptr<io_saver_type> saver_ptr; |
---|
94 | |
---|
95 | std::ostream* m_stream; |
---|
96 | saver_ptr m_stream_state_saver; |
---|
97 | log_level m_threshold_level; |
---|
98 | formatter_ptr m_log_formatter; |
---|
99 | |
---|
100 | // entry data |
---|
101 | bool m_entry_in_progress; |
---|
102 | bool m_entry_started; |
---|
103 | log_entry_data m_entry_data; |
---|
104 | |
---|
105 | // check point data |
---|
106 | log_checkpoint_data m_checkpoint_data; |
---|
107 | |
---|
108 | // helper functions |
---|
109 | std::ostream& stream() { return *m_stream; } |
---|
110 | void set_checkpoint( const_string file, std::size_t line_num, const_string msg ) |
---|
111 | { |
---|
112 | assign_op( m_checkpoint_data.m_message, msg, 0 ); |
---|
113 | m_checkpoint_data.m_file_name = file; |
---|
114 | m_checkpoint_data.m_line_num = line_num; |
---|
115 | } |
---|
116 | }; |
---|
117 | |
---|
118 | unit_test_log_impl& s_log_impl() { static unit_test_log_impl the_inst; return the_inst; } |
---|
119 | |
---|
120 | } // local namespace |
---|
121 | |
---|
122 | //____________________________________________________________________________// |
---|
123 | |
---|
124 | void |
---|
125 | unit_test_log_t::test_start( counter_t test_cases_amount ) |
---|
126 | { |
---|
127 | s_log_impl().m_log_formatter->log_start( s_log_impl().stream(), test_cases_amount ); |
---|
128 | |
---|
129 | if( runtime_config::show_build_info() ) |
---|
130 | s_log_impl().m_log_formatter->log_build_info( s_log_impl().stream() ); |
---|
131 | |
---|
132 | s_log_impl().m_entry_in_progress = false; |
---|
133 | } |
---|
134 | |
---|
135 | //____________________________________________________________________________// |
---|
136 | |
---|
137 | void |
---|
138 | unit_test_log_t::test_finish() |
---|
139 | { |
---|
140 | s_log_impl().m_log_formatter->log_finish( s_log_impl().stream() ); |
---|
141 | } |
---|
142 | |
---|
143 | //____________________________________________________________________________// |
---|
144 | |
---|
145 | void |
---|
146 | unit_test_log_t::test_aborted() |
---|
147 | { |
---|
148 | BOOST_TEST_LOG_ENTRY( log_messages ) << "Test is aborted"; |
---|
149 | } |
---|
150 | |
---|
151 | //____________________________________________________________________________// |
---|
152 | |
---|
153 | void |
---|
154 | unit_test_log_t::test_unit_start( test_unit const& tu ) |
---|
155 | { |
---|
156 | if( s_log_impl().m_threshold_level > log_test_suites ) |
---|
157 | return; |
---|
158 | |
---|
159 | if( s_log_impl().m_entry_in_progress ) |
---|
160 | *this << log::end(); |
---|
161 | |
---|
162 | s_log_impl().m_log_formatter->test_unit_start( s_log_impl().stream(), tu ); |
---|
163 | } |
---|
164 | |
---|
165 | //____________________________________________________________________________// |
---|
166 | |
---|
167 | void |
---|
168 | unit_test_log_t::test_unit_finish( test_unit const& tu, unsigned long elapsed ) |
---|
169 | { |
---|
170 | if( s_log_impl().m_threshold_level > log_test_suites ) |
---|
171 | return; |
---|
172 | |
---|
173 | s_log_impl().m_checkpoint_data.clear(); |
---|
174 | |
---|
175 | if( s_log_impl().m_entry_in_progress ) |
---|
176 | *this << log::end(); |
---|
177 | |
---|
178 | s_log_impl().m_log_formatter->test_unit_finish( s_log_impl().stream(), tu, elapsed ); |
---|
179 | } |
---|
180 | |
---|
181 | //____________________________________________________________________________// |
---|
182 | |
---|
183 | void |
---|
184 | unit_test_log_t::test_unit_skipped( test_unit const& tu ) |
---|
185 | { |
---|
186 | if( s_log_impl().m_threshold_level > log_test_suites ) |
---|
187 | return; |
---|
188 | |
---|
189 | if( s_log_impl().m_entry_in_progress ) |
---|
190 | *this << log::end(); |
---|
191 | |
---|
192 | s_log_impl().m_log_formatter->test_unit_skipped( s_log_impl().stream(), tu ); |
---|
193 | } |
---|
194 | |
---|
195 | //____________________________________________________________________________// |
---|
196 | |
---|
197 | void |
---|
198 | unit_test_log_t::test_unit_aborted( test_unit const& ) |
---|
199 | { |
---|
200 | // do nothing |
---|
201 | } |
---|
202 | |
---|
203 | //____________________________________________________________________________// |
---|
204 | |
---|
205 | void |
---|
206 | unit_test_log_t::assertion_result( bool ) |
---|
207 | { |
---|
208 | // do nothing |
---|
209 | } |
---|
210 | |
---|
211 | //____________________________________________________________________________// |
---|
212 | |
---|
213 | void |
---|
214 | unit_test_log_t::exception_caught( execution_exception const& ex ) |
---|
215 | { |
---|
216 | log_level l = |
---|
217 | ex.code() <= execution_exception::cpp_exception_error ? log_cpp_exception_errors : |
---|
218 | (ex.code() <= execution_exception::timeout_error ? log_system_errors |
---|
219 | : log_fatal_errors ); |
---|
220 | |
---|
221 | if( l >= s_log_impl().m_threshold_level ) { |
---|
222 | if( s_log_impl().m_entry_in_progress ) |
---|
223 | *this << log::end(); |
---|
224 | |
---|
225 | s_log_impl().m_log_formatter->log_exception( s_log_impl().stream(), s_log_impl().m_checkpoint_data, ex.what() ); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | //____________________________________________________________________________// |
---|
230 | |
---|
231 | void |
---|
232 | unit_test_log_t::set_checkpoint( const_string file, std::size_t line_num, const_string msg ) |
---|
233 | { |
---|
234 | s_log_impl().set_checkpoint( file, line_num, msg ); |
---|
235 | } |
---|
236 | |
---|
237 | //____________________________________________________________________________// |
---|
238 | |
---|
239 | char |
---|
240 | set_unix_slash( char in ) |
---|
241 | { |
---|
242 | return in == '\\' ? '/' : in; |
---|
243 | } |
---|
244 | |
---|
245 | unit_test_log_t& |
---|
246 | unit_test_log_t::operator<<( log::begin const& b ) |
---|
247 | { |
---|
248 | if( s_log_impl().m_entry_in_progress ) |
---|
249 | *this << log::end(); |
---|
250 | |
---|
251 | s_log_impl().m_stream_state_saver->restore(); |
---|
252 | |
---|
253 | s_log_impl().m_entry_data.clear(); |
---|
254 | |
---|
255 | assign_op( s_log_impl().m_entry_data.m_file_name, b.m_file_name, 0 ); |
---|
256 | |
---|
257 | // normalize file name |
---|
258 | std::transform( s_log_impl().m_entry_data.m_file_name.begin(), s_log_impl().m_entry_data.m_file_name.end(), |
---|
259 | s_log_impl().m_entry_data.m_file_name.begin(), |
---|
260 | &set_unix_slash ); |
---|
261 | |
---|
262 | s_log_impl().m_entry_data.m_line_num = b.m_line_num; |
---|
263 | |
---|
264 | return *this; |
---|
265 | } |
---|
266 | |
---|
267 | //____________________________________________________________________________// |
---|
268 | |
---|
269 | unit_test_log_t& |
---|
270 | unit_test_log_t::operator<<( log::end const& ) |
---|
271 | { |
---|
272 | if( s_log_impl().m_entry_in_progress ) |
---|
273 | s_log_impl().m_log_formatter->log_entry_finish( s_log_impl().stream() ); |
---|
274 | |
---|
275 | s_log_impl().m_entry_in_progress = false; |
---|
276 | |
---|
277 | return *this; |
---|
278 | } |
---|
279 | |
---|
280 | //____________________________________________________________________________// |
---|
281 | |
---|
282 | unit_test_log_t& |
---|
283 | unit_test_log_t::operator<<( log_level l ) |
---|
284 | { |
---|
285 | s_log_impl().m_entry_data.m_level = l; |
---|
286 | |
---|
287 | return *this; |
---|
288 | } |
---|
289 | |
---|
290 | //____________________________________________________________________________// |
---|
291 | |
---|
292 | ut_detail::entry_value_collector |
---|
293 | unit_test_log_t::operator()( log_level l ) |
---|
294 | { |
---|
295 | *this << l; |
---|
296 | |
---|
297 | ut_detail::entry_value_collector res; |
---|
298 | return res; |
---|
299 | } |
---|
300 | |
---|
301 | //____________________________________________________________________________// |
---|
302 | |
---|
303 | unit_test_log_t& |
---|
304 | unit_test_log_t::operator<<( const_string value ) |
---|
305 | { |
---|
306 | if( s_log_impl().m_entry_data.m_level >= s_log_impl().m_threshold_level && !value.empty() ) { |
---|
307 | if( !s_log_impl().m_entry_in_progress ) { |
---|
308 | s_log_impl().m_entry_in_progress = true; |
---|
309 | |
---|
310 | switch( s_log_impl().m_entry_data.m_level ) { |
---|
311 | case log_successful_tests: |
---|
312 | s_log_impl().m_log_formatter->log_entry_start( s_log_impl().stream(), s_log_impl().m_entry_data, |
---|
313 | unit_test_log_formatter::BOOST_UTL_ET_INFO ); |
---|
314 | break; |
---|
315 | case log_messages: |
---|
316 | s_log_impl().m_log_formatter->log_entry_start( s_log_impl().stream(), s_log_impl().m_entry_data, |
---|
317 | unit_test_log_formatter::BOOST_UTL_ET_MESSAGE ); |
---|
318 | break; |
---|
319 | case log_warnings: |
---|
320 | s_log_impl().m_log_formatter->log_entry_start( s_log_impl().stream(), s_log_impl().m_entry_data, |
---|
321 | unit_test_log_formatter::BOOST_UTL_ET_WARNING ); |
---|
322 | break; |
---|
323 | case log_all_errors: |
---|
324 | case log_cpp_exception_errors: |
---|
325 | case log_system_errors: |
---|
326 | s_log_impl().m_log_formatter->log_entry_start( s_log_impl().stream(), s_log_impl().m_entry_data, |
---|
327 | unit_test_log_formatter::BOOST_UTL_ET_ERROR ); |
---|
328 | break; |
---|
329 | case log_fatal_errors: |
---|
330 | s_log_impl().m_log_formatter->log_entry_start( s_log_impl().stream(), s_log_impl().m_entry_data, |
---|
331 | unit_test_log_formatter::BOOST_UTL_ET_FATAL_ERROR ); |
---|
332 | break; |
---|
333 | case log_nothing: |
---|
334 | case log_test_suites: |
---|
335 | case invalid_log_level: |
---|
336 | return *this; |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | s_log_impl().m_log_formatter->log_entry_value( s_log_impl().stream(), value ); |
---|
341 | } |
---|
342 | |
---|
343 | return *this; |
---|
344 | } |
---|
345 | |
---|
346 | //____________________________________________________________________________// |
---|
347 | |
---|
348 | void |
---|
349 | unit_test_log_t::set_stream( std::ostream& str ) |
---|
350 | { |
---|
351 | if( s_log_impl().m_entry_in_progress ) |
---|
352 | return; |
---|
353 | |
---|
354 | s_log_impl().m_stream = &str; |
---|
355 | s_log_impl().m_stream_state_saver.reset( new io_saver_type( str ) ); |
---|
356 | } |
---|
357 | |
---|
358 | //____________________________________________________________________________// |
---|
359 | |
---|
360 | void |
---|
361 | unit_test_log_t::set_threshold_level( log_level lev ) |
---|
362 | { |
---|
363 | if( s_log_impl().m_entry_in_progress || lev == invalid_log_level ) |
---|
364 | return; |
---|
365 | |
---|
366 | s_log_impl().m_threshold_level = lev; |
---|
367 | } |
---|
368 | |
---|
369 | //____________________________________________________________________________// |
---|
370 | |
---|
371 | void |
---|
372 | unit_test_log_t::set_format( output_format log_format ) |
---|
373 | { |
---|
374 | if( s_log_impl().m_entry_in_progress ) |
---|
375 | return; |
---|
376 | |
---|
377 | if( log_format == CLF ) |
---|
378 | set_formatter( new output::compiler_log_formatter ); |
---|
379 | else |
---|
380 | set_formatter( new output::xml_log_formatter ); |
---|
381 | } |
---|
382 | |
---|
383 | //____________________________________________________________________________// |
---|
384 | |
---|
385 | void |
---|
386 | unit_test_log_t::set_formatter( unit_test_log_formatter* the_formatter ) |
---|
387 | { |
---|
388 | s_log_impl().m_log_formatter.reset( the_formatter ); |
---|
389 | } |
---|
390 | |
---|
391 | //____________________________________________________________________________// |
---|
392 | |
---|
393 | } // namespace unit_test |
---|
394 | |
---|
395 | } // namespace boost |
---|
396 | |
---|
397 | //____________________________________________________________________________// |
---|
398 | |
---|
399 | #include <boost/test/detail/enable_warnings.hpp> |
---|
400 | |
---|
401 | // *************************************************************************** |
---|
402 | // Revision History : |
---|
403 | // |
---|
404 | // $Log: unit_test_log.ipp,v $ |
---|
405 | // Revision 1.11 2005/12/14 05:34:21 rogeeff |
---|
406 | // log API simplified |
---|
407 | // |
---|
408 | // Revision 1.10 2005/04/30 16:48:51 rogeeff |
---|
409 | // io saver warkaround for classic io is shared |
---|
410 | // |
---|
411 | // Revision 1.9 2005/04/29 06:28:35 rogeeff |
---|
412 | // bug fix for manipulator handling |
---|
413 | // |
---|
414 | // Revision 1.8 2005/04/12 06:50:46 rogeeff |
---|
415 | // assign_to -> assign_op |
---|
416 | // |
---|
417 | // Revision 1.7 2005/03/22 07:06:58 rogeeff |
---|
418 | // assign_to made free function |
---|
419 | // |
---|
420 | // Revision 1.6 2005/02/20 08:27:07 rogeeff |
---|
421 | // This a major update for Boost.Test framework. See release docs for complete list of fixes/updates |
---|
422 | // |
---|
423 | // *************************************************************************** |
---|
424 | |
---|
425 | #endif // BOOST_TEST_UNIT_TEST_LOG_IPP_012205GER |
---|