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, bool log_build_info)
117 output << "Platform: " << BOOST_PLATFORM << '\n'
118 << "Compiler: " << BOOST_COMPILER << '\n'
119 << "STL : " << BOOST_STDLIB << '\n';
120 output << "Boost : " << BOOST_VERSION / 100000 << '.'
121 << BOOST_VERSION / 100 % 1000 << '.'
122 << BOOST_VERSION % 100 << std::endl;
125 //____________________________________________________________________________//
128 colour_log_formatter::test_unit_start(
129 std::ostream &output,
132 if (test_unit_type_name_contains(tu, "suite")) {
133 output << "Starting test ";
135 output << "Running test ";
137 output << test_unit_type_name(tu) << " \"" << test_unit_name(tu)
138 << "\"" << std::endl;
142 //____________________________________________________________________________//
145 colour_log_formatter::test_unit_finish(
146 std::ostream &output,
148 unsigned long elapsed)
150 if (test_unit_type_name_contains(tu, "suite")) {
151 output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
156 std::string color = CKM::Colors::Text::GREEN_BEGIN;
157 std::string status = "OK";
159 if (m_isTestCaseFailed) {
160 color = CKM::Colors::Text::RED_BEGIN;
164 output << "\t" << "[ " << color << status << CKM::Colors::Text::COLOR_END <<
168 output << ", " << CKM::Colors::Text::CYAN_BEGIN << "time: ";
171 if (elapsed % 1000 == 0)
172 output << elapsed / 1000 << "ms";
174 output << elapsed << "mks";
179 output << CKM::Colors::Text::COLOR_END << std::endl;
180 m_isTestCaseFailed = false;
183 //____________________________________________________________________________//
186 colour_log_formatter::test_unit_skipped(
187 std::ostream &output,
190 output << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
191 "is skipped" << std::endl;
194 //____________________________________________________________________________//
197 colour_log_formatter::log_exception(
198 std::ostream &output,
199 log_checkpoint_data const &checkpoint_data,
200 boost::execution_exception const &ex)
202 boost::execution_exception::location const &loc = ex.where();
203 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
205 << '(' << loc.m_line_num << "), ";
207 output << "fatal error in \""
208 << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
211 output << CKM::Colors::Text::COLOR_END << ex.what();
213 if (!checkpoint_data.m_file_name.is_empty()) {
215 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
216 << '(' << checkpoint_data.m_line_num << ")";
218 if (!checkpoint_data.m_message.empty())
219 output << ": " << checkpoint_data.m_message;
223 m_isTestCaseFailed = true;
226 //____________________________________________________________________________//
229 colour_log_formatter::log_entry_start(
230 std::ostream &output,
231 log_entry_data const &entry_data,
235 case BOOST_UTL_ET_INFO:
236 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
241 case BOOST_UTL_ET_MESSAGE:
244 case BOOST_UTL_ET_WARNING:
245 output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
246 entry_data.m_line_num << "), ";
247 output << "warning in \"" << test_phase_identifier() << "\": ";
250 case BOOST_UTL_ET_ERROR:
251 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
252 entry_data.m_file_name)
253 << '(' << entry_data.m_line_num << "), ";
254 output << "error in \"" << test_phase_identifier() << "\": ";
255 m_isTestCaseFailed = true;
258 case BOOST_UTL_ET_FATAL_ERROR:
259 output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
260 entry_data.m_file_name)
261 << '(' << entry_data.m_line_num << "), ";
262 output << " fatal error in \"" << test_phase_identifier() << "\": ";
263 m_isTestCaseFailed = true;
267 output << CKM::Colors::Text::COLOR_END;
270 //____________________________________________________________________________//
273 colour_log_formatter::log_entry_value(
274 std::ostream &output,
280 //____________________________________________________________________________//
283 colour_log_formatter::log_entry_value(
284 std::ostream &output,
285 lazy_ostream const &value)
290 //____________________________________________________________________________//
293 colour_log_formatter::log_entry_finish(
294 std::ostream &output)
299 //____________________________________________________________________________//
301 #if BOOST_VERSION >= 106501
302 void colour_log_formatter::log_exception_start(std::ostream& os, boost::unit_test::log_checkpoint_data const& lcd, boost::execution_exception const& ex)
304 log_exception(os, lcd, ex);
307 void colour_log_formatter::log_exception_finish(std::ostream& os)
312 void colour_log_formatter::entry_context_start(std::ostream& os, boost::unit_test::log_level l)
318 void colour_log_formatter::log_entry_context(std::ostream& os, boost::unit_test::log_level l, boost::unit_test::const_string value)
325 void colour_log_formatter::entry_context_finish(std::ostream& os, boost::unit_test::log_level l)
332 //____________________________________________________________________________//
336 //____________________________________________________________________________//