1 //  (C) Copyright Gennadiy Rozental 2001-2014.
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 singleton class unit_test_log and all manipulators.
10 /// unit_test_log has output stream like interface. It's implementation is
11 /// completely hidden with pimple idiom
12 // ***************************************************************************
13 
14 #ifndef BOOST_TEST_UNIT_TEST_LOG_HPP_071894GER
15 #define BOOST_TEST_UNIT_TEST_LOG_HPP_071894GER
16 
17 // Boost.Test
18 #include <boost/test/tree/observer.hpp>
19 
20 #include <boost/test/detail/global_typedef.hpp>
21 #include <boost/test/detail/log_level.hpp>
22 #include <boost/test/detail/fwd_decl.hpp>
23 
24 #include <boost/test/utils/wrap_stringstream.hpp>
25 #include <boost/test/utils/trivial_singleton.hpp>
26 #include <boost/test/utils/lazy_ostream.hpp>
27 
28 // Boost
29 
30 // STL
31 #include <iosfwd>   // for std::ostream&
32 
33 #include <boost/test/detail/suppress_warnings.hpp>
34 
35 //____________________________________________________________________________//
36 
37 namespace boost {
38 namespace unit_test {
39 
40 // ************************************************************************** //
41 // **************                log manipulators              ************** //
42 // ************************************************************************** //
43 
44 namespace log {
45 
46 struct BOOST_TEST_DECL begin {
beginboost::unit_test::log::begin47     begin( const_string fn, std::size_t ln )
48     : m_file_name( fn )
49     , m_line_num( ln )
50     {}
51 
52     const_string m_file_name;
53     std::size_t m_line_num;
54 };
55 
56 struct end {};
57 
58 } // namespace log
59 
60 // ************************************************************************** //
61 // **************             entry_value_collector            ************** //
62 // ************************************************************************** //
63 
64 namespace ut_detail {
65 
66 class BOOST_TEST_DECL entry_value_collector {
67 public:
68     // Constructors
entry_value_collector()69     entry_value_collector() : m_last( true ) {}
entry_value_collector(entry_value_collector const & rhs)70     entry_value_collector( entry_value_collector const& rhs ) : m_last( true ) { rhs.m_last = false; }
71     ~entry_value_collector();
72 
73     // collection interface
74     entry_value_collector const& operator<<( lazy_ostream const& ) const;
75     entry_value_collector const& operator<<( const_string ) const;
76 
77 private:
78     // Data members
79     mutable bool    m_last;
80 };
81 
82 } // namespace ut_detail
83 
84 // ************************************************************************** //
85 // **************                 unit_test_log                ************** //
86 // ************************************************************************** //
87 
88 class BOOST_TEST_DECL unit_test_log_t : public test_observer, public singleton<unit_test_log_t> {
89 public:
90     // test_observer interface implementation
91     virtual void        test_start( counter_t test_cases_amount );
92     virtual void        test_finish();
93     virtual void        test_aborted();
94 
95     virtual void        test_unit_start( test_unit const& );
96     virtual void        test_unit_finish( test_unit const&, unsigned long elapsed );
97     virtual void        test_unit_skipped( test_unit const&, const_string );
98 
99     virtual void        exception_caught( execution_exception const& ex );
100 
priority()101     virtual int         priority() { return 1; }
102 
103     // log configuration methods
104     void                set_stream( std::ostream& );
105     void                set_threshold_level( log_level );
106     void                set_format( output_format );
107     void                set_formatter( unit_test_log_formatter* );
108 
109     // test progress logging
110     void                set_checkpoint( const_string file, std::size_t line_num, const_string msg = const_string() );
111 
112     // entry logging
113     unit_test_log_t&    operator<<( log::begin const& );        // begin entry
114     unit_test_log_t&    operator<<( log::end const& );          // end entry
115     unit_test_log_t&    operator<<( log_level );                // set entry level
116     unit_test_log_t&    operator<<( const_string );             // log entry value
117     unit_test_log_t&    operator<<( lazy_ostream const& );      // log entry value
118 
119     ut_detail::entry_value_collector operator()( log_level );   // initiate entry collection
120 
121 private:
122     // Implementation helpers
123     bool                log_entry_start();
124     void                log_entry_context( log_level l );
125     void                clear_entry_context();
126 
127     BOOST_TEST_SINGLETON_CONS( unit_test_log_t )
128 }; // unit_test_log_t
129 
130 BOOST_TEST_SINGLETON_INST( unit_test_log )
131 
132 // helper macros
133 #define BOOST_TEST_LOG_ENTRY( ll )                                                  \
134     (::boost::unit_test::unit_test_log                                              \
135         << ::boost::unit_test::log::begin( BOOST_TEST_L(__FILE__), __LINE__ ))(ll)  \
136 /**/
137 
138 } // namespace unit_test
139 } // namespace boost
140 
141 // ************************************************************************** //
142 // **************       Unit test log interface helpers        ************** //
143 // ************************************************************************** //
144 
145 #define BOOST_TEST_MESSAGE( M )                                 \
146     BOOST_TEST_LOG_ENTRY( ::boost::unit_test::log_messages )    \
147     << BOOST_TEST_LAZY_MSG( M )                                 \
148 /**/
149 
150 //____________________________________________________________________________//
151 
152 #define BOOST_TEST_PASSPOINT()                                  \
153     ::boost::unit_test::unit_test_log.set_checkpoint(           \
154         BOOST_TEST_L(__FILE__),                                 \
155         static_cast<std::size_t>(__LINE__) )                    \
156 /**/
157 
158 //____________________________________________________________________________//
159 
160 #define BOOST_TEST_CHECKPOINT( M )                              \
161     ::boost::unit_test::unit_test_log.set_checkpoint(           \
162         BOOST_TEST_L(__FILE__),                                 \
163         static_cast<std::size_t>(__LINE__),                     \
164         (::boost::wrap_stringstream().ref() << M).str() )       \
165 /**/
166 
167 //____________________________________________________________________________//
168 
169 #include <boost/test/detail/enable_warnings.hpp>
170 
171 #endif // BOOST_TEST_UNIT_TEST_LOG_HPP_071894GER
172 
173