From cf7c5feea15d1251827d957cad76675aad4607af Mon Sep 17 00:00:00 2001 From: heck Date: Sat, 24 Apr 2021 22:57:04 +0200 Subject: [PATCH] Test: Framework improve logging depend on pEpLog, but add logging macros that are for testing and are not controlled by pEpLog::set_enabled(), so you can disable library logging. --- test/framework/utils.cc | 34 --------------------- test/framework/utils.hh | 66 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 40 deletions(-) diff --git a/test/framework/utils.cc b/test/framework/utils.cc index de535e3..29d3bc9 100644 --- a/test/framework/utils.cc +++ b/test/framework/utils.cc @@ -17,40 +17,6 @@ using namespace pEp; namespace pEp { namespace Test { - namespace Log { - void log(const string &msg) - { - lograw(msg + "\n"); - } - - void logH1(const string &msg) - { - stringstream tmp; - char decoration{ '=' }; - tmp << endl - << endl - << std::string(30, decoration) << ' ' << msg << ' ' - << std::string(30, decoration) << endl; - lograw(tmp.str()); - } - - void logH2(const string &msg) - { - stringstream tmp; - char decoration{ '-' }; - tmp << endl - << std::string(10, decoration) << ' ' << msg << ' ' - << std::string(10, decoration) << endl; - lograw(tmp.str()); - } - - void lograw(const string &msg) - { - cerr << msg; - } - - } // namespace Log - namespace Utils { string to_string(const ::pEp_identity *const ident, bool full, int indent) diff --git a/test/framework/utils.hh b/test/framework/utils.hh index 189caad..bf5847c 100644 --- a/test/framework/utils.hh +++ b/test/framework/utils.hh @@ -4,20 +4,74 @@ #ifndef LIBPEPADAPTER_UTILS_HH #define LIBPEPADAPTER_UTILS_HH +#include "../../src/pEpLog.hh" #include #include #include #include #include +// ------------------------------------------------------------------------------------------------ + +#ifndef ASSERT_EXCEPT + #define ASSERT_EXCEPT(func) \ + do { \ + try { \ + (func); \ + assert(false); \ + } catch (const exception &e) { \ + pEp::Adapter::pEpLog::log(nested_exception_to_string(e)); \ + } \ + } while (0) +#endif + +// ------------------------------------------------------------------------------------------------ +// Logging macros for testing +// ------------------------------------------------------------------------------------------------ +// Use the macros if you need the message to be prefixed with "thread - __FILE__::__FUNTION__" +// OTHERWISE, just use the logging functions from pEp::Adapter::pEpLog + +// TESTLOG - logformat "thread - __FILE__::__FUNTION__ - " +// To be used in a non-class/object context +#ifndef TESTLOG + #define TESTLOG(msg) \ + do { \ + std::stringstream msg_; \ + msg_ << std::this_thread::get_id(); \ + msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \ + msg_ << " - " << msg; \ + pEp::Adapter::pEpLog::log(msg_.str()); \ + } while (0) +#endif // TESTLOG + +// TESTLOGH1 - logformat "Thread - __FILE__::__FUNTION__ - <=============== message ==============>" +#ifndef TESTLOGH1 + #define TESTLOGH1(msg) \ + do { \ + std::stringstream msg_; \ + msg_ << std::this_thread::get_id(); \ + msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \ + msg_ << " - " << pEp::Adapter::pEpLog::decorateH1(msg); \ + pEp::Adapter::pEpLog::log(msg_.str()); \ + } while (0) +#endif // TESTLOGH1 + +// TESTLOGH2 - logformat "Thread - __FILE__::__FUNTION__ - <--------------- message -------------->" +#ifndef TESTLOGH2 + #define TESTLOGH2(msg) \ + do { \ + std::stringstream msg_; \ + msg_ << std::this_thread::get_id(); \ + msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \ + msg_ << " - " << pEp::Adapter::pEpLog::decorateH2(msg); \ + pEp::Adapter::pEpLog::log(msg_.str()); \ + } while (0) +#endif // TESTLOGH2 + +// ------------------------------------------------------------------------------------------------ + namespace pEp { namespace Test { - namespace Log { - void log(const std::string &msg); - void logH1(const std::string &msg); - void logH2(const std::string &msg); - void lograw(const std::string &msg); - } // namespace Log namespace Utils { // pEpEngine datatypes to string std::string to_string(const ::pEp_identity *const ident, bool full = true, int indent = 0);