Skip to content

Commit

Permalink
optimize log
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjseagull committed Sep 6, 2024
1 parent 4415b6e commit 4e6e2a0
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 33 deletions.
5 changes: 5 additions & 0 deletions cpp/ppc-framework/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ inline std::string_view printP2PIDElegantly(std::string_view p2pId) noexcept
return p2pId.substr(RSA_PUBLIC_KEY_PREFIX, RSA_PUBLIC_KEY_TRUNC);
}

template <typename T>
inline std::string printNodeID(T const& nodeID)
{
return std::string(nodeID.begin(), nodeID.begin() + 8);
}

#if ENABLE_CPU_FEATURES
#if X86
Expand Down
4 changes: 3 additions & 1 deletion cpp/ppc-framework/protocol/INodeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @date 2024-08-26
*/
#pragma once
#include "ppc-framework/Common.h"
#include <bcos-utilities/Common.h>
#include <memory>
#include <set>
Expand Down Expand Up @@ -77,7 +78,8 @@ inline std::string printNodeInfo(INodeInfo::Ptr const& nodeInfo)
return "nullptr";
}
std::ostringstream stringstream;
stringstream << LOG_KV("endPoint", nodeInfo->endPoint());
stringstream << LOG_KV("endPoint", nodeInfo->endPoint())
<< LOG_KV("nodeID", printNodeID(nodeInfo->nodeID()));
std::string components = "";
for (auto const& it : nodeInfo->components())
{
Expand Down
3 changes: 3 additions & 0 deletions cpp/wedpr-transport/ppc-gateway/ppc-gateway/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ namespace ppc::gateway
#define GATEWAY_LOG(LEVEL) BCOS_LOG(LEVEL) << "[GATEWAY]"
#define SERVICE_LOG(LEVEL) BCOS_LOG(LEVEL) << "[GATEWAY][SERVICE]"
#define SERVICE_ROUTER_LOG(LEVEL) BCOS_LOG(LEVEL) << "[GATEWAY][SERVICE][ROUTER]"
#define LOCAL_ROUTER_LOG(LEVEL) BCOS_LOG(LEVEL) << "[GATEWAY][LOCAL_ROUTER]"
#define PEER_ROUTER_LOG(LEVEL) BCOS_LOG(LEVEL) << "[GATEWAY][PEER_ROUTER]"
#define ROUTER_MGR_LOG(LEVEL) BCOS_LOG(LEVEL) << "[GATEWAY][ROUTER_MGR]"

// HTTP HEADER DEFINE
#define HEAD_TASK_ID "x-ptp-session-id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,18 @@ void GatewayImpl::start()
return;
}
m_running = true;
m_service->start();
m_p2pRouterManager->start();
m_gatewayRouterManager->start();
if (m_service)
{
m_service->start();
}
if (m_p2pRouterManager)
{
m_p2pRouterManager->start();
}
if (m_gatewayRouterManager)
{
m_gatewayRouterManager->start();
}
GATEWAY_LOG(INFO) << LOG_DESC("Start gateway success");
}

Expand All @@ -77,10 +86,19 @@ void GatewayImpl::stop()
return;
}
m_running = false;
m_service->stop();
m_p2pRouterManager->stop();
m_gatewayRouterManager->stop();
GATEWAY_LOG(INFO) << LOG_DESC("Stop gateway success");
if (m_service)
{
m_service->stop();
}
if (m_p2pRouterManager)
{
m_p2pRouterManager->stop();
}
if (m_gatewayRouterManager)
{
m_gatewayRouterManager->stop();
}
PEER_ROUTER_LOG(INFO) << LOG_DESC("Stop gateway success");
}

