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_start(
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_exception_finish(std::ostream &os)
233 //____________________________________________________________________________//
236 colour_log_formatter::log_entry_start(
237 std::ostream &output,
238 log_entry_data const &entry_data,
242 case BOOST_UTL_ET_INFO:
243 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
248 case BOOST_UTL_ET_MESSAGE:
251 case BOOST_UTL_ET_WARNING:
252 output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
253 entry_data.m_line_num << "), ";
254 output << "warning in \"" << test_phase_identifier() << "\": ";
257 case BOOST_UTL_ET_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 << "error in \"" << test_phase_identifier() << "\": ";
262 m_isTestCaseFailed = true;
265 case BOOST_UTL_ET_FATAL_ERROR:
266 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
267 entry_data.m_file_name)
268 << '(' << entry_data.m_line_num << "), ";
269 output << " fatal error in \"" << test_phase_identifier() << "\": ";
270 m_isTestCaseFailed = true;
274 output << CKM::Colors::Text::COLOR_END;
277 //____________________________________________________________________________//
280 colour_log_formatter::log_entry_value(
281 std::ostream &output,
287 //____________________________________________________________________________//
290 colour_log_formatter::log_entry_value(
291 std::ostream &output,
292 lazy_ostream const &value)
297 //____________________________________________________________________________//
300 colour_log_formatter::log_entry_finish(
301 std::ostream &output)
306 //____________________________________________________________________________//
309 colour_log_formatter::entry_context_start(std::ostream& os, boost::unit_test::log_level l)
315 colour_log_formatter::log_entry_context(std::ostream& os, boost::unit_test::const_string value)
322 colour_log_formatter::log_entry_context(std::ostream&,
323 boost::unit_test::log_level,
324 boost::unit_test::const_string)
329 colour_log_formatter::entry_context_finish(std::ostream& os)
335 colour_log_formatter::entry_context_finish(std::ostream&,
336 boost::unit_test::log_level)
339 //____________________________________________________________________________//
341 //____________________________________________________________________________//
345 //____________________________________________________________________________//