summaryrefslogtreecommitdiff
path: root/boost/test/impl/junit_log_formatter.ipp
blob: dd528bc9037add8ebc1a9ca1bcb6b23d8d1907d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//  (C) Copyright 2016 Raffi Enficiaud.
//  Distributed under the Boost Software License, Version 1.0.
//  (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

//  See http://www.boost.org/libs/test for the library home page.
//
///@file
///@brief Contains the implementatoin of the Junit log formatter (OF_JUNIT)
// ***************************************************************************

#ifndef BOOST_TEST_JUNIT_LOG_FORMATTER_IPP__
#define BOOST_TEST_JUNIT_LOG_FORMATTER_IPP__

// Boost.Test
#include <boost/test/output/junit_log_formatter.hpp>
#include <boost/test/execution_monitor.hpp>
#include <boost/test/framework.hpp>
#include <boost/test/tree/test_unit.hpp>
#include <boost/test/utils/basic_cstring/io.hpp>
#include <boost/test/utils/xml_printer.hpp>
#include <boost/test/utils/string_cast.hpp>
#include <boost/test/framework.hpp>

#include <boost/test/tree/visitor.hpp>
#include <boost/test/tree/test_case_counter.hpp>
#include <boost/test/tree/traverse.hpp>
#include <boost/test/results_collector.hpp>

#include <boost/test/utils/algorithm.hpp>
#include <boost/test/utils/string_cast.hpp>

//#include <boost/test/results_reporter.hpp>


// Boost
#include <boost/version.hpp>

// STL
#include <iostream>
#include <fstream>
#include <set>

#include <boost/test/detail/suppress_warnings.hpp>


//____________________________________________________________________________//

namespace boost {
namespace unit_test {
namespace output {


struct s_replace_chars {
  template <class T>
  void operator()(T& to_replace)
  {
    if(to_replace == '/')
      to_replace = '.';
    else if(to_replace == ' ')
      to_replace = '_';
  }
};

inline std::string tu_name_normalize(std::string full_name)
{
  // maybe directly using normalize_test_case_name instead?
  std::for_each(full_name.begin(), full_name.end(), s_replace_chars());
  return full_name;
}

const_string file_basename(const_string filename) {

    const_string path_sep( "\\/" );
    const_string::iterator it = unit_test::utils::find_last_of( filename.begin(), filename.end(),
                                                                path_sep.begin(), path_sep.end() );
    if( it != filename.end() )
        filename.trim_left( it + 1 );

    return filename;

}

// ************************************************************************** //
// **************               junit_log_formatter              ************** //
// ************************************************************************** //

void
junit_log_formatter::log_start( std::ostream& ostr, counter_t test_cases_amount)
{
    map_tests.clear();
    list_path_to_root.clear();
    runner_log_entry.clear();
}

//____________________________________________________________________________//

class junit_result_helper : public test_tree_visitor {
public:
    explicit junit_result_helper(
        std::ostream& stream,
        test_unit const& ts,
        junit_log_formatter::map_trace_t const& mt,
        junit_impl::junit_log_helper const& runner_log_,
        bool display_build_info )
    : m_stream(stream)
    , m_ts( ts )
    , m_map_test( mt )
    , runner_log( runner_log_ )
    , m_id( 0 )
    , m_display_build_info(display_build_info)
    { }

    void add_log_entry(std::string const& entry_type,
                       test_case const& tc,
                       junit_impl::junit_log_helper::assertion_entry const& log) const
    {
        m_stream
            << "<" << entry_type
            << " message" << utils::attr_value() << log.logentry_message
            << " type" << utils::attr_value() << log.logentry_type
            << ">";

        if(!log.output.empty()) {
            m_stream << utils::cdata() << "\n" + log.output;
        }

        m_stream << "</" << entry_type << ">";
    }

    struct conditional_cdata_helper {
        std::ostream &ostr;
        std::string const field;
        bool empty;

        conditional_cdata_helper(std::ostream &ostr_, std::string field_)
        : ostr(ostr_)
        , field(field_)
        , empty(true)
        {}

        ~conditional_cdata_helper() {
            if(!empty) {
                ostr << BOOST_TEST_L( "]]>" ) << "</" << field << '>' << std::endl;
            }
        }