void GatewayImpl::asyncSendbroadcastMessage(ppc::protocol::RouteType routeType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,24 @@ void GatewayRouterManager::start()
{
if (m_running)
{
GATEWAY_LOG(INFO) << LOG_DESC("GatewayRouterManager has already been started");
ROUTER_MGR_LOG(INFO) << LOG_DESC("GatewayRouterManager has already been started");
return;
}
m_running = true;
m_timer->start();
GATEWAY_LOG(INFO) << LOG_DESC("start GatewayRouterManager success");
ROUTER_MGR_LOG(INFO) << LOG_DESC("start GatewayRouterManager success");
}

void GatewayRouterManager::stop()
{
if (!m_running)
{
GATEWAY_LOG(INFO) << LOG_DESC("GatewayRouterManager has already been stopped");
ROUTER_MGR_LOG(INFO) << LOG_DESC("GatewayRouterManager has already been stopped");
return;
}
m_running = false;
m_timer->stop();
GATEWAY_LOG(INFO) << LOG_DESC("stop GatewayRouterManager success");
ROUTER_MGR_LOG(INFO) << LOG_DESC("stop GatewayRouterManager success");
}

void GatewayRouterManager::onReceiveNodeSeqMessage(MessageFace::Ptr msg, WsSession::Ptr session)
Expand All @@ -93,9 +93,9 @@ void GatewayRouterManager::onReceiveNodeSeqMessage(MessageFace::Ptr msg, WsSessi
return;
}
// status changed, request for the nodeStatus
GATEWAY_LOG(TRACE) << LOG_DESC("onReceiveNodeSeqMessage")
<< LOG_KV("from", printP2PIDElegantly(from))
<< LOG_KV("statusSeq", statusSeq);
ROUTER_MGR_LOG(TRACE)
<< LOG_DESC("onReceiveNodeSeqMessage: statusChanged, request the lastest nodeStatus")
<< LOG_KV("node", printP2PIDElegantly(from)) << LOG_KV("statusSeq", statusSeq);
m_service->asyncSendMessageByP2PNodeID(
(uint16_t)GatewayPacketType::RequestNodeStatus, from, std::make_shared<bcos::bytes>());
}
Expand All @@ -122,7 +122,7 @@ void GatewayRouterManager::broadcastStatusSeq()
auto statusSeq = boost::asio::detail::socket_ops::host_to_network_long(seq);
auto payload = std::make_shared<bytes>((byte*)&statusSeq, (byte*)&statusSeq + 4);
message->setPayload(payload);
GATEWAY_LOG(TRACE) << LOG_DESC("broadcastStatusSeq") << LOG_KV("seq", seq);
ROUTER_MGR_LOG(TRACE) << LOG_DESC("broadcastStatusSeq") << LOG_KV("seq", seq);
m_service->asyncBroadcastMessage(message);
}

Expand All @@ -138,12 +138,14 @@ void GatewayRouterManager::onReceiveRequestNodeStatusMsg(
auto nodeStatusData = m_localRouter->generateNodeStatus();
if (!nodeStatusData)
{
GATEWAY_LOG(WARNING) << LOG_DESC("onReceiveRequestNodeStatusMsg: generate nodeInfo error")
<< LOG_KV("from", printP2PIDElegantly(from));
ROUTER_MGR_LOG(WARNING) << LOG_DESC(
"onReceiveRequestNodeStatusMsg: generate nodeInfo error")
<< LOG_KV("from", printP2PIDElegantly(from));
return;
}
GATEWAY_LOG(TRACE) << LOG_DESC("onReceiveRequestNodeStatusMsg: response the latest nodeStatus")
<< LOG_KV("from", printP2PIDElegantly(from));
ROUTER_MGR_LOG(TRACE) << LOG_DESC(
"onReceiveRequestNodeStatusMsg: response the latest nodeStatus")
<< LOG_KV("node", printP2PIDElegantly(from));
m_service->asyncSendMessageByP2PNodeID(
(uint16_t)GatewayPacketType::ResponseNodeStatus, from, nodeStatusData);
}
Expand All @@ -158,10 +160,10 @@ void GatewayRouterManager::onRecvResponseNodeStatusMsg(MessageFace::Ptr msg, WsS
p2pMessage->header()->srcGwNode() :
session->nodeId();

GATEWAY_LOG(INFO) << LOG_DESC("onRecvResponseNodeStatusMsg")
<< LOG_KV("from", printP2PIDElegantly(from))
<< LOG_KV("statusSeq", nodeStatus->statusSeq())
<< LOG_KV("agency", nodeStatus->agency());
ROUTER_MGR_LOG(INFO) << LOG_DESC("onRecvResponseNodeStatusMsg")
<< LOG_KV("node", printP2PIDElegantly(from))
<< LOG_KV("statusSeq", nodeStatus->statusSeq())
<< LOG_KV("agency", nodeStatus->agency());
updatePeerNodeStatus(from, nodeStatus);
}

