From 625c027c0ad3528b1da6e9087b0bd7fd518d0f2b Mon Sep 17 00:00:00 2001 From: Adrian Szyndela Date: Tue, 22 Jan 2019 15:37:34 +0100 Subject: [PATCH] internal: make debug logs small to be more readable The logs take relatively large space within actual source code. This makes them smaller to help with avoiding distraction. Change-Id: Iafca91f5e30f4446e28215cafbf78325ac9287bd --- src/internal/naive_policy_checker.cpp | 16 ++------ src/internal/naive_policy_db.cpp | 20 +++------- src/internal/own_tree.cpp | 4 +- src/internal/policy.cpp | 73 ++++++++++++++++++++--------------- src/internal/policy.hpp | 20 +++++++--- 5 files changed, 64 insertions(+), 69 deletions(-) diff --git a/src/internal/naive_policy_checker.cpp b/src/internal/naive_policy_checker.cpp index 0e2405b..f719e7f 100755 --- a/src/internal/naive_policy_checker.cpp +++ b/src/internal/naive_policy_checker.cpp @@ -13,9 +13,7 @@ DEF_NODESTRUCT_GLOBAL(ldp_xml_parser::NaivePolicyChecker, policy_checker) static void __log_item(const MatchItemSR& item) { - char tmp[MAX_LOG_LINE]; - const char* i_str = item.toString(tmp); - std::cout << "checkpolicy for: " << i_str <getDecision().toString(tmp); - std::cout << "-read: " << i_str; - i_str = i->toString(tmp); - std::cout << " " << i_str <getDecision() << " " << i << std::endl; } if (i->match(item)) { if (tslog::verbose()) { - char tmp[MAX_LOG_LINE]; - const char* i_str = i->getDecision().toString(tmp); - std::cout << "-matched: " << i_str; - const char* i_str2 = i->toString(tmp); - std::cout << " " << i_str2 <getDecision() << " " << i << std::endl; } return i->getDecision(); } diff --git a/src/internal/naive_policy_db.cpp b/src/internal/naive_policy_db.cpp index 1898b99..177df28 100755 --- a/src/internal/naive_policy_db.cpp +++ b/src/internal/naive_policy_db.cpp @@ -16,11 +16,7 @@ using namespace ldp_xml_parser; template void log_item_add(const T* item) { if (tslog::enabled()) { - char tmp[MAX_LOG_LINE]; - const char* i_str = item->toString(tmp); - char tmp2[MAX_LOG_LINE]; - const char* d_str = item->getDecision().toString(tmp2); - std::cout << "Add item: " << i_str << ", decision: " << d_str << std::endl; + std::cout << "Add item: " << item << ", decision: " << item->getDecision() << std::endl; } } @@ -211,11 +207,8 @@ size_t NaivePolicyDb::PolicySR::getSize() const { } void NaivePolicyDb::PolicySR::printContent() const { - char str[MAX_LOG_LINE]; - for (const auto& i : m_items) { - i->toString(str); - std::cerr << str << std::endl; - } + for (const auto& i : m_items) + std::cerr << i << std::endl; } void NaivePolicyDb::PolicyOwn::addItem(ItemOwn* item) { @@ -265,11 +258,8 @@ size_t NaivePolicyDb::PolicyAccess::getSize() const } void NaivePolicyDb::PolicyAccess::printContent() const { - char str[MAX_LOG_LINE]; - for (const auto& i : m_items) { - i.toString(str); - std::cerr << str << std::endl; - } + for (const auto& i : m_items) + std::cerr << i << std::endl; } template diff --git a/src/internal/own_tree.cpp b/src/internal/own_tree.cpp index e285526..9ae9b19 100644 --- a/src/internal/own_tree.cpp +++ b/src/internal/own_tree.cpp @@ -74,10 +74,8 @@ DecisionItem OwnershipTree::getDecisionItem(const ItemOwn& item) const void OwnershipTree::printTreeLevel(const TreeNode& node, const std::string& indent) const { - char str[MAX_LOG_LINE]; - std::cerr << indent << "| " << node.__token << " (" << node.__children.size() << ") | " - << node.__own_decision_item.toString(str) << " " << node.__own_prefix_decision_item.toString(str) << std::endl; + << node.__own_decision_item << " " << node.__own_prefix_decision_item << std::endl; for (const auto& i : node.__children) printTreeLevel(*i.second, indent + "\t"); diff --git a/src/internal/policy.cpp b/src/internal/policy.cpp index b10f7c6..ce0e3ae 100755 --- a/src/internal/policy.cpp +++ b/src/internal/policy.cpp @@ -239,11 +239,6 @@ ItemType DecisionItem::getType() const { return ItemType::GENERIC; } -const char* DecisionItem::toString(char* str) const { - snprintf(str, MAX_LOG_LINE, "%s%s%s", __decision_to_str(__decision), __privilege == "" ? "" : ":", __privilege.c_str()); - return str; -} - size_t DecisionItem::getSize() const { return get_string_heap_allocated_memory(__privilege); } @@ -267,11 +262,6 @@ ItemType ItemOwn::getType() const { return ItemType::OWN; } -const char* ItemOwn::toString(char* str) const { - snprintf(str, MAX_LOG_LINE, "ItemOwn: service(%s), pref(%d)", __name == "" ? "*" : __name.c_str(), __is_prefix); - return str; -} - const char* ItemOwn::getName() const { if (__name == "") { return nullptr; @@ -344,11 +334,6 @@ ItemSendReceive::ItemSendReceive(const char* name, __direction(direction) { } -const char* ItemSendReceive::toString(char* str) const { - snprintf(str, MAX_LOG_LINE, "ItemSR: name(%s), inter(%s), member(%s), path(%s), type(%s), dir(%s)", __name.name, __interface, __member, __path, __message_type_to_str(__type), __message_dir_to_str(__direction)); - return str; -} - ItemSendReceive::~ItemSendReceive() { delete[] __interface; delete[] __member; @@ -460,12 +445,6 @@ bool ItemAccess::match(const MatchItemAccess& query) const return false; } -const char* ItemAccess::toString(char* str) const -{ - snprintf(str, MAX_LOG_LINE, "ItemAccess: type(%s), uid(%d), gid(%d)", __access_type_to_str(__type), __uid, __gid); - return str; -} - size_t ItemAccess::getSize() const { return __decision.getSize(); @@ -582,17 +561,6 @@ void ItemBuilder::addName(const char* name, bool prefix) { sr->__is_name_prefix = prefix; } -const char* MatchItemSR::toString(char* str) const { - char tmp[MAX_LOG_LINE]; - tmp[0] = 0; - for (int i = 0; i < names_num; i++) { - std::strncat(tmp, names[i].name, sizeof(tmp) - strlen(tmp) - 1); - std::strncat(tmp, " ", sizeof(tmp) - strlen(tmp) - 1); - } - snprintf(str, MAX_LOG_LINE, "matcher: services(%s), interface(%s), member(%s), path(%s), type(%s), direction(%s)", tmp, interface, member, path, __message_type_to_str(type), __message_dir_to_str(direction) ); - return str; -} - void ItemBuilder::addInterface(const char* interface) { ItemSendReceive* sr = getSendReceiveItem(); sr->__interface = duplicate(interface); @@ -647,3 +615,44 @@ PolicyTypeValue::PolicyTypeValue(ContextType type) : context(type) { PolicyTypeValue::PolicyTypeValue(uid_t us) : user(us) { } + +namespace ldp_xml_parser { + +std::ostream &operator<<(std::ostream& stream, const DecisionItem &di) +{ + return stream << __decision_to_str(di.__decision) << (di.__privilege.empty() ? "" : ":") << di.__privilege; +} + +std::ostream &operator<<(std::ostream& stream, const ItemOwn &item) +{ + return stream << "ItemOwn: service(" << (item.__name.empty() ? "*" : item.__name) << + "), pref(" << item.__is_prefix << ")"; +} + +std::ostream &operator<<(std::ostream& stream, const MatchItemSR &item) +{ + stream << "matcher: services("; + for (int i = 0; i < item.names_num; i++) { + stream << item.names[i].name; + if (i != item.names_num -1) + stream << " "; + } + return stream << "), interface(" << item.interface << "), member(" << item.member << + "), path(" << item.path << "), type(" << __message_type_to_str(item.type) << "), direction(" << + __message_dir_to_str(item.direction) << ")"; +} + +std::ostream &operator<<(std::ostream& stream, const ItemSendReceive &item) +{ + return stream << "ItemSR: name(" << item.__name.name << "), inter(" << item.__interface << + "), member(" << item.__member << "), path(" << item.__path << "), type(" << + __message_type_to_str(item.__type) << "), dir(" << __message_dir_to_str(item.__direction) << ")"; +} + +std::ostream &operator<<(std::ostream& stream, const ItemAccess &item) +{ + return stream << "ItemAccess: type(" << __access_type_to_str(item.__type) << "), uid(" << + item.__uid << "), gid(" << item.__gid << ")"; +} + +} diff --git a/src/internal/policy.hpp b/src/internal/policy.hpp index dcee2f5..536ad8d 100755 --- a/src/internal/policy.hpp +++ b/src/internal/policy.hpp @@ -27,7 +27,6 @@ #include #include -#define MAX_LOG_LINE 1024 /** Maximum tree node children. It is connected with proper characters which can be used in name.*/ #define MAX_CHILDREN 65 @@ -122,9 +121,11 @@ namespace ldp_xml_parser Decision getDecision() const; const char* getPrivilege() const; ItemType getType() const; - const char* toString(char* str) const; size_t getSize() const; + + friend std::ostream &operator<<(std::ostream& stream, const DecisionItem &di); }; + std::ostream &operator<<(std::ostream& stream, const DecisionItem &di); /** Class contains info about ownership policy item */ class ItemOwn { @@ -140,12 +141,14 @@ namespace ldp_xml_parser const char* privilege = NULL); bool match(const char* const name) const; ItemType getType() const; - const char* toString(char* str) const; const DecisionItem& getDecision() const; const char* getName() const; bool isPrefix() const; bool isMatchAll() const; + + friend std::ostream &operator<<(std::ostream& stream, const ItemOwn &item); }; + std::ostream &operator<<(std::ostream& stream, const ItemOwn &item); /** Name structure for send/receive policy */ struct NameSR { @@ -166,8 +169,10 @@ namespace ldp_xml_parser MatchItemSR(const char* i = NULL, const char* me = NULL, const char* p = NULL, MessageType t = MessageType::ANY, MessageDirection d = MessageDirection::ANY); void addName(const char* name); bool addNames(const char* name); - const char* toString(char* str) const; + + friend std::ostream &operator<<(std::ostream& stream, const MatchItemSR &item); }; + std::ostream &operator<<(std::ostream& stream, const MatchItemSR &item); /** Class contains info about item send/receive */ class ItemSendReceive { @@ -192,10 +197,12 @@ namespace ldp_xml_parser bool match(const MatchItemSR& item) const; MessageDirection getDirection() const; ItemType getType() const; - const char* toString(char* str) const; const DecisionItem& getDecision() const; size_t getSize() const; + + friend std::ostream &operator<<(std::ostream& stream, const ItemSendReceive &item); }; + std::ostream &operator<<(std::ostream& stream, const ItemSendReceive &item); class MatchItemAccess { private: @@ -222,9 +229,10 @@ namespace ldp_xml_parser friend class ItemBuilder; const DecisionItem& getDecision() const; bool match(const MatchItemAccess& query) const; - const char* toString(char* str) const; size_t getSize() const; + friend std::ostream &operator<<(std::ostream& stream, const ItemAccess &item); }; + std::ostream &operator<<(std::ostream& stream, const ItemAccess &item); class NaivePolicyDb; -- 2.7.4