Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:09:21

0001 //  (C) Copyright Gennadiy Rozental 2001.
0002 //  Distributed under the Boost Software License, Version 1.0.
0003 //  (See accompanying file LICENSE_1_0.txt or copy at
0004 //  http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 //  See http://www.boost.org/libs/test for the library home page.
0007 //
0008 /// @file
0009 /// @brief Defines unit test log formatter interface
0010 ///
0011 /// You can define a class with implements this interface and use an instance of it
0012 /// as a Unit Test Framework log formatter
0013 // ***************************************************************************
0014 
0015 #ifndef BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER
0016 #define BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER
0017 
0018 // Boost.Test
0019 #include <boost/test/detail/global_typedef.hpp>
0020 #include <boost/test/detail/log_level.hpp>
0021 #include <boost/test/detail/fwd_decl.hpp>
0022 
0023 // STL
0024 #include <iosfwd>
0025 #include <string> // for std::string
0026 #include <iostream>
0027 
0028 #include <boost/test/detail/suppress_warnings.hpp>
0029 
0030 //____________________________________________________________________________//
0031 
0032 namespace boost {
0033 namespace unit_test {
0034 
0035 // ************************************************************************** //
0036 /// Collection of log entry attributes
0037 // ************************************************************************** //
0038 
0039 struct BOOST_TEST_DECL log_entry_data {
0040     log_entry_data()
0041     {
0042         m_file_name.reserve( 200 );
0043     }
0044 
0045     std::string     m_file_name; ///< log entry file name
0046     std::size_t     m_line_num;  ///< log entry line number
0047     log_level       m_level;     ///< log entry level
0048 
0049     void clear()
0050     {
0051         m_file_name.erase();
0052         m_line_num      = 0;
0053         m_level     = log_nothing;
0054     }
0055 };
0056 
0057 // ************************************************************************** //
0058 /// Collection of log checkpoint attributes
0059 // ************************************************************************** //
0060 
0061 struct BOOST_TEST_DECL log_checkpoint_data
0062 {
0063     const_string    m_file_name; ///< log checkpoint file name
0064     std::size_t     m_line_num;  ///< log checkpoint file name
0065     std::string     m_message;   ///< log checkpoint message
0066 
0067     void clear()
0068     {
0069         m_file_name.clear();
0070         m_line_num  = 0;
0071         m_message   = std::string();
0072     }
0073 };
0074 
0075 // ************************************************************************** //
0076 /// @brief Abstract Unit Test Framework log formatter interface
0077 ///
0078 /// During the test module execution Unit Test Framework can report messages about success
0079 /// or failure of assertions, which test suites are being run and more (specifically which
0080 /// messages are reported depends on log level threshold selected by the user).
0081 ///
0082 /// All these messages constitute Unit Test Framework log. There are many ways (formats) to present
0083 /// these messages to the user.
0084 ///
0085 /// Boost.Test comes with three formats:
0086 /// - Compiler-like log format: intended for human consumption/diagnostic
0087 /// - XML based log format:  intended for processing by automated regression test systems.
0088 /// - JUNIT based log format:  intended for processing by automated regression test systems.
0089 ///
0090 /// If you want to produce some other format you need to implement class with specific interface and use
0091 /// method @c unit_test_log_t::set_formatter during a test module initialization to set an active formatter.
0092 /// The class unit_test_log_formatter defines this interface.
0093 ///
0094 /// This interface requires you to format all possible messages being produced in the log.
0095 /// These includes error messages about failed assertions, messages about caught exceptions and
0096 /// information messages about test units being started/ended. All the methods in this interface takes
0097 /// a reference to standard stream as a first argument. This is where final messages needs to be directed
0098 /// to. Also you are given all the information necessary to produce a message.
0099 ///
0100 /// @par Since Boost 1.62:
0101 /// - Each formatter may indicate the default output stream. This is convenient for instance for streams intended
0102 ///   for automated processing that indicate a file. See @c get_default_stream_description for more details.
0103 /// - Each formatter may manage its own log level through the getter/setter @c get_log_level and @c set_log_level .
0104 ///
0105 /// @see
0106 /// - boost::unit_test::test_observer for an indication of the calls of the test observer interface
0107 class BOOST_TEST_DECL unit_test_log_formatter {
0108 public:
0109     /// Types of log entries (messages written into a log)
0110     enum log_entry_types { BOOST_UTL_ET_INFO,       ///< Information message from the framework
0111                            BOOST_UTL_ET_MESSAGE,    ///< Information message from the user
0112                            BOOST_UTL_ET_WARNING,    ///< Warning (non error) condition notification message
0113                            BOOST_UTL_ET_ERROR,      ///< Non fatal error notification message
0114                            BOOST_UTL_ET_FATAL_ERROR ///< Fatal error notification message
0115     };
0116 
0117     //! Constructor
0118     unit_test_log_formatter()
0119         : m_log_level(log_all_errors)
0120     {}
0121 
0122     // Destructor
0123     virtual             ~unit_test_log_formatter() {}
0124 
0125     // @name Test start/finish
0126 
0127     /// Invoked at the beginning of test module execution
0128     ///
0129     /// @param[in] os   output stream to write a messages to
0130     /// @param[in] test_cases_amount total test case amount to be run
0131     /// @see log_finish
0132     virtual void        log_start( std::ostream& os, counter_t test_cases_amount ) = 0;
0133 
0134     /// Invoked at the end of test module execution
0135     ///
0136     /// @param[in] os   output stream to write a messages into
0137     /// @see log_start
0138     virtual void        log_finish( std::ostream& os ) = 0;
0139 
0140     /// Invoked when Unit Test Framework build information is requested
0141     ///
0142     /// @param[in] os               output stream to write a messages into
0143     /// @param[in] log_build_info   indicates if build info should be logged or not
0144     virtual void        log_build_info( std::ostream& os, bool log_build_info = true ) = 0;
0145     // @}
0146 
0147     // @name Test unit start/finish
0148 
0149     /// Invoked when test unit starts (either test suite or test case)
0150     ///
0151     /// @param[in] os   output stream to write a messages into
0152     /// @param[in] tu   test unit being started
0153     /// @see test_unit_finish
0154     virtual void        test_unit_start( std::ostream& os, test_unit const& tu ) = 0;
0155 
0156     /// Invoked when test unit finishes
0157     ///
0158     /// @param[in] os   output stream to write a messages into
0159     /// @param[in] tu   test unit being finished
0160     /// @param[in] elapsed time in microseconds spend executing this test unit
0161     /// @see test_unit_start
0162     virtual void        test_unit_finish( std::ostream& os, test_unit const& tu, unsigned long elapsed ) = 0;
0163 
0164     /// Invoked if test unit skipped for any reason
0165     ///
0166     /// @param[in] os   output stream to write a messages into
0167     /// @param[in] tu   skipped test unit
0168     /// @param[in] reason explanation why was it skipped
0169     virtual void        test_unit_skipped( std::ostream& os, test_unit const& tu, const_string /* reason */)
0170     {
0171         test_unit_skipped( os, tu );
0172     }
0173 
0174     /// Deprecated version of this interface
0175     /// @deprecated
0176     virtual void        test_unit_skipped( std::ostream& /* os */, test_unit const& /* tu */) {}
0177 
0178     /// Invoked when a test unit is aborted
0179     virtual void        test_unit_aborted( std::ostream& /* os */, test_unit const& /* tu */) {}
0180 
0181     /// Invoked when a test unit times-out
0182     virtual void        test_unit_timed_out( std::ostream& /* os */, test_unit const& /* tu */) {}
0183 
0184 
0185     // @}
0186 
0187     // @name Uncaught exception report
0188 
0189     /// Invoked when Unit Test Framework detects uncaught exception
0190     ///
0191     /// The framwork calls this function when an uncaught exception it detected.
0192     /// This call is followed by context information:
0193     /// - one call to @c entry_context_start,
0194     /// - as many calls to @c log_entry_context as there are context entries
0195     /// - one call to @c entry_context_finish
0196     ///
0197     /// The logging of the exception information is finilized by a call to @c log_exception_finish.
0198     ///
0199     /// @param[in] os   output stream to write a messages into
0200     /// @param[in] lcd  information about the last checkpoint before the exception was triggered
0201     /// @param[in] ex   information about the caught exception
0202     /// @see log_exception_finish
0203     virtual void        log_exception_start( std::ostream& os, log_checkpoint_data const& lcd, execution_exception const& ex ) = 0;
0204 
0205     /// Invoked when Unit Test Framework detects uncaught exception
0206     ///
0207     /// Call to this function finishes uncaught exception report.
0208     /// @param[in] os   output stream to write a messages into
0209     /// @see log_exception_start
0210     virtual void        log_exception_finish( std::ostream& os ) = 0;
0211     // @}
0212 
0213     // @name Regular log entry
0214 
0215     /// Invoked by Unit Test Framework to start new log entry
0216 
0217     /// Call to this function starts new log entry. It is followed by series of log_entry_value calls and finally call to log_entry_finish.
0218     /// A log entry may consist of one or more values being reported. Some of these values will be plain strings, while others can be complicated
0219     /// expressions in a form of "lazy" expression template lazy_ostream.
0220     /// @param[in] os   output stream to write a messages into
0221     /// @param[in] led  log entry attributes
0222     /// @param[in] let  log entry type log_entry_finish
0223     /// @see log_entry_value, log_entry_finish
0224     ///
0225     /// @note call to this function may happen before any call to test_unit_start or all calls to test_unit_finish as the
0226     /// framework might log errors raised during global initialization/shutdown.
0227     virtual void        log_entry_start( std::ostream& os, log_entry_data const& led, log_entry_types let ) = 0;
0228 
0229     /// Invoked by Unit Test Framework to report a log entry content
0230     ///
0231     /// This is one of two overloaded methods to report log entry content. This one is used to report plain string value.
0232     /// @param[in] os   output stream to write a messages into.
0233     /// @param[in] value log entry string value
0234     /// @see log_entry_start, log_entry_finish
0235     virtual void        log_entry_value( std::ostream& os, const_string value ) = 0;
0236 
0237     /// Invoked by Unit Test Framework to report a log entry content
0238 
0239     /// This is one of two overloaded methods to report log entry content. This one is used to report some complicated expression passed as
0240     /// an expression template lazy_ostream. In most cases default implementation provided by the framework should work as is (it just converts
0241     /// the lazy expression into a string.
0242     /// @param[in] os   output stream to write a messages into
0243     /// @param[in] value log entry "lazy" value
0244     /// @see log_entry_start, log_entry_finish
0245     virtual void        log_entry_value( std::ostream& os, lazy_ostream const& value ); // there is a default impl
0246 
0247     /// Invoked by Unit Test Framework to finish a log entry report
0248 
0249     /// @param[in] os   output stream to write a messages into
0250     /// @see log_entry_start, log_entry_start
0251     virtual void        log_entry_finish( std::ostream& os ) = 0;
0252     // @}
0253 
0254     // @name Log entry context report
0255 
0256     /// Invoked by Unit Test Framework to start log entry context report
0257     //
0258     /// Unit Test Framework logs for failed assertions and uncaught exceptions context if one was defined by a test module.
0259     /// Context consists of multiple "scopes" identified by description messages assigned by the test module using
0260     /// BOOST_TEST_INFO/BOOST_TEST_CONTEXT statements.
0261     /// @param[in] os   output stream to write a messages into
0262     /// @param[in] l    entry log_level, to be used to fine tune the message
0263     /// @see log_entry_context, entry_context_finish
0264     virtual void        entry_context_start( std::ostream& os, log_level l ) = 0;
0265 
0266     /// Invoked by Unit Test Framework to report log entry context "scope" description
0267     //
0268     /// Each "scope" description is reported by separate call to log_entry_context.
0269     /// @param[in] os   output stream to write a messages into
0270     /// @param[in] l    entry log_level, to be used to fine tune the message
0271     /// @param[in] value  context "scope" description
0272     /// @see log_entry_start, entry_context_finish
0273     virtual void        log_entry_context( std::ostream& os, log_level l, const_string value ) = 0;
0274 
0275     /// Invoked by Unit Test Framework to finish log entry context report
0276     ///
0277     /// @param[in] os   output stream to write a messages into
0278     /// @param[in] l    entry log_level, to be used to fine tune the message
0279     /// @see log_entry_start, entry_context_context
0280     virtual void        entry_context_finish( std::ostream& os, log_level l ) = 0;
0281     // @}
0282 
0283     // @name Log level management
0284 
0285     /// Sets the log level of the logger/formatter
0286     ///
0287     /// Some loggers need to manage the log level by their own. This
0288     /// member function let the implementation decide of that.
0289     /// @par Since Boost 1.62
0290     virtual void        set_log_level(log_level new_log_level);
0291 
0292     /// Returns the log level of the logger/formatter
0293     /// @par Since Boost 1.62
0294     virtual log_level   get_log_level() const;
0295     // @}
0296 
0297 
0298     // @name Stream management
0299 
0300     /// Returns a default stream for this logger.
0301     ///
0302     /// The returned string describes the stream as if it was passed from
0303     /// the command line @c "--log_sink" parameter. With that regards, @b stdout and @b stderr
0304     /// have special meaning indicating the standard output or error stream respectively.
0305     ///
0306     /// @par Since Boost 1.62
0307     virtual std::string  get_default_stream_description() const
0308     {
0309         return "stdout";
0310     }
0311 
0312     // @}
0313 
0314 
0315 protected:
0316     log_level           m_log_level;
0317 
0318 };
0319 
0320 } // namespace unit_test
0321 } // namespace boost
0322 
0323 //____________________________________________________________________________//
0324 
0325 #include <boost/test/detail/enable_warnings.hpp>
0326 
0327 #endif // BOOST_TEST_UNIT_TEST_LOG_FORMATTER_HPP_071894GER