Add log colouring formatter for boost utf
authorZofia Abramowska <z.abramowska@samsung.com>
Wed, 2 Jul 2014 09:09:44 +0000 (11:09 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 12 Sep 2014 12:58:49 +0000 (14:58 +0200)
Change-Id: I89a56caeed30e97e3046a5852652ec9f74ca727b

src/manager/dpl/core/include/dpl/colors.h
src/manager/dpl/core/src/colors.cpp
tests/CMakeLists.txt
tests/colour_log_formatter.cpp [new file with mode: 0644]
tests/colour_log_formatter.h [new file with mode: 0644]
tests/test_common.h

index 19e8258..5750425 100644 (file)
@@ -21,8 +21,8 @@
  *              and html output
  */
 
-#ifndef CENT_KEY_COLORS_H
-#define CENT_KEY_COLORS_H
+#ifndef CKM_COLORS_H
+#define CKM_COLORS_H
 
 namespace CKM {
 namespace Colors {
@@ -45,29 +45,9 @@ extern const char* BOLD_GOLD_BEGIN;
 extern const char* BOLD_GOLD_END;
 extern const char* BOLD_WHITE_BEGIN;
 extern const char* BOLD_WHITE_END;
+extern const char* COLOR_END;
 } //namespace Text
-
-namespace Html {
-extern const char* BOLD_GREEN_BEGIN;
-extern const char* BOLD_GREEN_END;
-extern const char* PURPLE_BEGIN;
-extern const char* PURPLE_END;
-extern const char* RED_BEGIN;
-extern const char* RED_END;
-extern const char* GREEN_BEGIN;
-extern const char* GREEN_END;
-extern const char* CYAN_BEGIN;
-extern const char* CYAN_END;
-extern const char* BOLD_RED_BEGIN;
-extern const char* BOLD_RED_END;
-extern const char* BOLD_YELLOW_BEGIN;
-extern const char* BOLD_YELLOW_END;
-extern const char* BOLD_GOLD_BEGIN;
-extern const char* BOLD_GOLD_END;
-extern const char* BOLD_WHITE_BEGIN;
-extern const char* BOLD_WHITE_END;
-} //namespace Html
 } //namespace Colors
 } //namespace CKM
 
-#endif /* CENT_KEY_COLORS_H */
+#endif /* CKM_COLORS_H */
index 5758512..7b329b0 100644 (file)
@@ -44,27 +44,7 @@ const char* BOLD_GOLD_BEGIN = "\033[0;33m";
 const char* BOLD_GOLD_END = "\033[m";
 const char* BOLD_WHITE_BEGIN = "\033[1;37m";
 const char* BOLD_WHITE_END = "\033[m";
+const char* COLOR_END = "\033[m";
 } //namespace Text
-
-namespace Html {
-const char* BOLD_GREEN_BEGIN = "<font color=\"green\"><b>";
-const char* BOLD_GREEN_END = "</b></font>";
-const char* PURPLE_BEGIN = "<font color=\"purple\"><b>";
-const char* PURPLE_END = "</b></font>";
-const char* RED_BEGIN = "<font color=\"red\"><b>";
-const char* RED_END = "</b></font>";
-const char* GREEN_BEGIN = "<font color=\"green\">";
-const char* GREEN_END = "</font>";
-const char* CYAN_BEGIN = "<font color=\"cyan\">";
-const char* CYAN_END = "</font>";
-const char* BOLD_RED_BEGIN = "<font color=\"red\"><b>";
-const char* BOLD_RED_END = "</b></font>";
-const char* BOLD_YELLOW_BEGIN = "<font color=\"yellow\"><b>";
-const char* BOLD_YELLOW_END = "</b></font>";
-const char* BOLD_GOLD_BEGIN = "<font color=\"gold\"><b>";
-const char* BOLD_GOLD_END = "</b></font>";
-const char* BOLD_WHITE_BEGIN = "<font color=\"white\"><b>";
-const char* BOLD_WHITE_END = "</b></font>";
-} //namespace Html
 } //namespace Colors
 } //namespace CKM