        void operator()(const std::string& s) {
            bool current_empty = s.empty();
            if(empty) {
                if(!current_empty) {
                    empty = false;
                    ostr << '<' << field << '>' << BOOST_TEST_L( "<![CDATA[" );
                }
            }
            if(!current_empty) {
                ostr << s;
            }
        }
    };

    std::list<std::string> build_skipping_chain(test_case const & tc) const
    {
        // we enter here because we know that the tc has been skipped.
        // either junit has not seen this tc, or it is indicated as disabled
        assert(m_map_test.count(tc.p_id) == 0 || results_collector.results( tc.p_id ).p_skipped);

        std::list<std::string> out;

        test_unit_id id(tc.p_id);
        while( id != m_ts.p_id && id != INV_TEST_UNIT_ID) {
            test_unit const& tu = boost::unit_test::framework::get( id, TUT_ANY );
            out.push_back("- disabled test unit: '" + tu.full_name() + "'\n");
            if(m_map_test.count(id) > 0)
            {
                // junit has seen the reason: this is enough for constructing the chain
                break;
            }
            id = tu.p_parent_id;
        }
        junit_log_formatter::map_trace_t::const_iterator it_element_stack(m_map_test.find(id));
        if( it_element_stack != m_map_test.end() )
        {
            out.push_back("- reason: '" + it_element_stack->second.skipping_reason + "'");
            out.push_front("Test case disabled because of the following chain of decision:\n");
        }

        return out;
    }

    std::string get_class_name(test_case const & tc) const {
        std::string classname;
        test_unit_id id(tc.p_parent_id);
        while( id != m_ts.p_id && id != INV_TEST_UNIT_ID ) {
            test_unit const& tu = boost::unit_test::framework::get( id, TUT_ANY );
            classname = tu_name_normalize(tu.p_name) + "." + classname;
            id = tu.p_parent_id;
        }

        // removes the trailing dot
        if(!classname.empty() && *classname.rbegin() == '.') {
            classname.erase(classname.size()-1);
        }

        return classname;
    }

    void    write_testcase_header(test_case const & tc,
                                  test_results const *tr = 0) const
    {
        //
        // test case header

        // total number of assertions
        m_stream << "<testcase assertions" << utils::attr_value() << tr->p_assertions_passed + tr->p_assertions_failed;

        // class name
        const std::string classname = get_class_name(tc);
        if(!classname.empty())
            m_stream << " classname" << utils::attr_value() << classname;

        // test case name and time taken
        m_stream
            << " name"      << utils::attr_value() << tu_name_normalize(tc.p_name)
            << " time"      << utils::attr_value() << double(tr->p_duration_microseconds) * 1E-6
            << ">" << std::endl;
    }

    void    write_testcase_system_out(junit_impl::junit_log_helper const &detailed_log,
                                      test_case const * tc,
                                      bool skipped,
                                      test_results const *tr = 0) const
    {
        // system-out + all info/messages, the object skips the empty entries
        conditional_cdata_helper system_out_helper(m_stream, "system-out");

        // indicate why the test has been skipped first
        if( skipped ) {
            std::list<std::string> skipping_decision_chain = build_skipping_chain(*tc);
            for(std::list<std::string>::const_iterator it(skipping_decision_chain.begin()), ite(skipping_decision_chain.end());
                it != ite;
                ++it)
            {
              system_out_helper(*it);
            }
        }

        // stdout
        for(std::list<std::string>::const_iterator it(detailed_log.system_out.begin()), ite(detailed_log.system_out.end());
            it != ite;
            ++it)
        {
          system_out_helper(*it);
        }

        // warning/info message last
        for(std::vector< junit_impl::junit_log_helper::assertion_entry >::const_iterator it(detailed_log.assertion_entries.begin());
            it != detailed_log.assertion_entries.end();
            ++it)
        {
            if(it->log_entry != junit_impl::junit_log_helper::assertion_entry::log_entry_info)
                continue;
            system_out_helper(it->output);
        }
    }

