source: NonGTP/Boost/boost/test/impl/unit_test_log.ipp @ 857

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