diff --git a/test/framework/utils.cc b/test/framework/utils.cc index 1b8a10d..de535e3 100644 --- a/test/framework/utils.cc +++ b/test/framework/utils.cc @@ -18,27 +18,42 @@ using namespace pEp; namespace pEp { namespace Test { namespace Log { - void logH1(string msg) + void log(const string &msg) { + lograw(msg + "\n"); + } + + void logH1(const string &msg) + { + stringstream tmp; char decoration{ '=' }; - cout << endl - << endl - << std::string(30, decoration) << ' ' << msg << ' ' - << std::string(30, decoration) << endl; + tmp << endl + << endl + << std::string(30, decoration) << ' ' << msg << ' ' + << std::string(30, decoration) << endl; + lograw(tmp.str()); } - void logH2(string msg) + void logH2(const string &msg) { + stringstream tmp; char decoration{ '-' }; - cout << endl - << std::string(10, decoration) << ' ' << msg << ' ' - << std::string(10, decoration) << endl; + 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(::pEp_identity *ident, bool full, int indent) + string to_string(const ::pEp_identity *const ident, bool full, int indent) { stringstream builder; if (ident != nullptr) { @@ -81,14 +96,14 @@ namespace pEp { return builder.str(); } - std::string to_string(::identity_list *idl, bool full, int indent) + std::string to_string(const ::identity_list *const idl, bool full, int indent) { stringstream builder; if (idl != nullptr) { builder << endl; builder << std::string(indent, '\t') << "[" << endl; indent++; - for (::identity_list *curr = idl; curr != nullptr; curr = curr->next) { + for (const ::identity_list *curr = idl; curr != nullptr; curr = curr->next) { builder << to_string(curr->ident, full, indent) << endl; } indent--; @@ -100,7 +115,7 @@ namespace pEp { return builder.str(); } - string to_string(::pEp_member *member, bool full, int indent) + string to_string(const ::pEp_member *const member, bool full, int indent) { stringstream builder; if (member != nullptr) { @@ -118,14 +133,14 @@ namespace pEp { return builder.str(); } - string to_string(::member_list *mbl, bool full, int indent) + string to_string(const ::member_list *const mbl, bool full, int indent) { stringstream builder; if (mbl != nullptr) { builder << endl; builder << std::string(indent, '\t') << "[" << endl; indent++; - for (member_list *curr_member = mbl; curr_member != nullptr; + for (const member_list *curr_member = mbl; curr_member != nullptr; curr_member = curr_member->next) { builder << to_string(curr_member->member, full, indent) << endl; } @@ -138,22 +153,21 @@ namespace pEp { return builder.str(); } - string to_string(::pEp_group *group, bool full, int indent) + string to_string(const ::pEp_group *const group, bool full, int indent) { stringstream builder; if (group != nullptr) { builder << endl; builder << std::string(indent, '\t') << "{" << endl; indent++; - builder << std::string(indent, '\t') << "group_identity: " - << to_string(group->group_identity, full, indent) << endl; builder << std::string(indent, '\t') - << "manager: " << to_string(group->manager, full, indent) + << "group_identity: " << to_string(group->group_identity, full, indent) << endl; + builder << std::string(indent, '\t') + << "manager: " << to_string(group->manager, full, indent) << endl; builder << std::string(indent, '\t') << "active: " << group->active << endl; builder << std::string(indent, '\t') - << "members: " << to_string(group->members, full, indent) - << endl; + << "members: " << to_string(group->members, full, indent) << endl; indent--; builder << std::string(indent, '\t') << "]"; } else { @@ -163,17 +177,31 @@ namespace pEp { return builder.str(); } - void print_exception(const exception &e, int level) + string nested_exception_to_string(const exception &e, int level, string src) { - cerr << string(level, ' ') << "exception: " << e.what() << endl; + src += string(level, ' ') + "exception: " + e.what() + "\n"; try { rethrow_if_nested(e); } catch (const exception &e) { - print_exception(e, level + 1); + src = nested_exception_to_string(e, level + 1, src); } catch (...) { } + return src; } + + // void print_exception(const exception &e, int level) + // { + // cerr << string(level, ' ') << "exception: " << e.what() << endl; + // try { + // rethrow_if_nested(e); + // } catch (const exception &e) { + // print_exception(e, level + 1); + // } catch (...) { + // } + // } + + // File utils ofstream file_create(const string &filename) { @@ -198,7 +226,7 @@ namespace pEp { } } - void file_ensure_not_existing(string path) + void file_ensure_not_existing(const string &path) { while (file_exists(path)) { file_delete(path); diff --git a/test/framework/utils.hh b/test/framework/utils.hh index ccd96a3..189caad 100644 --- a/test/framework/utils.hh +++ b/test/framework/utils.hh @@ -13,31 +13,37 @@ namespace pEp { namespace Test { namespace Log { - void logH1(std::string msg); - void logH2(std::string msg); - } + 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(::pEp_identity *ident, bool full = true, int indent = 0); - std::string to_string(::identity_list *idl, bool full = true, int indent = 0); - std::string to_string(::pEp_member *member, bool full = true, int indent = 0); - std::string to_string(::member_list *mbl, bool full = true, int indent = 0); - std::string to_string(::pEp_group *group, bool full = true, int indent = 0); + std::string to_string(const ::pEp_identity *const ident, bool full = true, int indent = 0); + std::string to_string(const ::identity_list *const idl, bool full = true, int indent = 0); + std::string to_string(const ::pEp_member *const member, bool full = true, int indent = 0); + std::string to_string(const ::member_list *const mbl, bool full = true, int indent = 0); + std::string to_string(const ::pEp_group *const group, bool full = true, int indent = 0); // C++/STL data types to string template - std::string to_string(std::vector v); + std::string to_string(const std::vector &v); // exception utils - void print_exception(const std::exception& e, int level = 0); + std::string nested_exception_to_string( + const std::exception &e, + int level = 0, + std::string src = ""); + void print_exception(const std::exception &e, int level = 0); // file utils std::ofstream file_create(const std::string &filename); bool file_exists(const std::string &filename); void file_delete(const std::string &filename); - void file_ensure_not_existing(std::string path); - } - } // namespace Test + void file_ensure_not_existing(const std::string &path); + } // namespace Utils + } // namespace Test } // namespace pEp #include "utils.hxx"