]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/test/impl/unit_test_log.ipp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / test / impl / unit_test_log.ipp
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 : $RCSfile$
9 //
10 // Version : $Revision$
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/execution_monitor.hpp>
22 #include <boost/test/framework.hpp>
23 #include <boost/test/unit_test_parameters.hpp>
24
25 #include <boost/test/utils/basic_cstring/compare.hpp>
26 #include <boost/test/utils/foreach.hpp>
27
28 #include <boost/test/output/compiler_log_formatter.hpp>
29 #include <boost/test/output/xml_log_formatter.hpp>
30 #include <boost/test/output/junit_log_formatter.hpp>
31
32 // Boost
33 #include <boost/shared_ptr.hpp>
34 #include <boost/io/ios_state.hpp>
35 typedef ::boost::io::ios_base_all_saver io_saver_type;
36
37 #include <boost/test/detail/suppress_warnings.hpp>
38
39 //____________________________________________________________________________//
40
41 namespace boost {
42 namespace unit_test {
43
44 // ************************************************************************** //
45 // ************** entry_value_collector ************** //
46 // ************************************************************************** //
47
48 namespace ut_detail {
49
50 entry_value_collector const&
51 entry_value_collector::operator<<( lazy_ostream const& v ) const
52 {
53 unit_test_log << v;
54
55 return *this;
56 }
57
58 //____________________________________________________________________________//
59
60 entry_value_collector const&
61 entry_value_collector::operator<<( const_string v ) const
62 {
63 unit_test_log << v;
64
65 return *this;
66 }
67
68 //____________________________________________________________________________//
69
70 entry_value_collector::~entry_value_collector()
71 {
72 if( m_last )
73 unit_test_log << log::end();
74 }
75
76 //____________________________________________________________________________//
77
78 } // namespace ut_detail
79
80 // ************************************************************************** //
81 // ************** unit_test_log ************** //
82 // ************************************************************************** //
83
84 namespace {
85
86 // log data
87 struct unit_test_log_data_helper_impl {
88 typedef boost::shared_ptr<unit_test_log_formatter> formatter_ptr;
89 typedef boost::shared_ptr<io_saver_type> saver_ptr;
90
91 bool m_enabled;
92 output_format m_format;
93 std::ostream* m_stream;
94 saver_ptr m_stream_state_saver;
95 formatter_ptr m_log_formatter;
96 bool m_entry_in_progress;
97
98 unit_test_log_data_helper_impl(unit_test_log_formatter* p_log_formatter, output_format format, bool enabled = false)
99 : m_enabled( enabled )
100 , m_format( format )
101 , m_stream( &std::cout )
102 , m_stream_state_saver( new io_saver_type( std::cout ) )
103 , m_log_formatter()
104 , m_entry_in_progress( false )
105 {
106 m_log_formatter.reset(p_log_formatter);
107 m_log_formatter->set_log_level(log_all_errors);
108 }
109
110 // helper functions
111 std::ostream& stream()
112 {
113 return *m_stream;
114 }
115
116 log_level get_log_level() const
117 {
118 return m_log_formatter->get_log_level();
119 }
120 };
121
122 struct unit_test_log_impl {
123 // Constructor
124 unit_test_log_impl()
125 {
126 m_log_formatter_data.push_back( unit_test_log_data_helper_impl(new output::compiler_log_formatter, OF_CLF, true) ); // only this one is active by default,
127 m_log_formatter_data.push_back( unit_test_log_data_helper_impl(new output::xml_log_formatter, OF_XML, false) );
128 m_log_formatter_data.push_back( unit_test_log_data_helper_impl(new output::junit_log_formatter, OF_JUNIT, false) );
129 }
130
131 typedef std::vector<unit_test_log_data_helper_impl> v_formatter_data_t;
132 v_formatter_data_t m_log_formatter_data;
133
134 // entry data
135 log_entry_data m_entry_data;
136
137 bool has_entry_in_progress() const {
138 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl const&, current_logger_data, m_log_formatter_data ) {
139 if( current_logger_data.m_entry_in_progress )
140 return true;
141 }
142 return false;
143 }
144
145 // check point data
146 log_checkpoint_data m_checkpoint_data;
147
148 void set_checkpoint( const_string file, std::size_t line_num, const_string msg )
149 {
150 assign_op( m_checkpoint_data.m_message, msg, 0 );
151 m_checkpoint_data.m_file_name = file;
152 m_checkpoint_data.m_line_num = line_num;
153 }
154 };
155
156 unit_test_log_impl& s_log_impl() { static unit_test_log_impl the_inst; return the_inst; }
157
158 } // local namespace
159
160 //____________________________________________________________________________//
161
162 void
163 unit_test_log_t::test_start( counter_t test_cases_amount )
164 {
165 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
166 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() == log_nothing )
167 continue;
168
169 current_logger_data.m_log_formatter->log_start( current_logger_data.stream(), test_cases_amount );
170
171 if( runtime_config::get<bool>( runtime_config::btrt_build_info ) )
172 current_logger_data.m_log_formatter->log_build_info( current_logger_data.stream() );
173
174 //current_logger_data.stream().flush();
175
176 current_logger_data.m_entry_in_progress = false;
177 }
178 }
179
180 //____________________________________________________________________________//
181
182 void
183 unit_test_log_t::test_finish()
184 {
185 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
186 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() == log_nothing )
187 continue;
188
189 current_logger_data.m_log_formatter->log_finish( current_logger_data.stream() );
190
191 current_logger_data.stream().flush();
192 }
193 }
194
195 //____________________________________________________________________________//
196
197 void
198 unit_test_log_t::test_aborted()
199 {
200 BOOST_TEST_LOG_ENTRY( log_messages ) << "Test is aborted";
201 }
202
203 //____________________________________________________________________________//
204
205 void
206 unit_test_log_t::test_unit_start( test_unit const& tu )
207 {
208 if( s_log_impl().has_entry_in_progress() )
209 *this << log::end();
210 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
211 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() > log_test_units )
212 continue;
213 current_logger_data.m_log_formatter->test_unit_start( current_logger_data.stream(), tu );
214 }
215 }
216
217 //____________________________________________________________________________//
218
219 void
220 unit_test_log_t::test_unit_finish( test_unit const& tu, unsigned long elapsed )
221 {
222 s_log_impl().m_checkpoint_data.clear();
223
224 if( s_log_impl().has_entry_in_progress() )
225 *this << log::end();
226
227 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
228
229 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() > log_test_units )
230 continue;
231
232 current_logger_data.m_log_formatter->test_unit_finish( current_logger_data.stream(), tu, elapsed );
233 }
234 }
235
236 //____________________________________________________________________________//
237
238 void
239 unit_test_log_t::test_unit_skipped( test_unit const& tu, const_string reason )
240 {
241 if( s_log_impl().has_entry_in_progress() )
242 *this << log::end();
243
244 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
245 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() > log_test_units )
246 continue;
247
248 current_logger_data.m_log_formatter->test_unit_skipped( current_logger_data.stream(), tu, reason );
249 }
250 }
251
252 void
253 unit_test_log_t::test_unit_aborted( test_unit const& tu )
254 {
255 if( s_log_impl().has_entry_in_progress() )
256 *this << log::end();
257
258 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
259 if( !current_logger_data.m_enabled || current_logger_data.get_log_level() > log_test_units )
260 continue;
261
262 current_logger_data.m_log_formatter->test_unit_aborted(current_logger_data.stream(), tu );
263 }
264 }
265
266 //____________________________________________________________________________//
267
268 void
269 unit_test_log_t::exception_caught( execution_exception const& ex )
270 {
271 log_level l =
272 ex.code() <= execution_exception::cpp_exception_error ? log_cpp_exception_errors :
273 (ex.code() <= execution_exception::timeout_error ? log_system_errors
274 : log_fatal_errors );
275
276 if( s_log_impl().has_entry_in_progress() )
277 *this << log::end();
278
279 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
280
281 if( current_logger_data.m_enabled && l >= current_logger_data.get_log_level() ) {
282
283 current_logger_data.m_log_formatter->log_exception_start( current_logger_data.stream(), s_log_impl().m_checkpoint_data, ex );
284
285 log_entry_context( l );
286
287 current_logger_data.m_log_formatter->log_exception_finish( current_logger_data.stream() );
288 }
289 }
290 clear_entry_context();
291 }
292
293 //____________________________________________________________________________//
294
295 void
296 unit_test_log_t::set_checkpoint( const_string file, std::size_t line_num, const_string msg )
297 {
298 s_log_impl().set_checkpoint( file, line_num, msg );
299 }
300
301 //____________________________________________________________________________//
302
303 char
304 set_unix_slash( char in )
305 {
306 return in == '\\' ? '/' : in;
307 }
308
309 unit_test_log_t&
310 unit_test_log_t::operator<<( log::begin const& b )
311 {
312 if( s_log_impl().has_entry_in_progress() )
313 *this << log::end();
314
315 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
316 if( current_logger_data.m_enabled ) {
317 current_logger_data.m_stream_state_saver->restore();
318 }
319 }
320
321 s_log_impl().m_entry_data.clear();
322
323 assign_op( s_log_impl().m_entry_data.m_file_name, b.m_file_name, 0 );
324
325 // normalize file name
326 std::transform( s_log_impl().m_entry_data.m_file_name.begin(), s_log_impl().m_entry_data.m_file_name.end(),
327 s_log_impl().m_entry_data.m_file_name.begin(),
328 &set_unix_slash );
329
330 s_log_impl().m_entry_data.m_line_num = b.m_line_num;
331
332 return *this;
333 }
334
335 //____________________________________________________________________________//
336
337 unit_test_log_t&
338 unit_test_log_t::operator<<( log::end const& )
339 {
340 if( s_log_impl().has_entry_in_progress() ) {
341 log_entry_context( s_log_impl().m_entry_data.m_level );
342
343 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
344 if( current_logger_data.m_enabled && current_logger_data.m_entry_in_progress ) {
345 current_logger_data.m_log_formatter->log_entry_finish( current_logger_data.stream() );
346 }
347 current_logger_data.m_entry_in_progress = false;
348 }
349 }
350
351 clear_entry_context();
352
353 return *this;
354 }
355
356 //____________________________________________________________________________//
357
358 unit_test_log_t&
359 unit_test_log_t::operator<<( log_level l )
360 {
361 s_log_impl().m_entry_data.m_level = l;
362
363 return *this;
364 }
365
366 //____________________________________________________________________________//
367
368 ut_detail::entry_value_collector
369 unit_test_log_t::operator()( log_level l )
370 {
371 *this << l;
372
373 return ut_detail::entry_value_collector();
374 }
375
376 //____________________________________________________________________________//
377
378 bool
379 unit_test_log_t::log_entry_start(output_format log_format)
380 {
381 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
382
383 if( current_logger_data.m_format != log_format )
384 continue;
385
386 if( current_logger_data.m_entry_in_progress )
387 return true;
388
389 if( !current_logger_data.m_enabled )
390 return false;
391
392 switch( s_log_impl().m_entry_data.m_level ) {
393 case log_successful_tests:
394 current_logger_data.m_log_formatter->log_entry_start( current_logger_data.stream(), s_log_impl().m_entry_data,
395 unit_test_log_formatter::BOOST_UTL_ET_INFO );
396 break;
397 case log_messages:
398 current_logger_data.m_log_formatter->log_entry_start( current_logger_data.stream(), s_log_impl().m_entry_data,
399 unit_test_log_formatter::BOOST_UTL_ET_MESSAGE );
400 break;
401 case log_warnings:
402 current_logger_data.m_log_formatter->log_entry_start( current_logger_data.stream(), s_log_impl().m_entry_data,
403 unit_test_log_formatter::BOOST_UTL_ET_WARNING );
404 break;
405 case log_all_errors:
406 case log_cpp_exception_errors:
407 case log_system_errors:
408 current_logger_data.m_log_formatter->log_entry_start( current_logger_data.stream(), s_log_impl().m_entry_data,
409 unit_test_log_formatter::BOOST_UTL_ET_ERROR );
410 break;
411 case log_fatal_errors:
412 current_logger_data.m_log_formatter->log_entry_start( current_logger_data.stream(), s_log_impl().m_entry_data,
413 unit_test_log_formatter::BOOST_UTL_ET_FATAL_ERROR );
414 break;
415 case log_nothing:
416 case log_test_units:
417 case invalid_log_level:
418 return false;
419 }
420
421 current_logger_data.m_entry_in_progress = true;
422 return true;
423 }
424
425 return false;
426 }
427
428 //____________________________________________________________________________//
429
430 unit_test_log_t&
431 unit_test_log_t::operator<<( const_string value )
432 {
433 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
434 if( current_logger_data.m_enabled && s_log_impl().m_entry_data.m_level >= current_logger_data.get_log_level() && !value.empty() && log_entry_start(current_logger_data.m_format) )
435 current_logger_data.m_log_formatter->log_entry_value( current_logger_data.stream(), value );
436
437 }
438 return *this;
439 }
440
441 //____________________________________________________________________________//
442
443 unit_test_log_t&
444 unit_test_log_t::operator<<( lazy_ostream const& value )
445 {
446 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
447 if( current_logger_data.m_enabled && s_log_impl().m_entry_data.m_level >= current_logger_data.get_log_level() && !value.empty() ) {
448 if( log_entry_start(current_logger_data.m_format) ) {
449 current_logger_data.m_log_formatter->log_entry_value( current_logger_data.stream(), value );
450 }
451 }
452 }
453 return *this;
454 }
455
456 //____________________________________________________________________________//
457
458 void
459 unit_test_log_t::log_entry_context( log_level l )
460 {
461 framework::context_generator const& context = framework::get_context();
462 if( context.is_empty() )
463 return;
464
465 const_string frame;
466
467 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
468 if( current_logger_data.m_enabled ) {
469 current_logger_data.m_log_formatter->entry_context_start( current_logger_data.stream(), l );
470 }
471 }
472
473 while( !(frame=context.next()).is_empty() )
474 {
475 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
476 if( current_logger_data.m_enabled ) {
477 current_logger_data.m_log_formatter->log_entry_context( current_logger_data.stream(), l, frame );
478 }
479 }
480 }
481
482 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
483 if( current_logger_data.m_enabled ) {
484 current_logger_data.m_log_formatter->entry_context_finish( current_logger_data.stream(), l );
485 }
486 }
487 }
488
489 //____________________________________________________________________________//
490
491 void
492 unit_test_log_t::clear_entry_context()
493 {
494 framework::clear_context();
495 }
496
497 //____________________________________________________________________________//
498
499 void
500 unit_test_log_t::set_stream( std::ostream& str )
501 {
502 if( s_log_impl().has_entry_in_progress() )
503 return;
504
505 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
506 current_logger_data.m_stream = &str;
507 current_logger_data.m_stream_state_saver.reset( new io_saver_type( str ) );
508 }
509 }
510
511 //____________________________________________________________________________//
512
513 void
514 unit_test_log_t::set_stream( output_format log_format, std::ostream& str )
515 {
516 if( s_log_impl().has_entry_in_progress() )
517 return;
518
519 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
520 if( current_logger_data.m_format == log_format) {
521 current_logger_data.m_stream = &str;
522 current_logger_data.m_stream_state_saver.reset( new io_saver_type( str ) );
523 break;
524 }
525 }
526 }
527
528 std::ostream*
529 unit_test_log_t::get_stream( output_format log_format ) const
530 {
531 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
532 if( current_logger_data.m_format == log_format) {
533 return current_logger_data.m_stream;
534 }
535 }
536 return 0;
537 }
538
539 //____________________________________________________________________________//
540
541 void
542 unit_test_log_t::set_threshold_level( log_level lev )
543 {
544 if( s_log_impl().has_entry_in_progress() || lev == invalid_log_level )
545 return;
546
547 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
548 current_logger_data.m_log_formatter->set_log_level( lev );
549 }
550 }
551
552 //____________________________________________________________________________//
553
554 void
555 unit_test_log_t::set_threshold_level( output_format log_format, log_level lev )
556 {
557 if( s_log_impl().has_entry_in_progress() || lev == invalid_log_level )
558 return;
559
560 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
561 if( current_logger_data.m_format == log_format) {
562 current_logger_data.m_log_formatter->set_log_level( lev );
563 break;
564 }
565 }
566 }
567
568 //____________________________________________________________________________//
569
570 void
571 unit_test_log_t::set_format( output_format log_format )
572 {
573 if( s_log_impl().has_entry_in_progress() )
574 return;
575
576 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
577 current_logger_data.m_enabled = current_logger_data.m_format == log_format;
578 }
579 }
580
581 //____________________________________________________________________________//
582
583 void
584 unit_test_log_t::add_format( output_format log_format )
585 {
586 if( s_log_impl().has_entry_in_progress() )
587 return;
588
589 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
590 if( current_logger_data.m_format == log_format) {
591 current_logger_data.m_enabled = true;
592 break;
593 }
594 }
595 }
596
597 //____________________________________________________________________________//
598
599 unit_test_log_formatter*
600 unit_test_log_t::get_formatter( output_format log_format ) {
601 BOOST_TEST_FOREACH( unit_test_log_data_helper_impl&, current_logger_data, s_log_impl().m_log_formatter_data ) {
602 if( current_logger_data.m_format == log_format) {
603 return current_logger_data.m_log_formatter.get();
604 }
605 }
606 return 0;
607 }
608
609
610 void
611 unit_test_log_t::add_formatter( unit_test_log_formatter* the_formatter )
612 {
613 // remove only user defined logger
614 for(unit_test_log_impl::v_formatter_data_t::iterator it(s_log_impl().m_log_formatter_data.begin()),
615 ite(s_log_impl().m_log_formatter_data.end());
616 it != ite;
617 ++it)
618 {
619 if( it->m_format == OF_CUSTOM_LOGGER) {
620 s_log_impl().m_log_formatter_data.erase(it);
621 break;
622 }
623 }
624
625 if( the_formatter ) {
626 s_log_impl().m_log_formatter_data.push_back( unit_test_log_data_helper_impl(the_formatter, OF_CUSTOM_LOGGER, true) );
627 }
628 }
629
630 void
631 unit_test_log_t::set_formatter( unit_test_log_formatter* the_formatter )
632 {
633 // remove only user defined logger
634 log_level current_level = invalid_log_level;
635 std::ostream *current_stream = 0;
636 output_format previous_format = OF_INVALID;
637 for(unit_test_log_impl::v_formatter_data_t::iterator it(s_log_impl().m_log_formatter_data.begin()),
638 ite(s_log_impl().m_log_formatter_data.end());
639 it != ite;
640 ++it)
641 {
642 if( it->m_enabled ) {
643 if( current_level == invalid_log_level || it->m_format < previous_format || it->m_format == OF_CUSTOM_LOGGER) {
644 current_level = it->get_log_level();
645 current_stream = &(it->stream());
646 previous_format = it->m_format;
647 }
648 }
649 }
650
651 if( the_formatter ) {
652 add_formatter(the_formatter);
653 set_format(OF_CUSTOM_LOGGER);
654 set_threshold_level(OF_CUSTOM_LOGGER, current_level);
655 set_stream(OF_CUSTOM_LOGGER, *current_stream);
656 }
657 }
658
659 //____________________________________________________________________________//
660
661 // ************************************************************************** //
662 // ************** unit_test_log_formatter ************** //
663 // ************************************************************************** //
664
665 void
666 unit_test_log_formatter::log_entry_value( std::ostream& ostr, lazy_ostream const& value )
667 {
668 log_entry_value( ostr, (wrap_stringstream().ref() << value).str() );
669 }
670
671 void
672 unit_test_log_formatter::set_log_level(log_level new_log_level)
673 {
674 m_log_level = new_log_level;
675 }
676
677 log_level
678 unit_test_log_formatter::get_log_level() const
679 {
680 return m_log_level;
681 }
682
683 //____________________________________________________________________________//
684
685 } // namespace unit_test
686 } // namespace boost
687
688 #include <boost/test/detail/enable_warnings.hpp>
689
690 #endif // BOOST_TEST_UNIT_TEST_LOG_IPP_012205GER
691