From 32ca8f5f713867b58897f001b8f26538b04d6dcf Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 11 Apr 2025 11:55:23 +0200 Subject: [PATCH] Improve debug logging --- EspIdf/EspIdfParticipant.cpp | 4 +-- Messages/ModelUrlMsg.cpp | 12 ------- Messages/ModelUrlMsg.h | 2 +- Participant.cpp | 4 +-- Participants/ParticipantUDP.cpp | 57 ++++++++++++++++++++++----------- 5 files changed, 43 insertions(+), 36 deletions(-) diff --git a/EspIdf/EspIdfParticipant.cpp b/EspIdf/EspIdfParticipant.cpp index b0f7974..22e8160 100644 --- a/EspIdf/EspIdfParticipant.cpp +++ b/EspIdf/EspIdfParticipant.cpp @@ -132,8 +132,8 @@ void ParticipantUDP::Receive() { bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) { #if defined(IDF_VER) - std::cout << "Sending to " << remoteParticipant->ipAddress << ":" - << remoteParticipant->port << "\n"; + // std::cout << "Sending to " << remoteParticipant->ipAddress << ":" + // << remoteParticipant->port << "\n"; int err = sendto(this->sockfd, buffer, bufferSize, 0, (struct sockaddr*)&dest_addr, sizeof(dest_addr)); diff --git a/Messages/ModelUrlMsg.cpp b/Messages/ModelUrlMsg.cpp index 0753750..df003be 100644 --- a/Messages/ModelUrlMsg.cpp +++ b/Messages/ModelUrlMsg.cpp @@ -4,16 +4,6 @@ namespace RoboidControl { -// ModelUrlMsg::ModelUrlMsg(unsigned char networkId, unsigned char thingId, -// unsigned char urlLength, const char *url, -// float scale) { -// this->networkId = networkId; -// this->thingId = thingId; -// this->urlLength = urlLength; -// this->url = url; -// this->scale = scale; -// } - ModelUrlMsg::ModelUrlMsg(unsigned char networkId, Thing* thing) { this->networkId = networkId; this->thingId = thing->id; @@ -22,8 +12,6 @@ ModelUrlMsg::ModelUrlMsg(unsigned char networkId, Thing* thing) { else this->urlLength = (unsigned char)strlen(thing->modelUrl); - // this->url = thing->modelUrl; // dangerous! - // the url string in the buffer is not \0 terminated! char* url = new char[this->urlLength + 1]; for (int i = 0; i < this->urlLength; i++) diff --git a/Messages/ModelUrlMsg.h b/Messages/ModelUrlMsg.h index 3c35e68..7c493eb 100644 --- a/Messages/ModelUrlMsg.h +++ b/Messages/ModelUrlMsg.h @@ -15,7 +15,7 @@ class ModelUrlMsg : public IMessage { /// @brief The ID of the thing unsigned char thingId; - /// @brief The length of the url st5ring, excluding the null terminator + /// @brief The length of the url string, excluding the null terminator unsigned char urlLength; /// @brief The url of the model, not terminated by a null character const char* url; diff --git a/Participant.cpp b/Participant.cpp index 80b484f..65a1f51 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -39,8 +39,8 @@ Thing* Participant::Get(unsigned char thingId) { if (thing->id == thingId) return thing; } - std::cout << "Could not find thing " << this->ipAddress << ":" << this->port - << "[" << (int)thingId << "]\n"; + // std::cout << "Could not find thing " << this->ipAddress << ":" << this->port + // << "[" << (int)thingId << "]\n"; return nullptr; } diff --git a/Participants/ParticipantUDP.cpp b/Participants/ParticipantUDP.cpp index a406449..f643587 100644 --- a/Participants/ParticipantUDP.cpp +++ b/Participants/ParticipantUDP.cpp @@ -88,7 +88,6 @@ void ParticipantUDP::Update(unsigned long currentTimeMs) { if (this->publishInterval > 0 && currentTimeMs > this->nextPublishMe) { ParticipantMsg* msg = new ParticipantMsg(this->networkId); - std::cout << "Send Participant\n"; if (this->remoteSite == nullptr) this->Publish(msg); else @@ -160,7 +159,7 @@ Participant* ParticipantUDP::AddParticipant(const char* ipAddress, int port) { void ParticipantUDP::SendThingInfo(Participant* remoteParticipant, Thing* thing) { - std::cout << "Send thing info [" << (int)thing->id << "] \n"; + // std::cout << "Send thing info [" << (int)thing->id << "] \n"; ThingMsg* thingMsg = new ThingMsg(this->networkId, thing); this->Send(remoteParticipant, thingMsg); delete thingMsg; @@ -253,12 +252,11 @@ bool ParticipantUDP::Publish(IMessage* msg) { void ParticipantUDP::ReceiveData(unsigned char packetSize, char* senderIpAddress, unsigned int senderPort) { - Participant* sender = - this->GetParticipant(senderIpAddress, senderPort); + Participant* sender = this->GetParticipant(senderIpAddress, senderPort); if (sender == nullptr) { sender = this->AddParticipant(senderIpAddress, senderPort); - std::cout << "New remote participant " << sender->ipAddress - << ":" << sender->port << std::endl; + std::cout << "New remote participant " << sender->ipAddress << ":" + << sender->port << std::endl; } ReceiveData(packetSize, sender); @@ -267,7 +265,7 @@ void ParticipantUDP::ReceiveData(unsigned char packetSize, void ParticipantUDP::ReceiveData(unsigned char bufferSize, Participant* sender) { unsigned char msgId = this->buffer[0]; - std::cout << "receive msg " << (int)msgId << "\n"; + // std::cout << "receive msg " << (int)msgId << "\n"; // std::cout << " buffer size = " <<(int) bufferSize << "\n"; switch (msgId) { case ParticipantMsg::id: { @@ -325,13 +323,18 @@ void ParticipantUDP::ReceiveData(unsigned char bufferSize, } void ParticipantUDP::Process(Participant* sender, ParticipantMsg* msg) { - std::cout << this->name << ": Process Participant " << (int)msg->networkId +#if defined(DEBUG) + std::cout << this->name << ": Process ParticipantMsg " << (int)msg->networkId << "\n"; +#endif } void ParticipantUDP::Process(Participant* sender, SiteMsg* msg) { - std::cout << this->name << ": process Site Id " << (int)this->networkId - << "->" << (int)msg->networkId << "\n"; +#if defined(DEBUG) + std::cout << this->name << ": process SiteMsg " << (int)this->networkId + << " -> " << (int)msg->networkId << "\n"; +#endif + if (this->networkId != msg->networkId) { this->networkId = msg->networkId; // std::cout << this->things.size() << " things\n"; @@ -340,16 +343,25 @@ void ParticipantUDP::Process(Participant* sender, SiteMsg* msg) { } } -void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) {} +void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) { +#if defined(DEBUG) + std::cout << this->name << ": Process InvestigateMsg [" << (int)msg->networkId + << "/" << (int)msg->thingId << "]\n"; +#endif +} void ParticipantUDP::Process(Participant* sender, ThingMsg* msg) { - std::cout << this->name << ": process Thing [" << (int)msg->networkId << "/" - << (int)msg->thingId << "]\n"; +#if defined(DEBUG) + std::cout << this->name << ": process ThingMsg [" << (int)msg->networkId + << "/" << (int)msg->thingId << "] " << (int)msg->thingType << " " << (int)msg->parentId << "\n"; +#endif } void ParticipantUDP::Process(Participant* sender, NameMsg* msg) { - std::cout << this->name << ": process Name [" << (int)msg->networkId << "/" +#if defined(DEBUG) + std::cout << this->name << ": process NameMsg [" << (int)msg->networkId << "/" << (int)msg->thingId << "] "; +#endif Thing* thing = sender->Get(msg->thingId); if (thing != nullptr) { @@ -374,18 +386,25 @@ void ParticipantUDP::Process(Participant* sender, NameMsg* msg) { } void ParticipantUDP::Process(Participant* sender, ModelUrlMsg* msg) { - std::cout << this->name << ": process model url [" << (int)msg->networkId +#if defined(DEBUG) + std::cout << this->name << ": process ModelUrlMsg [" << (int)msg->networkId << "/" << (int)msg->thingId << "]\n"; +#endif } void ParticipantUDP::Process(Participant* sender, PoseMsg* msg) { - std::cout << this->name << ": process Pose [" << (int)this->networkId << "/" - << (int)msg->networkId << "]\n"; +#if defined(DEBUG) + std::cout << this->name << ": process PoseMsg [" << (int)this->networkId + << "/" << (int)msg->networkId << "]\n"; +#endif } void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) { - std::cout << this->name << ": process Binary [" << (int)msg->networkId << "/" - << (int)msg->thingId << "] "; +#if defined(DEBUG) + std::cout << this->name << ": process BinaryMsg [" << (int)msg->networkId + << "/" << (int)msg->thingId << "] "; +#endif + Thing* thing = sender->Get(msg->thingId); if (thing != nullptr) thing->ProcessBinary(msg->data);