From b398de6e9f0c0a8c5e20ec47460190924e03347b Mon Sep 17 00:00:00 2001 From: sowle Date: Sat, 19 Oct 2019 06:38:21 +0300 Subject: [PATCH] minor log improvements in currency protocol and epee's net_utils_base --- contrib/epee/include/net/net_utils_base.h | 26 +++++++++--------- .../currency_protocol_handler.h | 4 +-- .../currency_protocol_handler.inl | 27 +++++++++++++------ 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/contrib/epee/include/net/net_utils_base.h b/contrib/epee/include/net/net_utils_base.h index 88586857..1ef06777 100644 --- a/contrib/epee/include/net/net_utils_base.h +++ b/contrib/epee/include/net/net_utils_base.h @@ -117,34 +117,34 @@ namespace net_utils //some helpers - inline - std::string print_connection_context(const connection_context_base& ctx) + inline std::string print_connection_context(const connection_context_base& ctx) { std::stringstream ss; ss << epee::string_tools::get_ip_string_from_int32(ctx.m_remote_ip) << ":" << ctx.m_remote_port << " " << epee::string_tools::get_str_from_guid_a(ctx.m_connection_id) << (ctx.m_is_income ? " INC":" OUT"); return ss.str(); } - inline - void print_connection_context_short(const connection_context_base& ctx, std::stringstream& ss) + inline std::ostream &operator <<(std::ostream &o, const connection_context_base& ctx) { - ss << epee::string_tools::get_ip_string_from_int32(ctx.m_remote_ip) << ":" << ctx.m_remote_port << (ctx.m_is_income ? " INC" : " OUT"); - } + o << epee::string_tools::get_ip_string_from_int32(ctx.m_remote_ip) << ":" << ctx.m_remote_port << (ctx.m_is_income ? " INC" : " OUT"); + return o; + } - inline - std::string print_connection_context_short(const connection_context_base& ctx) + inline std::string print_connection_context_short(const connection_context_base& ctx) { std::stringstream ss; - print_connection_context_short(ctx, ss); + ss << ctx; return ss.str(); } - inline std::string print_connection_context_list(const std::list& contexts) + inline std::string print_connection_context_list(const std::list& contexts, const std::string& delim = std::string("\n")) { std::stringstream ss; for (auto& c : contexts) { - ss << epee::string_tools::get_ip_string_from_int32(c.m_remote_ip) << ":" << c.m_remote_port << (c.m_is_income ? " INC" : " OUT") << ENDL; + if (ss.tellp()) + ss << delim; + ss << c; } return ss.str(); } @@ -179,7 +179,7 @@ namespace net_utils #define CHECK_AND_ASSERT_MES_CC(condition, return_val, err_message) CHECK_AND_ASSERT_MES(condition, return_val, "[" << epee::net_utils::print_connection_context_short(context) << "]" << err_message) -} -} +} // namespace net_utils +} // namespace epee #endif //_NET_UTILS_BASE_H_ diff --git a/src/currency_protocol/currency_protocol_handler.h b/src/currency_protocol/currency_protocol_handler.h index 08dd8ea8..58f9d218 100644 --- a/src/currency_protocol/currency_protocol_handler.h +++ b/src/currency_protocol/currency_protocol_handler.h @@ -119,7 +119,7 @@ namespace currency template bool post_notify(typename t_parametr::request& arg, currency_connection_context& context) { - LOG_PRINT_L2("[POST]" << typeid(t_parametr).name()); + LOG_PRINT_L2("[POST]" << typeid(t_parametr).name() << " to " << context); std::string blob; epee::serialization::store_t_to_binary(arg, blob); return m_p2p->invoke_notify_to_peer(t_parametr::ID, blob, context); @@ -133,7 +133,7 @@ namespace currency std::list relayed_peers; bool r = m_p2p->relay_notify_to_all(t_parametr::ID, arg_buff, exlude_context, relayed_peers); - LOG_PRINT_GREEN("[POST RELAY] " << typeid(t_parametr).name() << " relayed contexts list: " << ENDL << print_connection_context_list(relayed_peers), LOG_LEVEL_2); + LOG_PRINT_GREEN("[POST RELAY] " << typeid(t_parametr).name() << " relayed contexts list: " << print_connection_context_list(relayed_peers, ", "), LOG_LEVEL_2); return r; } }; diff --git a/src/currency_protocol/currency_protocol_handler.inl b/src/currency_protocol/currency_protocol_handler.inl index d9a36d8d..e9ca4eb7 100644 --- a/src/currency_protocol/currency_protocol_handler.inl +++ b/src/currency_protocol/currency_protocol_handler.inl @@ -94,22 +94,32 @@ namespace currency << std::setw(20) << "Peer id" << std::setw(25) << "Recv/Sent (idle,sec)" << std::setw(25) << "State" - << std::setw(20) << "Livetime(seconds)" + << std::setw(20) << "Livetime" << std::setw(20) << "Client version" << ENDL; + size_t incoming_count = 0, outgoing_count = 0; + std::multimap conn_map; m_p2p->for_each_connection([&](const connection_context& cntxt, nodetool::peerid_type peer_id) { - ss << std::setw(29) << std::left << std::string(cntxt.m_is_income ? "[INC]":"[OUT]") + + std::stringstream conn_ss; + time_t livetime = time(NULL) - cntxt.m_started; + conn_ss << std::setw(29) << std::left << std::string(cntxt.m_is_income ? "[INC]":"[OUT]") + string_tools::get_ip_string_from_int32(cntxt.m_remote_ip) + ":" + std::to_string(cntxt.m_remote_port) << std::setw(20) << std::hex << peer_id << std::setw(25) << std::to_string(cntxt.m_recv_cnt)+ "(" + std::to_string(time(NULL) - cntxt.m_last_recv) + ")" + "/" + std::to_string(cntxt.m_send_cnt) + "(" + std::to_string(time(NULL) - cntxt.m_last_send) + ")" << std::setw(25) << get_protocol_state_string(cntxt.m_state) - << std::setw(20) << std::to_string(time(NULL) - cntxt.m_started) + << std::setw(20) << epee::misc_utils::get_time_interval_string(livetime) << std::setw(20) << cntxt.m_remote_version << ENDL; + conn_map.insert(std::make_pair(livetime, conn_ss.str())); + (cntxt.m_is_income ? incoming_count : outgoing_count) += 1; return true; }); - LOG_PRINT_L0("Connections: " << ENDL << ss.str()); + + for(auto it = conn_map.rbegin(); it != conn_map.rend(); ++it) + ss << it->second; + + LOG_PRINT_L0("Connections (" << incoming_count << " in, " << outgoing_count << " out, " << incoming_count + outgoing_count << " total):" << ENDL << ss.str()); } //------------------------------------------------------------------------------------------------------------------------ template @@ -723,13 +733,14 @@ namespace currency if (req.txs.size()) { post_notify(req, cc); - print_connection_context_short(cc, debug_ss); - debug_ss << ": " << req.txs.size() << ENDL; + + if (debug_ss.tellp()) + debug_ss << ", "; + debug_ss << cc << ": " << req.txs.size(); } } TIME_MEASURE_FINISH_MS(ms); - LOG_PRINT_GREEN("[POST RELAY] NOTIFY_NEW_TRANSACTIONS relayed (" << ms << "ms)contexts list: " << debug_ss.str(), LOG_LEVEL_2); - + LOG_PRINT_GREEN("[POST RELAY] NOTIFY_NEW_TRANSACTIONS relayed (" << ms << "ms) to: " << debug_ss.str(), LOG_LEVEL_2); } //------------------------------------------------------------------------------------------------------------------------ template