Update print functions

This commit is contained in:
Sybren A. Stüvel 2026-03-14 19:29:49 +01:00
parent 9f7d644a0c
commit 90bb54b3b7
2 changed files with 38 additions and 28 deletions

View file

@ -217,24 +217,18 @@ void Dispatcher::checkRecv() {
}
if (pkt) {
#if MESH_PACKET_LOGGING
{
char buf[128];
snprintf(buf, sizeof(buf), "%s: RX, len=%d (type=%d, route=%s, payload_len=%d) SNR=%d RSSI=%d score=%d time=%d",
getLogDateTime(), pkt->getRawLength(), pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len,
(int)pkt->getSNR(), (int)_radio->getLastRSSI(), (int)(score*1000), air_time);
_packet_log->print(buf);
}
_packet_log.printf("%s: RX, len=%d (type=%d, route=%s, payload_len=%d) SNR=%d RSSI=%d score=%d time=%d",
getLogDateTime(), pkt->getRawLength(), pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len,
(int)pkt->getSNR(), (int)_radio->getLastRSSI(), (int)(score*1000), air_time);
static uint8_t packet_hash[MAX_HASH_SIZE];
pkt->calculatePacketHash(packet_hash);
_packet_log->print(" hash=");
mesh::Utils::printHex(*_packet_log, packet_hash, MAX_HASH_SIZE);
_packet_log.print(" hash=");
mesh::Utils::printHex(_packet_log, packet_hash, MAX_HASH_SIZE);
if (pkt->getPayloadType() == PAYLOAD_TYPE_PATH || pkt->getPayloadType() == PAYLOAD_TYPE_REQ
|| pkt->getPayloadType() == PAYLOAD_TYPE_RESPONSE || pkt->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) {
char buf[16];
snprintf(buf, sizeof(buf), " [%02X -> %02X]\n", (uint32_t)pkt->payload[1], (uint32_t)pkt->payload[0]);
_packet_log->print(buf);
_packet_log.printf(" [%02X -> %02X]\n", (uint32_t)pkt->payload[1], (uint32_t)pkt->payload[0]);
} else {
_packet_log->print("\n");
_packet_log.print("\n");
}
#endif
logRx(pkt, pkt->getRawLength(), score); // hook for custom logging
@ -341,19 +335,13 @@ void Dispatcher::checkSend() {
outbound_expiry = futureMillis(max_airtime);
#if MESH_PACKET_LOGGING
{
char buf[128];
snprintf(buf, sizeof(buf), "%s: TX, len=%d (type=%d, route=%s, payload_len=%d)",
getLogDateTime(), len, outbound->getPayloadType(), outbound->isRouteDirect() ? "D" : "F", outbound->payload_len);
_packet_log->print(buf);
}
_packet_log.printf("%s: TX, len=%d (type=%d, route=%s, payload_len=%d)",
getLogDateTime(), len, outbound->getPayloadType(), outbound->isRouteDirect() ? "D" : "F", outbound->payload_len);
if (outbound->getPayloadType() == PAYLOAD_TYPE_PATH || outbound->getPayloadType() == PAYLOAD_TYPE_REQ
|| outbound->getPayloadType() == PAYLOAD_TYPE_RESPONSE || outbound->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) {
char buf[16];
snprintf(buf, sizeof(buf), " [%02X -> %02X]\n", (uint32_t)outbound->payload[1], (uint32_t)outbound->payload[0]);
_packet_log->print(buf);
_packet_log.printf(" [%02X -> %02X]\n", (uint32_t)outbound->payload[1], (uint32_t)outbound->payload[0]);
} else {
_packet_log->print("\n");
_packet_log.print("\n");
}
#endif
}

View file

@ -3,6 +3,31 @@
#include <MeshCore.h>
#if MESH_PACKET_LOGGING && ARDUINO
#include <Arduino.h>
#include <stdarg.h>
class LogPrint : public Print {
Print* _impl;
public:
LogPrint() : _impl(&Serial) {}
void setStream(Print* s) { if (s) _impl = s; }
size_t write(uint8_t c) override { return _impl->write(c); }
size_t write(const uint8_t* buf, size_t n) override { return _impl->write(buf, n); }
void printf(const char* fmt, ...) {
char buf[192]; // sized for longest log line: ~31 char datetime + ~90 char fields
va_list args;
va_start(args, fmt);
int n = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
if (n >= (int)sizeof(buf)) {
// truncation occurred: mark it visibly rather than silently losing data
buf[sizeof(buf) - 4] = '.';
buf[sizeof(buf) - 3] = '.';
buf[sizeof(buf) - 2] = '\n';
buf[sizeof(buf) - 1] = '\0';
}
_impl->print(buf);
}
};
#endif
#include <Identity.h>
#include <Packet.h>
@ -133,7 +158,7 @@ class Dispatcher {
void processRecvPacket(Packet* pkt);
void updateTxBudget();
#if MESH_PACKET_LOGGING
Print* _packet_log;
LogPrint _packet_log;
#endif
protected:
@ -156,9 +181,6 @@ protected:
tx_budget_ms = 0;
last_budget_update = 0;
duty_cycle_window_ms = 3600000;
#if MESH_PACKET_LOGGING
_packet_log = &Serial;
#endif
}
virtual DispatcherAction onRecvPacket(Packet* pkt) = 0;
@ -182,7 +204,7 @@ public:
void begin();
void loop();
#if MESH_PACKET_LOGGING
void setPacketLogStream(Print* s) { if (s) _packet_log = s; }
void setPacketLogStream(Print* s) { _packet_log.setStream(s); }
#endif
Packet* obtainNewPacket();