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::log_build_info(std::ostream &output, bool log_build_info)
130 output << "Platform: " << BOOST_PLATFORM << '\n'
131 << "Compiler: " << BOOST_COMPILER << '\n'
132 << "STL : " << BOOST_STDLIB << '\n';
133 output << "Boost : " << BOOST_VERSION / 100000 << '.'
134 << BOOST_VERSION / 100 % 1000 << '.'
135 << BOOST_VERSION % 100 << std::endl;
138 //____________________________________________________________________________//
141 colour_log_formatter::test_unit_start(
142 std::ostream &output,
145 if (test_unit_type_name_contains(tu, "suite")) {
146 output << "Starting test ";
148 output << "Running test ";
150 output << test_unit_type_name(tu) << " \"" << test_unit_name(tu)
151 << "\"" << std::endl;
155 //____________________________________________________________________________//
158 colour_log_formatter::test_unit_finish(
159 std::ostream &output,
161 unsigned long elapsed)
163 if (test_unit_type_name_contains(tu, "suite")) {
164 output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
169 std::string color = CKM::Colors::Text::GREEN_BEGIN;
170 std::string status = "OK";
172 if (m_isTestCaseFailed) {
173 color = CKM::Colors::Text::RED_BEGIN;
177 output << "\t" << "[ " << color << status << CKM::Colors::Text::COLOR_END <<
181 output << ", " << CKM::Colors::Text::CYAN_BEGIN << "time: ";
184 if (elapsed % 1000 == 0)
185 output << elapsed / 1000 << "ms";
187 output << elapsed << "mks";
192 output << CKM::Colors::Text::COLOR_END << std::endl;
193 m_isTestCaseFailed = false;
196 //____________________________________________________________________________//
199 colour_log_formatter::test_unit_skipped(
200 std::ostream &output,
203 output << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
204 "is skipped" << std::endl;
207 //____________________________________________________________________________//
210 colour_log_formatter::log_exception(
211 std::ostream &output,
212 log_checkpoint_data const &checkpoint_data,
213 boost::execution_exception const &ex)
215 boost::execution_exception::location const &loc = ex.where();
216 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
218 << '(' << loc.m_line_num << "), ";
220 output << "fatal error in \""
221 << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
224 output << CKM::Colors::Text::COLOR_END << ex.what();
226 if (!checkpoint_data.m_file_name.is_empty()) {
228 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
229 << '(' << checkpoint_data.m_line_num << ")";
231 if (!checkpoint_data.m_message.empty())
232 output << ": " << checkpoint_data.m_message;
236 m_isTestCaseFailed = true;
239 //____________________________________________________________________________//
242 colour_log_formatter::log_entry_start(
243 std::ostream &output,
244 log_entry_data const &entry_data,
248 case BOOST_UTL_ET_INFO:
249 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
254 case BOOST_UTL_ET_MESSAGE:
257 case BOOST_UTL_ET_WARNING:
258 output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
259 entry_data.m_line_num << "), ";
260 output << "warning in \"" << test_phase_identifier() << "\": ";
263 case BOOST_UTL_ET_ERROR:
264 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
265 entry_data.m_file_name)
266 << '(' << entry_data.m_line_num << "), ";
267 output << "error in \"" << test_phase_identifier() << "\": ";
268 m_isTestCaseFailed = true;
271 case BOOST_UTL_ET_FATAL_ERROR:
272 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
273 entry_data.m_file_name)
274 << '(' << entry_data.m_line_num << "), ";
275 output << " fatal error in \"" << test_phase_identifier() << "\": ";
276 m_isTestCaseFailed = true;
280 output << CKM::Colors::Text::COLOR_END;
283 //____________________________________________________________________________//
286 colour_log_formatter::log_entry_value(
287 std::ostream &output,
293 //____________________________________________________________________________//
296 colour_log_formatter::log_entry_value(
297 std::ostream &output,
298 lazy_ostream const &value)
303 //____________________________________________________________________________//
306 colour_log_formatter::log_entry_finish(
307 std::ostream &output)
312 //____________________________________________________________________________//
314 #if BOOST_VERSION >= 106501
315 void colour_log_formatter::log_exception_start(std::ostream& os, boost::unit_test::log_checkpoint_data const& lcd, boost::execution_exception const& ex)
322 void colour_log_formatter::log_exception_finish(std::ostream& os)
327 void colour_log_formatter::entry_context_start(std::ostream& os, boost::unit_test::log_level l)
333 void colour_log_formatter::log_entry_context(std::ostream& os, boost::unit_test::log_level l, boost::unit_test::const_string value)
340 void colour_log_formatter::entry_context_finish(std::ostream& os, boost::unit_test::log_level l)
347 //____________________________________________________________________________//
351 //____________________________________________________________________________//