2 * (C) Copyright Gennadiy Rozental 2005-2008.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
7 * See http://www.boost.org/libs/test for the library home page.
10 * @file colour_log_formatter.cpp
11 * @author Zofia Abramowska (z.abramowska@samsung.com)
16 #include <colour_log_formatter.h>
17 #include <boost/test/impl/execution_monitor.ipp>
18 #if BOOST_VERSION >= 105900
19 #include <boost/test/tree/test_unit.hpp>
21 #include <boost/test/unit_test_suite_impl.hpp>
23 #include <boost/test/framework.hpp>
24 #include <boost/test/utils/basic_cstring/io.hpp>
25 #include <boost/test/utils/lazy_ostream.hpp>
28 #include <boost/version.hpp>
34 #include <dpl/colors.h>
36 // ************************************************************************** //
37 // ************** colour_log_formatter ************** //
38 // ************************************************************************** //
40 using namespace boost::unit_test;
46 test_unit_type_name(const test_unit &tu)
48 #if BOOST_VERSION >= 105900
49 return const_string(tu.p_type_name);
51 return tu.p_type_name.get();
56 test_unit_name(const test_unit &tu)
58 #if BOOST_VERSION >= 105900
59 return const_string(tu.p_name);
61 return tu.p_name.get();
66 test_phase_identifier()
68 return test_unit_name(framework::current_test_case());
72 get_basename(const const_string &file_name)
74 return basename(file_name.begin());
78 get_basename(const std::string &file_name)
80 return basename(file_name.c_str());
84 test_unit_type_name_contains(const test_unit &tu, const std::string &substr)
86 return test_unit_type_name(tu).find(const_string(substr)) == 0;
91 //____________________________________________________________________________//
94 colour_log_formatter::log_start(
96 counter_t test_cases_amount)
98 if (test_cases_amount > 0)
99 output << "Running " << test_cases_amount << " test "
100 << (test_cases_amount > 1 ? "cases" : "case") << "...\n";
103 //____________________________________________________________________________//
106 colour_log_formatter::log_finish(std::ostream &ostr)
111 //____________________________________________________________________________//
114 colour_log_formatter::log_build_info(std::ostream &output)
116 output << "Platform: " << BOOST_PLATFORM << '\n'
117 << "Compiler: " << BOOST_COMPILER << '\n'
118 << "STL : " << BOOST_STDLIB << '\n'
119 << "Boost : " << BOOST_VERSION / 100000 << "."
120 << BOOST_VERSION / 100 % 1000 << "."
121 << BOOST_VERSION % 100 << std::endl;
124 //____________________________________________________________________________//
127 colour_log_formatter::test_unit_start(
128 std::ostream &output,
131 if (test_unit_type_name_contains(tu, "suite")) {
132 output << "Starting test ";
134 output << "Running test ";
136 output << test_unit_type_name(tu) << " \"" << test_unit_name(tu)
137 << "\"" << std::endl;
141 //____________________________________________________________________________//
144 colour_log_formatter::test_unit_finish(
145 std::ostream &output,
147 unsigned long elapsed)
149 if (test_unit_type_name_contains(tu, "suite")) {
150 output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
155 std::string color = CKM::Colors::Text::GREEN_BEGIN;
156 std::string status = "OK";
158 if (m_isTestCaseFailed) {
159 color = CKM::Colors::Text::RED_BEGIN;
163 output << "\t" << "[ " << color << status << CKM::Colors::Text::COLOR_END <<
167 output << ", " << CKM::Colors::Text::CYAN_BEGIN << "time: ";
170 if (elapsed % 1000 == 0)
171 output << elapsed / 1000 << "ms";
173 output << elapsed << "mks";
178 output << CKM::Colors::Text::COLOR_END << std::endl;
179 m_isTestCaseFailed = false;
182 //____________________________________________________________________________//
185 colour_log_formatter::test_unit_skipped(
186 std::ostream &output,
189 output << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
190 "is skipped" << std::endl;
193 //____________________________________________________________________________//
196 colour_log_formatter::log_exception(
197 std::ostream &output,
198 log_checkpoint_data const &checkpoint_data,
199 boost::execution_exception const &ex)
201 boost::execution_exception::location const &loc = ex.where();
202 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
204 << '(' << loc.m_line_num << "), ";
206 output << "fatal error in \""
207 << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
210 output << CKM::Colors::Text::COLOR_END << ex.what();
212 if (!checkpoint_data.m_file_name.is_empty()) {
214 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
215 << '(' << checkpoint_data.m_line_num << ")";
217 if (!checkpoint_data.m_message.empty())
218 output << ": " << checkpoint_data.m_message;
222 m_isTestCaseFailed = true;
225 //____________________________________________________________________________//
228 colour_log_formatter::log_entry_start(
229 std::ostream &output,
230 log_entry_data const &entry_data,
234 case BOOST_UTL_ET_INFO:
235 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
240 case BOOST_UTL_ET_MESSAGE:
243 case BOOST_UTL_ET_WARNING:
244 output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
245 entry_data.m_line_num << "), ";
246 output << "warning in \"" << test_phase_identifier() << "\": ";
249 case BOOST_UTL_ET_ERROR:
250 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
251 entry_data.m_file_name)
252 << '(' << entry_data.m_line_num << "), ";
253 output << "error in \"" << test_phase_identifier() << "\": ";
254 m_isTestCaseFailed = true;
257 case BOOST_UTL_ET_FATAL_ERROR:
258 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
259 entry_data.m_file_name)
260 << '(' << entry_data.m_line_num << "), ";
261 output << " fatal error in \"" << test_phase_identifier() << "\": ";
262 m_isTestCaseFailed = true;
266 output << CKM::Colors::Text::COLOR_END;
269 //____________________________________________________________________________//
272 colour_log_formatter::log_entry_value(
273 std::ostream &output,
279 //____________________________________________________________________________//
282 colour_log_formatter::log_entry_value(
283 std::ostream &output,
284 lazy_ostream const &value)
289 //____________________________________________________________________________//
292 colour_log_formatter::log_entry_finish(
293 std::ostream &output)
298 //____________________________________________________________________________//
300 #if BOOST_VERSION >= 106501
301 void colour_log_formatter::log_exception_start(std::ostream& os, boost::unit_test::log_checkpoint_data const& lcd, boost::execution_exception const& ex)
308 void colour_log_formatter::log_exception_finish(std::ostream& os)
313 void colour_log_formatter::entry_context_start(std::ostream& os, boost::unit_test::log_level l)
319 void colour_log_formatter::log_entry_context(std::ostream& os, boost::unit_test::log_level l, boost::unit_test::const_string value)
326 void colour_log_formatter::entry_context_finish(std::ostream& os, boost::unit_test::log_level l)
333 //____________________________________________________________________________//
337 //____________________________________________________________________________//