Improve debug logging

This commit is contained in:
Pascal Serrarens 2025-04-11 11:55:23 +02:00
parent 304856ea0a
commit 32ca8f5f71
5 changed files with 43 additions and 36 deletions

View File

@ -132,8 +132,8 @@ void ParticipantUDP::Receive() {
bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) { bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
#if defined(IDF_VER) #if defined(IDF_VER)
std::cout << "Sending to " << remoteParticipant->ipAddress << ":" // std::cout << "Sending to " << remoteParticipant->ipAddress << ":"
<< remoteParticipant->port << "\n"; // << remoteParticipant->port << "\n";
int err = sendto(this->sockfd, buffer, bufferSize, 0, int err = sendto(this->sockfd, buffer, bufferSize, 0,
(struct sockaddr*)&dest_addr, sizeof(dest_addr)); (struct sockaddr*)&dest_addr, sizeof(dest_addr));

View File

@ -4,16 +4,6 @@
namespace RoboidControl { 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) { ModelUrlMsg::ModelUrlMsg(unsigned char networkId, Thing* thing) {
this->networkId = networkId; this->networkId = networkId;
this->thingId = thing->id; this->thingId = thing->id;
@ -22,8 +12,6 @@ ModelUrlMsg::ModelUrlMsg(unsigned char networkId, Thing* thing) {
else else
this->urlLength = (unsigned char)strlen(thing->modelUrl); this->urlLength = (unsigned char)strlen(thing->modelUrl);
// this->url = thing->modelUrl; // dangerous!
// the url string in the buffer is not \0 terminated! // the url string in the buffer is not \0 terminated!
char* url = new char[this->urlLength + 1]; char* url = new char[this->urlLength + 1];
for (int i = 0; i < this->urlLength; i++) for (int i = 0; i < this->urlLength; i++)

View File

@ -15,7 +15,7 @@ class ModelUrlMsg : public IMessage {
/// @brief The ID of the thing /// @brief The ID of the thing
unsigned char thingId; 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; unsigned char urlLength;
/// @brief The url of the model, not terminated by a null character /// @brief The url of the model, not terminated by a null character
const char* url; const char* url;

View File

@ -39,8 +39,8 @@ Thing* Participant::Get(unsigned char thingId) {
if (thing->id == thingId) if (thing->id == thingId)
return thing; return thing;
} }
std::cout << "Could not find thing " << this->ipAddress << ":" << this->port // std::cout << "Could not find thing " << this->ipAddress << ":" << this->port
<< "[" << (int)thingId << "]\n"; // << "[" << (int)thingId << "]\n";
return nullptr; return nullptr;
} }

View File

@ -88,7 +88,6 @@ void ParticipantUDP::Update(unsigned long currentTimeMs) {
if (this->publishInterval > 0 && currentTimeMs > this->nextPublishMe) { if (this->publishInterval > 0 && currentTimeMs > this->nextPublishMe) {
ParticipantMsg* msg = new ParticipantMsg(this->networkId); ParticipantMsg* msg = new ParticipantMsg(this->networkId);
std::cout << "Send Participant\n";
if (this->remoteSite == nullptr) if (this->remoteSite == nullptr)
this->Publish(msg); this->Publish(msg);
else else
@ -160,7 +159,7 @@ Participant* ParticipantUDP::AddParticipant(const char* ipAddress, int port) {
void ParticipantUDP::SendThingInfo(Participant* remoteParticipant, void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
Thing* thing) { 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); ThingMsg* thingMsg = new ThingMsg(this->networkId, thing);
this->Send(remoteParticipant, thingMsg); this->Send(remoteParticipant, thingMsg);
delete thingMsg; delete thingMsg;
@ -253,12 +252,11 @@ bool ParticipantUDP::Publish(IMessage* msg) {
void ParticipantUDP::ReceiveData(unsigned char packetSize, void ParticipantUDP::ReceiveData(unsigned char packetSize,
char* senderIpAddress, char* senderIpAddress,
unsigned int senderPort) { unsigned int senderPort) {
Participant* sender = Participant* sender = this->GetParticipant(senderIpAddress, senderPort);
this->GetParticipant(senderIpAddress, senderPort);
if (sender == nullptr) { if (sender == nullptr) {
sender = this->AddParticipant(senderIpAddress, senderPort); sender = this->AddParticipant(senderIpAddress, senderPort);
std::cout << "New remote participant " << sender->ipAddress std::cout << "New remote participant " << sender->ipAddress << ":"
<< ":" << sender->port << std::endl; << sender->port << std::endl;
} }
ReceiveData(packetSize, sender); ReceiveData(packetSize, sender);
@ -267,7 +265,7 @@ void ParticipantUDP::ReceiveData(unsigned char packetSize,
void ParticipantUDP::ReceiveData(unsigned char bufferSize, void ParticipantUDP::ReceiveData(unsigned char bufferSize,
Participant* sender) { Participant* sender) {
unsigned char msgId = this->buffer[0]; 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"; // std::cout << " buffer size = " <<(int) bufferSize << "\n";
switch (msgId) { switch (msgId) {
case ParticipantMsg::id: { case ParticipantMsg::id: {
@ -325,13 +323,18 @@ void ParticipantUDP::ReceiveData(unsigned char bufferSize,
} }
void ParticipantUDP::Process(Participant* sender, ParticipantMsg* msg) { 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"; << "\n";
#endif
} }
void ParticipantUDP::Process(Participant* sender, SiteMsg* msg) { void ParticipantUDP::Process(Participant* sender, SiteMsg* msg) {
std::cout << this->name << ": process Site Id " << (int)this->networkId #if defined(DEBUG)
<< "->" << (int)msg->networkId << "\n"; std::cout << this->name << ": process SiteMsg " << (int)this->networkId
<< " -> " << (int)msg->networkId << "\n";
#endif
if (this->networkId != msg->networkId) { if (this->networkId != msg->networkId) {
this->networkId = msg->networkId; this->networkId = msg->networkId;
// std::cout << this->things.size() << " things\n"; // 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) { void ParticipantUDP::Process(Participant* sender, ThingMsg* msg) {
std::cout << this->name << ": process Thing [" << (int)msg->networkId << "/" #if defined(DEBUG)
<< (int)msg->thingId << "]\n"; 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) { 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 << "] "; << (int)msg->thingId << "] ";
#endif
Thing* thing = sender->Get(msg->thingId); Thing* thing = sender->Get(msg->thingId);
if (thing != nullptr) { if (thing != nullptr) {
@ -374,18 +386,25 @@ void ParticipantUDP::Process(Participant* sender, NameMsg* msg) {
} }
void ParticipantUDP::Process(Participant* sender, ModelUrlMsg* 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"; << "/" << (int)msg->thingId << "]\n";
#endif
} }
void ParticipantUDP::Process(Participant* sender, PoseMsg* msg) { void ParticipantUDP::Process(Participant* sender, PoseMsg* msg) {
std::cout << this->name << ": process Pose [" << (int)this->networkId << "/" #if defined(DEBUG)
<< (int)msg->networkId << "]\n"; std::cout << this->name << ": process PoseMsg [" << (int)this->networkId
<< "/" << (int)msg->networkId << "]\n";
#endif
} }
void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) { void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
std::cout << this->name << ": process Binary [" << (int)msg->networkId << "/" #if defined(DEBUG)
<< (int)msg->thingId << "] "; std::cout << this->name << ": process BinaryMsg [" << (int)msg->networkId
<< "/" << (int)msg->thingId << "] ";
#endif
Thing* thing = sender->Get(msg->thingId); Thing* thing = sender->Get(msg->thingId);
if (thing != nullptr) if (thing != nullptr)
thing->ProcessBinary(msg->data); thing->ProcessBinary(msg->data);