Expand All @@ -178,9 +180,9 @@ void GatewayRouterManager::updatePeerNodeStatus(
UpgradeGuard ul(l);
m_p2pID2Seq[p2pID] = statusSeq;
}
GATEWAY_LOG(INFO) << LOG_DESC("updatePeerNodeStatus")
<< LOG_KV("from", printP2PIDElegantly(p2pID))
<< LOG_KV("statusSeq", status->statusSeq())
<< LOG_KV("agency", status->agency());
ROUTER_MGR_LOG(INFO) << LOG_DESC("updatePeerNodeStatus")
<< LOG_KV("node", printP2PIDElegantly(p2pID))
<< LOG_KV("statusSeq", status->statusSeq())
<< LOG_KV("agency", status->agency());
m_peerRouter->updateGatewayInfo(status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@ using namespace ppc::gateway;

bool LocalRouter::registerNodeInfo(ppc::protocol::INodeInfo::Ptr nodeInfo)
{
GATEWAY_LOG(INFO) << LOG_DESC("registerNodeInfo") << printNodeInfo(nodeInfo);
LOCAL_ROUTER_LOG(INFO) << LOG_DESC("registerNodeInfo") << printNodeInfo(nodeInfo);
nodeInfo->setFront(m_frontBuilder->buildClient(nodeInfo->endPoint()));
auto ret = m_routerInfo->tryAddNodeInfo(nodeInfo);
if (ret)
{
LOCAL_ROUTER_LOG(INFO) << LOG_DESC("registerNodeInfo success") << printNodeInfo(nodeInfo);
increaseSeq();
}
return ret;
}
// Note: the change of the topic will not trigger router-update
void LocalRouter::registerTopic(bcos::bytesConstRef _nodeID, std::string const& topic)
{
GATEWAY_LOG(INFO) << LOG_DESC("registerTopic") << LOG_KV("topic", topic);
LOCAL_ROUTER_LOG(INFO) << LOG_DESC("registerTopic") << LOG_KV("topic", topic)
<< LOG_KV("nodeID", printNodeID(_nodeID));
m_routerInfo->registerTopic(_nodeID.toBytes(), topic);
// try to dispatch the cacheInfo
if (!m_cache)
Expand All @@ -65,6 +67,8 @@ void LocalRouter::registerTopic(bcos::bytesConstRef _nodeID, std::string const&
// Note: the change of the topic will not trigger router-update
void LocalRouter::unRegisterTopic(bcos::bytesConstRef _nodeID, std::string const& topic)
{
LOCAL_ROUTER_LOG(INFO) << LOG_DESC("unRegisterTopic") << LOG_KV("topic", topic)
<< LOG_KV("nodeID", printNodeID(_nodeID));
m_routerInfo->unRegisterTopic(_nodeID.toBytes(), topic);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ using namespace ppc::protocol;

void PeerRouterTable::updateGatewayInfo(GatewayNodeInfo::Ptr const& gatewayInfo)
{
PEER_ROUTER_LOG(INFO) << LOG_DESC("updateGatewayInfo")
<< LOG_KV("detail", printNodeStatus(gatewayInfo));
auto nodeList = gatewayInfo->nodeList();
bcos::WriteGuard l(x_mutex);
for (auto const& it : nodeList)
Expand Down Expand Up @@ -138,9 +140,9 @@ void PeerRouterTable::asyncBroadcastMessage(ppc::protocol::Message::Ptr const& m
{
continue;
}
GATEWAY_LOG(TRACE) << LOG_DESC("asyncBroadcastMessage")
<< LOG_KV("nodeID", printP2PIDElegantly(selectedNode->p2pNodeID()))
<< LOG_KV("msg", printMessage(msg));
PEER_ROUTER_LOG(TRACE) << LOG_DESC("asyncBroadcastMessage")
<< LOG_KV("nodeID", printP2PIDElegantly(selectedNode->p2pNodeID()))
<< LOG_KV("msg", printMessage(msg));
m_service->asyncSendMessageByNodeID(selectedNode->p2pNodeID(), msg);
}
}

0 comments on commit 4e6e2a0

Please sign in to comment.