index bd78cab..00430ca 100644 (file)
@@ -27,6 +27,7 @@ INCLUDE_DIRECTORIES(
 
 SET(TEST_MERGED_SOURCES
     ${KEY_MANAGER_TEST_MERGED_SRC}/test_common.cpp
+    ${KEY_MANAGER_TEST_MERGED_SRC}/colour_log_formatter.cpp
     ${KEY_MANAGER_TEST_MERGED_SRC}/test_db_crypto.cpp
     ${KEY_MANAGER_TEST_MERGED_SRC}/test_sql.cpp
     ${KEY_MANAGER_TEST_MERGED_SRC}/test-key-provider.cpp
diff --git a/tests/colour_log_formatter.cpp b/tests/colour_log_formatter.cpp
new file mode 100644 (file)
index 0000000..8555eac
--- /dev/null
@@ -0,0 +1,238 @@
+// Boost.Test
+#include <colour_log_formatter.h>
+#include <boost/test/unit_test_suite_impl.hpp>
+#include <boost/test/framework.hpp>
+#include <boost/test/utils/basic_cstring/io.hpp>
+#include <boost/test/utils/lazy_ostream.hpp>
+
+// Boost
+#include <boost/version.hpp>
+
+// STL
+#include <iostream>
+#include <string>
+
+#include <dpl/colors.h>
+
+// ************************************************************************** //
+// **************            colour_log_formatter            ************** //
+// ************************************************************************** //
+
+using namespace boost::unit_test;
+namespace CKM {
+
+namespace {
+
+const_string
+test_phase_identifier()
+{
+    return framework::is_initialized()
+            ? const_string( framework::current_test_case().p_name.get() )
+            : BOOST_TEST_L( "Test setup" );
+}
+
+const_string
+get_basename(const const_string &file_name) {
+    return basename(file_name.begin());
+}
+
+std::string
+get_basename(const std::string &file_name) {
+    return basename(file_name.c_str());
+}
+
+} // local namespace
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_start(
+        std::ostream& output,
+        counter_t test_cases_amount )
+{
+    if( test_cases_amount > 0 )
+        output  << "Running " << test_cases_amount << " test "
+                << (test_cases_amount > 1 ? "cases" : "case") << "...\n";
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_finish( std::ostream& ostr )
+{
+    ostr.flush();
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_build_info( std::ostream& output )
+{
+    output  << "Platform: " << BOOST_PLATFORM            << '\n'
+            << "Compiler: " << BOOST_COMPILER            << '\n'
+            << "STL     : " << BOOST_STDLIB              << '\n'
+            << "Boost   : " << BOOST_VERSION/100000      << "."
+                            << BOOST_VERSION/100 % 1000  << "."
+                            << BOOST_VERSION % 100       << std::endl;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::test_unit_start(
+        std::ostream& output,
+        test_unit const& tu )
+{
+    if (tu.p_type_name->find(const_string("suite")) == 0) {
+        output << "Starting test " << tu.p_type_name << " \"" << tu.p_name << "\"" << std::endl;
+    } else {
+        output << "Running test " << tu.p_type_name << " \"" << tu.p_name << "\"" << std::endl;
+    }
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::test_unit_finish(
+        std::ostream& output,
+        test_unit const& tu,
+        unsigned long elapsed )
+{
+    if (tu.p_type_name->find(const_string("suite")) == 0) {
+        output << "Finished test " << tu.p_type_name << " \"" << tu.p_name << "\""<< std::endl;
+        return;
+    }
+    std::string color = CKM::Colors::Text::GREEN_BEGIN;
+    std::string status = "OK";
+    if (m_isTestCaseFailed) {
+        color = CKM::Colors::Text::RED_BEGIN;
+        status = "FAIL";
+    }
+    output << "\t" << "[   " << color << status << CKM::Colors::Text::COLOR_END << "   ]";
+
+
+    output << ", " << CKM::Colors::Text::CYAN_BEGIN << "time: ";
+    if( elapsed > 0 ) {
+        if( elapsed % 1000 == 0 )
+            output << elapsed/1000 << "ms";
+        else
+            output << elapsed << "mks";
+    } else {
+        output << "N/A";
+    }
+
+    output << CKM::Colors::Text::COLOR_END << std::endl;
+    m_isTestCaseFailed = false;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::test_unit_skipped(
+        std::ostream& output,
+        test_unit const& tu )
+{
+    output  << "Test " << tu.p_type_name << " \"" << tu.p_name << "\"" << "is skipped" << std::endl;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_exception(
+        std::ostream& output,
+        log_checkpoint_data const& checkpoint_data,
+        boost::execution_exception const& ex )
+{
+    boost::execution_exception::location const& loc = ex.where();
+    output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
+            << '(' << loc.m_line_num << "), ";
+
+    output << "fatal error in \""
+            << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function ) << "\": ";
+
+    output << CKM::Colors::Text::COLOR_END << ex.what();
+
+    if( !checkpoint_data.m_file_name.is_empty() ) {
+        output << '\n';
+        output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
+                << '(' << checkpoint_data.m_line_num << ")";
+        if( !checkpoint_data.m_message.empty() )
+            output << ": " << checkpoint_data.m_message;
+    }
+
+    output << std::endl;
+    m_isTestCaseFailed = true;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_entry_start(
+        std::ostream& output,
+        log_entry_data const& entry_data,
+        log_entry_types let )
+{
+    switch( let ) {
+        case BOOST_UTL_ET_INFO:
+            output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num << "), ";
+            output << "info: ";
+            break;
+        case BOOST_UTL_ET_MESSAGE:
+            break;
+        case BOOST_UTL_ET_WARNING:
+            output << '\t' << get_basename(entry_data.m_file_name) << '(' << entry_data.m_line_num << "), ";
+            output << "warning in \"" << test_phase_identifier() << "\": ";
+            break;
+        case BOOST_UTL_ET_ERROR:
+            output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN <<  get_basename(entry_data.m_file_name)
+                << '(' << entry_data.m_line_num << "), ";
+            output << "error in \"" << test_phase_identifier() << "\": ";
+            m_isTestCaseFailed = true;
+            break;
+        case BOOST_UTL_ET_FATAL_ERROR:
+            output << '\t' << CKM::Colors::Text::BOLD_YELLOW_BEGIN <<  get_basename(entry_data.m_file_name)
+                << '(' << entry_data.m_line_num << "),  ";
+            output <<  " fatal error in \"" << test_phase_identifier() << "\": ";
+            m_isTestCaseFailed = true;
+            break;
+    }
+    output << CKM::Colors::Text::COLOR_END;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_entry_value(
+        std::ostream& output,
+        const_string value )
+{
+    output << value;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_entry_value(
+        std::ostream& output,
+        lazy_ostream const& value )
+{
+    output << value;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_entry_finish(
+        std::ostream& output )
+{
+    output << std::endl;
+}
+
+//____________________________________________________________________________//
+
+//____________________________________________________________________________//
+
+} // namespace CKM
+
+//____________________________________________________________________________//
+
diff --git a/tests/colour_log_formatter.h b/tests/colour_log_formatter.h
new file mode 100644 (file)
index 0000000..d463af6
--- /dev/null
@@ -0,0 +1,49 @@
+#ifndef COLOUR_LOG_FORMATTER_H_
+#define COLOUR_LOG_FORMATTER_H_
+
+#include <boost/test/unit_test_log_formatter.hpp>
+
+namespace CKM {
+class colour_log_formatter : public boost::unit_test::unit_test_log_formatter {
+public:
+    // Formatter interface
+    colour_log_formatter() : m_isTestCaseFailed(false) {}
+    void    log_start(
+                std::ostream&,
+                boost::unit_test::counter_t test_cases_amount );
+    void    log_finish( std::ostream& );
+    void    log_build_info( std::ostream& );
+
+    void    test_unit_start(
+                std::ostream&,
+                boost::unit_test::test_unit const& tu );
+    void    test_unit_finish(
+                std::ostream&,
+                boost::unit_test::test_unit const& tu,
+                unsigned long elapsed );
+    void    test_unit_skipped(
+                std::ostream&,
+                boost::unit_test::test_unit const& tu );
+
+    void    log_exception(
+                std::ostream&,
+                boost::unit_test::log_checkpoint_data const&,
+                boost::execution_exception const& ex );
+
+    void    log_entry_start(
+                std::ostream&,
+                boost::unit_test::log_entry_data const&,
+                log_entry_types let );
+    void    log_entry_value(
+                std::ostream&,
+                boost::unit_test::const_string value );
+    void    log_entry_value(
+                std::ostream&,
+                boost::unit_test::lazy_ostream const& value );
+    void    log_entry_finish( std::ostream& );
+private:
+    bool m_isTestCaseFailed;
+};
+} // namespace CKM
+
+#endif /* COLOUR_LOG_FORMATTER_H_ */
index 16fe2e4..c4a5865 100644 (file)
@@ -3,13 +3,17 @@
 #include <ckm/ckm-type.h>
 #include <boost/test/unit_test_log.hpp>
 #include <boost/test/results_reporter.hpp>
+#include <colour_log_formatter.h>
 
 struct TestConfig {
     TestConfig() {
         boost::unit_test::unit_test_log.set_threshold_level( boost::unit_test::log_test_units);
         boost::unit_test::results_reporter::set_level(boost::unit_test::SHORT_REPORT);
+        boost::unit_test::unit_test_log.set_formatter(new CKM::colour_log_formatter);
     }
-    ~TestConfig(){}
+    ~TestConfig(){
+    }
+private:
 };
 
 CKM::RawBuffer createDefaultPass();