2 * Boost Software License - Version 1.0 - August 17th, 2003
4 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
7 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
8 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
9 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
10 * DEALINGS IN THE SOFTWARE.
12 #include "colour_log_formatter.h"
17 #include <boost/test/unit_test_suite_impl.hpp>
18 #include <boost/test/framework.hpp>
19 #include <boost/test/utils/basic_cstring/io.hpp>
20 #include <boost/test/utils/lazy_ostream.hpp>
21 #include <boost/version.hpp>
23 // ************************************************************************** //
24 // ************** colour_log_formatter ************** //
25 // ************************************************************************** //
27 using namespace boost::unit_test;
33 const char *GREEN_BEGIN = "\033[0;32m";
34 const char *RED_BEGIN = "\033[0;31m";
35 const char *CYAN_BEGIN = "\033[0;36m";
36 const char *BOLD_YELLOW_BEGIN = "\033[1;33m";
37 const char *COLOR_END = "\033[m";
40 test_phase_identifier()
42 return framework::is_initialized()
43 ? const_string(framework::current_test_case().p_name.get())
44 : BOOST_TEST_L("Test setup");
48 get_basename(const const_string &file_name)
50 return basename(file_name.begin());
54 get_basename(const std::string &file_name)
56 return basename(file_name.c_str());
61 //____________________________________________________________________________//
64 colour_log_formatter::log_start(
66 counter_t test_cases_amount)
68 if (test_cases_amount > 0)
69 output << "Running " << test_cases_amount << " test "
70 << (test_cases_amount > 1 ? "cases" : "case") << "..." << std::endl;
73 //____________________________________________________________________________//
76 colour_log_formatter::log_finish(std::ostream &ostr)
81 //____________________________________________________________________________//
84 colour_log_formatter::log_build_info(std::ostream &output)
86 output << "Platform: " << BOOST_PLATFORM << std::endl
87 << "Compiler: " << BOOST_COMPILER << std::endl
88 << "STL : " << BOOST_STDLIB << std::endl
89 << "Boost : " << BOOST_VERSION / 100000 << "."
90 << BOOST_VERSION / 100 % 1000 << "."
91 << BOOST_VERSION % 100 << std::endl;
94 //____________________________________________________________________________//
97 colour_log_formatter::test_unit_start(
101 if (tu.p_type_name->find(const_string("suite")) == 0)
102 output << "Starting test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
105 output << "Running test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
109 //____________________________________________________________________________//
112 colour_log_formatter::test_unit_finish(
113 std::ostream &output,
115 unsigned long elapsed)
117 if (tu.p_type_name->find(const_string("suite")) == 0) {
118 output << "Finished test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
123 std::string color = GREEN_BEGIN;
124 std::string status = "OK";
126 if (m_isTestCaseFailed) {
131 output << "\t" << "[ " << color << status << COLOR_END << " ]";
133 output << ", " << CYAN_BEGIN << "time: ";
136 if (elapsed % 1000 == 0)
137 output << elapsed / 1000 << "ms";
139 output << elapsed << "mks";
144 output << COLOR_END << std::endl;
145 m_isTestCaseFailed = false;
148 //____________________________________________________________________________//
151 colour_log_formatter::test_unit_skipped(
152 std::ostream &output,
155 output << "Test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
156 "is skipped" << std::endl;
159 //____________________________________________________________________________//
162 colour_log_formatter::log_exception(
163 std::ostream &output,
164 log_checkpoint_data const &checkpoint_data,
165 boost::execution_exception const &ex)
167 boost::execution_exception::location const &loc = ex.where();
168 output << '\t' << BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
169 << '(' << loc.m_line_num << "), ";
171 output << "fatal error in \""
172 << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
175 output << COLOR_END << ex.what();
177 if (!checkpoint_data.m_file_name.is_empty()) {
179 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
180 << '(' << checkpoint_data.m_line_num << ")";
182 if (!checkpoint_data.m_message.empty())
183 output << ": " << checkpoint_data.m_message;
187 m_isTestCaseFailed = true;
190 //____________________________________________________________________________//
193 colour_log_formatter::log_entry_start(
194 std::ostream &output,
195 log_entry_data const &entry_data,
199 case BOOST_UTL_ET_INFO:
200 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
205 case BOOST_UTL_ET_MESSAGE:
208 case BOOST_UTL_ET_WARNING:
209 output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
210 entry_data.m_line_num << "), ";
211 output << "warning in \"" << test_phase_identifier() << "\": ";
214 case BOOST_UTL_ET_ERROR:
215 output << '\t' << BOLD_YELLOW_BEGIN << get_basename(entry_data.m_file_name)
216 << '(' << entry_data.m_line_num << "), ";
217 output << "error in \"" << test_phase_identifier() << "\": ";
218 m_isTestCaseFailed = true;
221 case BOOST_UTL_ET_FATAL_ERROR:
222 output << '\t' << BOLD_YELLOW_BEGIN << get_basename(entry_data.m_file_name)
223 << '(' << entry_data.m_line_num << "), ";
224 output << " fatal error in \"" << test_phase_identifier() << "\": ";
225 m_isTestCaseFailed = true;
232 //____________________________________________________________________________//
235 colour_log_formatter::log_entry_value(
236 std::ostream &output,
242 //____________________________________________________________________________//
245 colour_log_formatter::log_entry_value(
246 std::ostream &output,
247 lazy_ostream const &value)
252 //____________________________________________________________________________//
255 colour_log_formatter::log_entry_finish(
256 std::ostream &output)
261 //____________________________________________________________________________//
263 //____________________________________________________________________________//
267 //____________________________________________________________________________//