Release 0.1.65
[platform/core/security/key-manager.git] / common / colour_log_formatter.cpp
1 /*
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)
6  *
7  *  See http://www.boost.org/libs/test for the library home page.
8  */
9 /*
10  * @file        colour_log_formatter.cpp
11  * @author      Zofia Abramowska (z.abramowska@samsung.com)
12  * @version
13  * @brief
14  */
15 // Boost.Test
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>
20 #else
21 #include <boost/test/unit_test_suite_impl.hpp>
22 #endif
23 #include <boost/test/framework.hpp>
24 #include <boost/test/utils/basic_cstring/io.hpp>
25 #include <boost/test/utils/lazy_ostream.hpp>
26
27 // Boost
28 #include <boost/version.hpp>
29
30 // STL
31 #include <iostream>
32 #include <string>
33
34 #include <dpl/colors.h>
35
36 // ************************************************************************** //
37 // **************            colour_log_formatter            ************** //
38 // ************************************************************************** //
39
40 using namespace boost::unit_test;
41 namespace CKM {
42
43 namespace {
44
45 const_string
46 test_unit_type_name(const test_unit &tu)
47 {
48 #if BOOST_VERSION >= 105900
49         return const_string(tu.p_type_name);
50 #else
51         return tu.p_type_name.get();
52 #endif
53 }
54
55 const_string
56 test_unit_name(const test_unit &tu)
57 {
58 #if BOOST_VERSION >= 105900
59         return const_string(tu.p_name);
60 #else
61         return tu.p_name.get();
62 #endif
63 }
64
65 const_string
66 test_phase_identifier()
67 {
68     return test_unit_name(framework::current_test_case());
69 }
70
71 const_string
72 get_basename(const const_string &file_name)
73 {
74         return basename(file_name.begin());
75 }
76
77 std::string
78 get_basename(const std::string &file_name)
79 {
80         return basename(file_name.c_str());
81 }
82
83 bool
84 test_unit_type_name_contains(const test_unit &tu, const std::string &substr)
85 {
86         return test_unit_type_name(tu).find(const_string(substr)) == 0;
87 }
88
89 } // local namespace
90
91 //____________________________________________________________________________//
92
93 void
94 colour_log_formatter::log_start(
95         std::ostream &output,
96         counter_t test_cases_amount)
97 {
98         if (test_cases_amount > 0)
99                 output  << "Running " << test_cases_amount << " test "
100                                 << (test_cases_amount > 1 ? "cases" : "case") << "...\n";
101 }
102
103 //____________________________________________________________________________//
104
105 void
106 colour_log_formatter::log_finish(std::ostream &ostr)
107 {
108         ostr.flush();
109 }
110
111 //____________________________________________________________________________//
112
113 void
114 colour_log_formatter::log_build_info(std::ostream &output, bool log_build_info)
115 {
116         if (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;
123 }
124
125 //____________________________________________________________________________//
126
127 void
128 colour_log_formatter::test_unit_start(
129         std::ostream &output,
130         test_unit const &tu)
131 {
132         if (test_unit_type_name_contains(tu, "suite")) {
133                 output << "Starting test ";
134         } else {
135                 output << "Running test ";
136         }
137         output << test_unit_type_name(tu) << " \"" << test_unit_name(tu)
138                << "\"" << std::endl;
139
140 }
141
142 //____________________________________________________________________________//
143
144 void
145 colour_log_formatter::test_unit_finish(
146         std::ostream &output,
147         test_unit const &tu,
148         unsigned long elapsed)
149 {
150         if (test_unit_type_name_contains(tu, "suite")) {
151                 output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
152                            std::endl;
153                 return;
154         }
155
156         std::string color = CKM::Colors::Text::GREEN_BEGIN;
157         std::string status = "OK";
158
159         if (m_isTestCaseFailed) {
160                 color = CKM::Colors::Text::RED_BEGIN;
161                 status = "FAIL";
162         }
163
164         output << "\t" << "[   " << color << status << CKM::Colors::Text::COLOR_END <<
165                    "   ]";
166
167
168         output << ", " << CKM::Colors::Text::CYAN_BEGIN << "time: ";
169
170         if (elapsed > 0) {
171                 if (elapsed % 1000 == 0)
172                         output << elapsed / 1000 << "ms";
173                 else
174                         output << elapsed << "mks";
175         } else {
176                 output << "N/A";
177         }
178
179         output << CKM::Colors::Text::COLOR_END << std::endl;
180         m_isTestCaseFailed = false;
181 }
182
183 //____________________________________________________________________________//
184
185 void
186 colour_log_formatter::test_unit_skipped(
187         std::ostream &output,
188         test_unit const &tu)
189 {
190         output  << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
191                         "is skipped" << std::endl;
192 }
193
194 //____________________________________________________________________________//
195
196 void
197 colour_log_formatter::log_exception(
198         std::ostream &output,
199         log_checkpoint_data const &checkpoint_data,
200         boost::execution_exception const &ex)
201 {
202         boost::execution_exception::location const &loc = ex.where();
203         output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
204                            loc.m_file_name)
205                    << '(' << loc.m_line_num << "), ";
206
207         output << "fatal error in \""
208                    << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
209                    "\": ";
210
211         output << CKM::Colors::Text::COLOR_END << ex.what();
212
213         if (!checkpoint_data.m_file_name.is_empty()) {
214                 output << '\n';
215                 output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
216                            << '(' << checkpoint_data.m_line_num << ")";
217
218                 if (!checkpoint_data.m_message.empty())
219                         output << ": " << checkpoint_data.m_message;
220         }
221
222         output << std::endl;
223         m_isTestCaseFailed = true;
224 }
225
226 //____________________________________________________________________________//
227
228 void
229 colour_log_formatter::log_entry_start(
230         std::ostream &output,
231         log_entry_data const &entry_data,
232         log_entry_types let)
233 {
234         switch (let) {
235         case BOOST_UTL_ET_INFO:
236                 output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
237                            "), ";
238                 output << "info: ";
239                 break;
240
241         case BOOST_UTL_ET_MESSAGE:
242                 break;
243
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() << "\": ";
248                 break;
249
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;
256                 break;
257
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;
264                 break;
265         }
266
267         output << CKM::Colors::Text::COLOR_END;
268 }
269
270 //____________________________________________________________________________//
271
272 void
273 colour_log_formatter::log_entry_value(
274         std::ostream &output,
275         const_string value)
276 {
277         output << value;
278 }
279
280 //____________________________________________________________________________//
281
282 void
283 colour_log_formatter::log_entry_value(
284         std::ostream &output,
285         lazy_ostream const &value)
286 {
287         output << value;
288 }
289
290 //____________________________________________________________________________//
291
292 void
293 colour_log_formatter::log_entry_finish(
294         std::ostream &output)
295 {
296         output << std::endl;
297 }
298
299 //____________________________________________________________________________//
300
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)
303 {
304         log_exception(os, lcd, ex);
305 }
306
307 void colour_log_formatter::log_exception_finish(std::ostream& os)
308 {
309         (void)os;
310 }
311
312 void colour_log_formatter::entry_context_start(std::ostream& os, boost::unit_test::log_level l)
313 {
314         (void)os;
315         (void)l;
316 }
317
318 void colour_log_formatter::log_entry_context(std::ostream& os, boost::unit_test::log_level l, boost::unit_test::const_string value)
319 {
320     (void)os;
321     (void)l;
322     (void)value;
323 }
324
325 void colour_log_formatter::entry_context_finish(std::ostream& os, boost::unit_test::log_level l)
326 {
327     (void)os;
328     (void)l;
329 }
330 #endif
331
332 //____________________________________________________________________________//
333
334 } // namespace CKM
335
336 //____________________________________________________________________________//
337