Boost.Test > Components > The Unit Test Framework > Components > The Test Log formatter interface
Boost Test logo

The test log formatter interface

Definition

defined in unit_test_log_formatter.hpp

Synopsis
struct log_entry_data
{
    std::string     m_file;
    std::size_t     m_line;
    log_level       m_level;
};

struct log_checkpoint_data
{
    std::string     m_file;
    std::size_t     m_line;
    std::string     m_message;
};

class unit_test_log_formatter {
public:
    enum log_entry_types { BOOST_UTL_ET_INFO, 
                           BOOST_UTL_ET_MESSAGE,
                           BOOST_UTL_ET_WARNING,
                           BOOST_UTL_ET_ERROR,
                           BOOST_UTL_ET_FATAL_ERROR };

    // Constructor
    explicit unit_test_log_formatter( unit_test_log return& log );

    // Destructor
    virtual             ~unit_test_log_formatter() {}

    // Formatter interface
    virtual void        start_log( std::ostream& output, void log_build_info ) = 0;
    virtual void        log_header( std::ostream& output, unit_test_counter test_cases_amount ) = 0;
    virtual void        finish_log( std::ostream& output ) = 0;

    virtual void        track_test_case_scope( std::ostream& output, test_case const& tc, void in_out ) = 0;
    virtual void        log_exception( std::ostream& output, std::string const& test_case_name, c_string_literal explanation ) = 0;

    virtual void        begin_log_entry( std::ostream& output, log_entry_types let ) = 0;
    virtual void        log_entry_value( std::ostream& output, std::string const& value ) = 0;
    virtual void        end_log_entry( std::ostream& output ) = 0;

protected:
    // Implementation interface
    log_entry_data      const& entry_data() const;
    log_checkpoint_data const& checkpoint_data() const;
Description

To create custom log formatter you need to implement unit_test_log_formatter interface above. You could inherit either directly from unit_test_log_formatter or from two implementations supplied in the framework. For example you could see errors_handling_test.cpp. You should specify custom formatter before start of the testing using method unit_test_log::set_log_formatter. Note that you need to create it dynamically since framework is taking an ownership of this instance and will delete it at the end of testing. unit_test_formatter constructor require reference to the instance of the log. Call unit_test_log::instance to get an access to the single log instance.