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)
6 // See http://www.boost.org/libs/test for the library home page.
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 // ***************************************************************************
14 #ifndef BOOST_TEST_UNIT_TEST_LOG_HPP_071894GER
15 #define BOOST_TEST_UNIT_TEST_LOG_HPP_071894GER
18 #include <boost/test/tree/observer.hpp>
20 #include <boost/test/detail/global_typedef.hpp>
21 #include <boost/test/detail/log_level.hpp>
22 #include <boost/test/detail/fwd_decl.hpp>
24 #include <boost/test/utils/wrap_stringstream.hpp>
25 #include <boost/test/utils/trivial_singleton.hpp>
26 #include <boost/test/utils/lazy_ostream.hpp>
31 #include <iosfwd> // for std::ostream&
33 #include <boost/test/detail/suppress_warnings.hpp>
35 //____________________________________________________________________________//
40 // ************************************************************************** //
41 // ************** log manipulators ************** //
42 // ************************************************************************** //
46 struct BOOST_TEST_DECL begin {
47 begin( const_string fn, std::size_t ln )
52 const_string m_file_name;
53 std::size_t m_line_num;
60 // ************************************************************************** //
61 // ************** entry_value_collector ************** //
62 // ************************************************************************** //
66 class BOOST_TEST_DECL entry_value_collector {
69 entry_value_collector() : m_last( true ) {}
70 entry_value_collector( entry_value_collector const& rhs ) : m_last( true ) { rhs.m_last = false; }
71 ~entry_value_collector();
73 // collection interface
74 entry_value_collector const& operator<<( lazy_ostream const& ) const;
75 entry_value_collector const& operator<<( const_string ) const;
82 } // namespace ut_detail
84 // ************************************************************************** //
85 // ************** unit_test_log ************** //
86 // ************************************************************************** //
88 /// @brief Manages the sets of loggers, their streams and log levels
90 /// The Boost.Test framework allows for having several formatters/loggers at the same time, each of which
91 /// having their own log level and output stream.
93 /// This class serves the purpose of
94 /// - exposing an interface to the test framework (as a boost::unit_test::test_observer)
95 /// - exposing an interface to the testing tools
96 /// - managing several loggers
98 /// @note Accesses to the functions exposed by this class are made through the singleton
99 /// @c boost::unit_test::unit_test_log.
101 /// Users/developers willing to implement their own formatter need to:
102 /// - implement a boost::unit_test::unit_test_log_formatter that will output the desired format
103 /// - register the formatter during a eg. global fixture using the method @c set_formatter (though the framework singleton).
105 /// @warning this observer has a higher priority than the @ref boost::unit_test::results_collector_t. This means
106 /// that the various @ref boost::unit_test::test_results associated to each test unit may not be available at the time
107 /// the @c test_unit_start, @c test_unit_finish ... are called.
110 /// - boost::unit_test::test_observer
111 /// - boost::unit_test::unit_test_log_formatter
112 class BOOST_TEST_DECL unit_test_log_t : public test_observer, public singleton<unit_test_log_t> {
114 // test_observer interface implementation
115 virtual void test_start( counter_t test_cases_amount );
116 virtual void test_finish();
117 virtual void test_aborted();
119 virtual void test_unit_start( test_unit const& );
120 virtual void test_unit_finish( test_unit const&, unsigned long elapsed );
121 virtual void test_unit_skipped( test_unit const&, const_string );
122 virtual void test_unit_aborted( test_unit const& );
124 virtual void exception_caught( execution_exception const& ex );
126 virtual int priority() { return 1; }
128 // log configuration methods
129 //! Sets the stream for all loggers
131 //! This will override the log sink/stream of all loggers, whether enabled or not.
132 void set_stream( std::ostream& );
134 //! Sets the stream for specific logger
136 //! @note Has no effect if the specified format is not found
137 //! @par Since Boost 1.62
138 void set_stream( output_format, std::ostream& );
140 //! Sets the threshold level for all loggers/formatters.
142 //! This will override the log level of all loggers, whether enabled or not.
143 void set_threshold_level( log_level );
145 //! Sets the threshold/log level of a specific format
147 //! @note Has no effect if the specified format is not found
148 //! @par Since Boost 1.62
149 void set_threshold_level( output_format, log_level );
151 //! Add a format to the set of loggers
153 //! Adding a logger means that the specified logger is enabled. The log level is managed by the formatter itself
154 //! and specifies what events are forwarded to the underlying formatter.
155 //! @par Since Boost 1.62
156 void add_format( output_format );
158 //! Sets the format of the logger
160 //! This will become the only active format of the logs.
161 void set_format( output_format );
163 //! Returns the logger instance for a specific format.
165 //! @returns the logger/formatter instance, or @c (unit_test_log_formatter*)0 if the format is not found.
166 //! @par Since Boost 1.62
167 unit_test_log_formatter* get_formatter( output_format );
169 //! Sets the logger instance
171 //! The specified logger becomes the unique active one. The custom log formatter has the
172 //! format @c OF_CUSTOM_LOGGER. If such a format exists already, its formatter gets replaced by the one
173 //! given in argument.
175 //! The log level and output stream of the new formatter are taken from the currently active logger. In case
176 //! several loggers are active, the order of priority is CUSTOM, HRF, XML, and JUNIT.
177 //! If (unit_test_log_formatter*)0 is given as argument, the custom logger (if any) is removed.
179 //! @note The ownership of the pointer is transfered to the Boost.Test framework. This call is equivalent to
180 //! - a call to @c add_formatter
181 //! - a call to @c set_format(OF_CUSTOM_LOGGER)
182 //! - a configuration of the newly added logger with a previously configured stream and log level.
183 void set_formatter( unit_test_log_formatter* );
185 //! Adds a custom log formatter to the set of formatters
187 //! The specified logger is added with the format @c OF_CUSTOM_LOGGER, such that it can
188 //! be futher selected or its stream/log level can be specified.
189 //! If there is already a custom logger (with @c OF_CUSTOM_LOGGER), then
190 //! the existing one gets replaced by the one given in argument.
191 //! The provided logger is added with an enabled state.
192 //! If (unit_test_log_formatter*)0 is given as argument, the custom logger (if any) is removed and
193 //! no other action is performed.
195 //! @note The ownership of the pointer is transfered to the Boost.Test framework.
196 //! @par Since Boost 1.62
197 void add_formatter( unit_test_log_formatter* the_formatter );
199 // test progress logging
200 void set_checkpoint( const_string file, std::size_t line_num, const_string msg = const_string() );
203 unit_test_log_t& operator<<( log::begin const& ); // begin entry
204 unit_test_log_t& operator<<( log::end const& ); // end entry
205 unit_test_log_t& operator<<( log_level ); // set entry level
206 unit_test_log_t& operator<<( const_string ); // log entry value
207 unit_test_log_t& operator<<( lazy_ostream const& ); // log entry value
209 ut_detail::entry_value_collector operator()( log_level ); // initiate entry collection
212 // Implementation helpers
213 bool log_entry_start(output_format log_format);
214 void log_entry_context( log_level l );
215 void clear_entry_context();
217 BOOST_TEST_SINGLETON_CONS( unit_test_log_t )
218 }; // unit_test_log_t
220 BOOST_TEST_SINGLETON_INST( unit_test_log )
223 #define BOOST_TEST_LOG_ENTRY( ll ) \
224 (::boost::unit_test::unit_test_log \
225 << ::boost::unit_test::log::begin( BOOST_TEST_L(__FILE__), __LINE__ ))(ll) \
228 } // namespace unit_test
231 // ************************************************************************** //
232 // ************** Unit test log interface helpers ************** //
233 // ************************************************************************** //
235 // messages sent by the framework
236 #define BOOST_TEST_FRAMEWORK_MESSAGE( M ) \
237 (::boost::unit_test::unit_test_log \
238 << ::boost::unit_test::log::begin( \
239 "boost.test framework", \
241 ( ::boost::unit_test::log_messages ) \
242 << BOOST_TEST_LAZY_MSG( M ) \
246 #define BOOST_TEST_MESSAGE( M ) \
247 BOOST_TEST_LOG_ENTRY( ::boost::unit_test::log_messages ) \
248 << BOOST_TEST_LAZY_MSG( M ) \
251 //____________________________________________________________________________//
253 #define BOOST_TEST_PASSPOINT() \
254 ::boost::unit_test::unit_test_log.set_checkpoint( \
255 BOOST_TEST_L(__FILE__), \
256 static_cast<std::size_t>(__LINE__) ) \
259 //____________________________________________________________________________//
261 #define BOOST_TEST_CHECKPOINT( M ) \
262 ::boost::unit_test::unit_test_log.set_checkpoint( \
263 BOOST_TEST_L(__FILE__), \
264 static_cast<std::size_t>(__LINE__), \
265 (::boost::wrap_stringstream().ref() << M).str() ) \
268 //____________________________________________________________________________//
270 #include <boost/test/detail/enable_warnings.hpp>
272 #endif // BOOST_TEST_UNIT_TEST_LOG_HPP_071894GER