    void    write_testcase_system_err(junit_impl::junit_log_helper const &detailed_log,
                                      test_case const * tc,
                                      test_results const *tr = 0) const
    {
        // system-err output + test case informations
        bool has_failed = (tr != 0) ? !tr->passed() : false;
        if(!detailed_log.system_err.empty() || has_failed)
        {
            conditional_cdata_helper system_err_helper(m_stream, "system-err");
            std::ostringstream o;
            if(has_failed) {
                o << "Failures detected in:" << std::endl;
            }
            else {
                o << "ERROR STREAM:" << std::endl;
            }

            o << "- test case: " << tc->full_name() << std::endl;
            if(!tc->p_description.value.empty())
                o << " '" << tc->p_description << "'";

            o << std::endl
                << "- file: " << file_basename(tc->p_file_name) << std::endl
                << "- line: " << tc->p_line_num << std::endl
                ;

            if(!detailed_log.system_err.empty())
                o << std::endl << "STDERR BEGIN: ------------" << std::endl;

            system_err_helper(o.str());
            for(std::list<std::string>::const_iterator it(detailed_log.system_err.begin()), ite(detailed_log.system_err.end());
                it != ite;
                ++it)
            {
              system_err_helper(*it);
            }

            if(!detailed_log.system_err.empty())
                o << std::endl << "STDERR END    ------------" << std::endl;
        }
    }

    void    output_detailed_logs(junit_impl::junit_log_helper const &detailed_log,
                                 test_case const & tc,
                                 bool skipped,
                                 test_results const *tr = 0) const
    {
        write_testcase_header(tc, tr);

        if( skipped ) {
            m_stream << "<skipped/>" << std::endl;
        }
        else {

          for(std::vector< junit_impl::junit_log_helper::assertion_entry >::const_iterator it(detailed_log.assertion_entries.begin());
              it != detailed_log.assertion_entries.end();
              ++it)
          {
              if(it->log_entry == junit_impl::junit_log_helper::assertion_entry::log_entry_failure) {
                  add_log_entry("failure", tc, *it);
              }
              else if(it->log_entry == junit_impl::junit_log_helper::assertion_entry::log_entry_error) {
                  add_log_entry("error", tc, *it);
              }
          }
        }

        write_testcase_system_out(detailed_log, &tc, skipped, tr);
        write_testcase_system_err(detailed_log, &tc, tr);
        m_stream << "</testcase>" << std::endl;
    }

    void    visit( test_case const& tc )
    {

        test_results const& tr = results_collector.results( tc.p_id );
        junit_log_formatter::map_trace_t::const_iterator it_find = m_map_test.find(tc.p_id);
        if(it_find == m_map_test.end())
        {
            // test has been skipped and not seen by the logger
            output_detailed_logs(junit_impl::junit_log_helper(), tc, true, &tr);
        }
        else {
            output_detailed_logs(it_find->second, tc, tr.p_skipped, &tr);
        }
    }

    bool    test_suite_start( test_suite const& ts )
    {
        // unique test suite, without s, nesting not supported in CI
        if( m_ts.p_id != ts.p_id )
            return true;

        test_results const& tr = results_collector.results( ts.p_id );
        m_stream << "<testsuite";

        m_stream
          // << "disabled=\"" << tr.p_test_cases_skipped << "\" "
          << " tests"     << utils::attr_value() << tr.p_test_cases_passed
          << " skipped"   << utils::attr_value() << tr.p_test_cases_skipped
          << " errors"    << utils::attr_value() << tr.p_test_cases_aborted
          << " failures"  << utils::attr_value() << tr.p_test_cases_failed
          << " id"        << utils::attr_value() << m_id++
          << " name"      << utils::attr_value() << tu_name_normalize(ts.p_name)
          << " time"      << utils::attr_value() << (tr.p_duration_microseconds * 1E-6)
          << ">" << std::endl;

        if(m_display_build_info)
        {
            m_stream  << "<properties>" << std::endl;
            m_stream  << "<property name=\"platform\" value" << utils::attr_value() << BOOST_PLATFORM << std::endl;
            m_stream  << "<property name=\"compiler\" value" << utils::attr_value() << BOOST_COMPILER << std::endl;
            m_stream  << "<property name=\"stl\" value" << utils::attr_value() << BOOST_STDLIB << std::endl;

            std::ostringstream o;
            o << BOOST_VERSION/100000 << "." << BOOST_VERSION/100 % 1000 << "." << BOOST_VERSION % 100;
            m_stream  << "<property name=\"boost\" value" << utils::attr_value() << o.str() << std::endl;
            m_stream  << "</properties>" << std::endl;
        }

        return true; // indicates that the children should also be parsed
    }

    virtual void    test_suite_finish( test_suite const& ts )
    {
        if( m_ts.p_id != ts.p_id )
            return;

        write_testcase_system_out(runner_log, 0, false, 0);
        write_testcase_system_err(runner_log, 0, 0);

        m_stream << "</testsuite>";
    }

private:
    // Data members
    std::ostream& m_stream;
    test_unit const& m_ts;
    junit_log_formatter::map_trace_t const& m_map_test;
    junit_impl::junit_log_helper const& runner_log;
    size_t m_id;
    bool m_display_build_info;
};



void
junit_log_formatter::log_finish( std::ostream& ostr )
{
    ostr << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;

    // getting the root test suite
    if(!map_tests.empty()) {
        test_unit* root = &boost::unit_test::framework::get( map_tests.begin()->first, TUT_ANY );

        // looking for the root of the SUBtree (we stay in the subtree)
        while(root->p_parent_id != INV_TEST_UNIT_ID && map_tests.count(root->p_parent_id) > 0) {
            root = &boost::unit_test::framework::get( root->p_parent_id, TUT_ANY );
        }
        junit_result_helper ch( ostr, *root, map_tests, this->runner_log_entry, m_display_build_info );
        traverse_test_tree( root->p_id, ch, true ); // last is to ignore disabled suite special handling
    }
    else {
        ostr << "<testsuites errors=\"1\">";
        ostr << "<testsuite errors=\"1\" name=\"boost-test-framework\">";
        ostr << "<testcase assertions=\"1\" name=\"test-setup\">";
        ostr << "<system-out>Incorrect setup: no test case executed</system-out>";
        ostr << "</testcase></testsuite></testsuites>";
    }
    return;
}

//____________________________________________________________________________//

void
junit_log_formatter::log_build_info( std::ostream& ostr )
{
    m_display_build_info = true;
}

//____________________________________________________________________________//

void
junit_log_formatter::test_unit_start( std::ostream& ostr, test_unit const& tu )
{
    list_path_to_root.push_back( tu.p_id );
    map_tests.insert(std::make_pair(tu.p_id, junit_impl::junit_log_helper())); // current_test_case_id not working here
}



//____________________________________________________________________________//

void
junit_log_formatter::test_unit_finish( std::ostream& ostr, test_unit const& tu, unsigned long elapsed )
{
    // the time is already stored in the result_reporter
    assert( tu.p_id == list_path_to_root.back() );
    list_path_to_root.pop_back();
}

void
junit_log_formatter::test_unit_aborted( std::ostream& os, test_unit const& tu )
{
    assert( tu.p_id == list_path_to_root.back() );
    //list_path_to_root.pop_back();
}

//____________________________________________________________________________//

void
junit_log_formatter::test_unit_skipped( std::ostream& ostr, test_unit const& tu, const_string reason )
{
    // if a test unit is skipped, then the start of this TU has not been called yet.
    // we cannot use get_current_log_entry here, but the TU id should appear in the map.
    // The "skip" boolean is given by the boost.test framework
    junit_impl::junit_log_helper& v = map_tests[tu.p_id]; // not sure if we can use get_current_log_entry()
    v.skipping_reason.assign(reason.begin(), reason.end());
}

//____________________________________________________________________________//

void
junit_log_formatter::log_exception_start( std::ostream& ostr, log_checkpoint_data const& checkpoint_data, execution_exception const& ex )
{
    std::ostringstream o;
    execution_exception::location const& loc = ex.where();

    m_is_last_assertion_or_error = false;

    junit_impl::junit_log_helper& last_entry = get_current_log_entry();

    junit_impl::junit_log_helper::assertion_entry entry;

    entry.logentry_message = "unexpected exception";
    entry.log_entry = junit_impl::junit_log_helper::assertion_entry::log_entry_error;

    switch(ex.code())
    {
    case execution_exception::cpp_exception_error:
        entry.logentry_type = "uncaught exception";
        break;
    case execution_exception::timeout_error:
        entry.logentry_type = "execution timeout";
        break;
    case execution_exception::user_error:
        entry.logentry_type = "user, assert() or CRT error";
        break;
    case execution_exception::user_fatal_error:
        // Looks like never used
        entry.logentry_type = "user fatal error";
        break;
    case execution_exception::system_error:
        entry.logentry_type = "system error";
        break;
    case execution_exception::system_fatal_error:
        entry.logentry_type = "system fatal error";
        break;
    default:
        entry.logentry_type = "no error"; // not sure how to handle this one
        break;
    }

    o << "UNCAUGHT EXCEPTION:" << std::endl;
    if( !loc.m_function.is_empty() )
        o << "- function: \""   << loc.m_function << "\"" << std::endl;

    o << "- file: " << file_basename(loc.m_file_name) << std::endl
      << "- line: " << loc.m_line_num << std::endl
      << std::endl;

    o << "\nEXCEPTION STACK TRACE: --------------\n" << ex.what()
      << "\n-------------------------------------";

    if( !checkpoint_data.m_file_name.is_empty() ) {
        o << std::endl << std::endl
          << "Last checkpoint:" << std::endl
          << "- message: \"" << checkpoint_data.m_message << "\"" << std::endl
          << "- file: " << file_basename(checkpoint_data.m_file_name) << std::endl
          << "- line: " << checkpoint_data.m_line_num << std::endl
        ;
    }

    entry.output = o.str();

    last_entry.assertion_entries.push_back(entry);
}

//____________________________________________________________________________//

void
junit_log_formatter::log_exception_finish( std::ostream& ostr )
{
    // sealing the last entry
    assert(!get_current_log_entry().assertion_entries.back().sealed);
    get_current_log_entry().assertion_entries.back().sealed = true;
}

//____________________________________________________________________________//

void
junit_log_formatter::log_entry_start( std::ostream& ostr, log_entry_data const& entry_data, log_entry_types let )
{
    junit_impl::junit_log_helper& last_entry = get_current_log_entry();
    last_entry.skipping = false;
    m_is_last_assertion_or_error = true;
    switch(let)
    {
      case unit_test_log_formatter::BOOST_UTL_ET_INFO:
      {
        if(m_log_level_internal > log_successful_tests) {
          last_entry.skipping = true;
          break;
        }
        // no break on purpose
      }
      case unit_test_log_formatter::BOOST_UTL_ET_MESSAGE:
      {
        if(m_log_level_internal > log_messages) {
          last_entry.skipping = true;
          break;
        }
        // no break on purpose
      }
      case unit_test_log_formatter::BOOST_UTL_ET_WARNING:
      {
        if(m_log_level_internal > log_warnings) {
          last_entry.skipping = true;
          break;
        }
        std::ostringstream o;
        junit_impl::junit_log_helper::assertion_entry entry;

        entry.log_entry = junit_impl::junit_log_helper::assertion_entry::log_entry_info;
        entry.logentry_message = "info";
        entry.logentry_type = "message";

        o << (let == unit_test_log_formatter::BOOST_UTL_ET_WARNING ?
              "WARNING:" : (let == unit_test_log_formatter::BOOST_UTL_ET_MESSAGE ?
                            "MESSAGE:" : "INFO:"))
             << std::endl
          << "- file   : " << file_basename(entry_data.m_file_name) << std::endl
          << "- line   : " << entry_data.m_line_num << std::endl
          << "- message: "; // no CR

        entry.output += o.str();
        last_entry.assertion_entries.push_back(entry);
        break;
      }
      default:
      case unit_test_log_formatter::BOOST_UTL_ET_ERROR:
      case unit_test_log_formatter::BOOST_UTL_ET_FATAL_ERROR:
      {
        std::ostringstream o;
        junit_impl::junit_log_helper::assertion_entry entry;
        entry.log_entry = junit_impl::junit_log_helper::assertion_entry::log_entry_failure;
        entry.logentry_message = "failure";
        entry.logentry_type = (let == unit_test_log_formatter::BOOST_UTL_ET_ERROR ? "assertion error" : "fatal error");

        o << "ASSERTION FAILURE:" << std::endl
          << "- file   : " << file_basename(entry_data.m_file_name) << std::endl
          << "- line   : " << entry_data.m_line_num << std::endl
          << "- message: " ; // no CR

        entry.output += o.str();
        last_entry.assertion_entries.push_back(entry);
        break;
      }
    }
}

//____________________________________________________________________________//

void
junit_log_formatter::log_entry_value( std::ostream& ostr, const_string value )
{
    junit_impl::junit_log_helper& last_entry = get_current_log_entry();
    if(last_entry.skipping)
        return;

    assert(last_entry.assertion_entries.empty() || !last_entry.assertion_entries.back().sealed);

    if(!last_entry.assertion_entries.empty())
    {
        junit_impl::junit_log_helper::assertion_entry& log_entry = last_entry.assertion_entries.back();
        log_entry.output += value;
    }
    else
    {
        // this may be a message coming from another observer
        // the prefix is set in the log_entry_start
        last_entry.system_out.push_back(std::string(value.begin(), value.end()));
    }
}

//____________________________________________________________________________//

void
junit_log_formatter::log_entry_finish( std::ostream& ostr )
{
    junit_impl::junit_log_helper& last_entry = get_current_log_entry();
    if(!last_entry.skipping)
    {
        assert(last_entry.assertion_entries.empty() || !last_entry.assertion_entries.back().sealed);

        if(!last_entry.assertion_entries.empty()) {
            junit_impl::junit_log_helper::assertion_entry& log_entry = last_entry.assertion_entries.back();
            log_entry.output += "\n\n"; // quote end, CR
            log_entry.sealed = true;
        }
        else {
            last_entry.system_out.push_back("\n\n"); // quote end, CR
        }
    }

    last_entry.skipping = false;
}

//____________________________________________________________________________//

void
junit_log_formatter::entry_context_start( std::ostream& ostr, log_level )
{
    junit_impl::junit_log_helper& last_entry = get_current_log_entry();
    if(last_entry.skipping)
        return;

    std::vector< junit_impl::junit_log_helper::assertion_entry > &v_failure_or_error = last_entry.assertion_entries;
    assert(!v_failure_or_error.back().sealed);

    junit_impl::junit_log_helper::assertion_entry& last_log_entry = v_failure_or_error.back();
    if(m_is_last_assertion_or_error)
    {
        last_log_entry.output += "\n- context:\n";
    }
    else
    {
        last_log_entry.output += "\n\nCONTEXT:\n";
    }
}

//____________________________________________________________________________//

void
junit_log_formatter::entry_context_finish( std::ostream& ostr )
{
    // no op, may be removed
    junit_impl::junit_log_helper& last_entry = get_current_log_entry();
    if(last_entry.skipping)
        return;
    assert(!get_current_log_entry().assertion_entries.back().sealed);
}

//____________________________________________________________________________//

void
junit_log_formatter::log_entry_context( std::ostream& ostr, const_string context_descr )
{
    junit_impl::junit_log_helper& last_entry = get_current_log_entry();
    if(last_entry.skipping)
        return;

    assert(!last_entry.assertion_entries.back().sealed);
    junit_impl::junit_log_helper::assertion_entry& last_log_entry = get_current_log_entry().assertion_entries.back();

    last_log_entry.output +=
        (m_is_last_assertion_or_error ? "  - '": "- '") + std::string(context_descr.begin(), context_descr.end()) + "'\n"; // quote end
}

//____________________________________________________________________________//


std::string
junit_log_formatter::get_default_stream_description() const {
    std::string name = framework::master_test_suite().p_name.value;

    static const std::string to_replace[] =  { " ", "\"", "/", "\\", ":"};
    static const std::string replacement[] = { "_", "_" , "_", "_" , "_"};

    name = unit_test::utils::replace_all_occurrences_of(
        name,
        to_replace, to_replace + sizeof(to_replace)/sizeof(to_replace[0]),
        replacement, replacement + sizeof(replacement)/sizeof(replacement[0]));

    std::ifstream check_init((name + ".xml").c_str());
    if(!check_init)
        return name + ".xml";

    int index = 0;
    for(; index < 100; index++) {
      std::string candidate = name + "_" + utils::string_cast(index) + ".xml";
      std::ifstream file(candidate.c_str());
      if(!file)
          return candidate;
    }

    return name + ".xml";
}

} // namespace output
} // namespace unit_test
} // namespace boost

#include <boost/test/detail/enable_warnings.hpp>

#endif // BOOST_TEST_junit_log_formatter_IPP_020105GER