diff --git a/src/RedirectablePrint.cpp b/src/RedirectablePrint.cpp index 14e85f0c8..943d38982 100644 --- a/src/RedirectablePrint.cpp +++ b/src/RedirectablePrint.cpp @@ -5,6 +5,7 @@ #include "configuration.h" #include #include +#include #include #include @@ -173,3 +174,22 @@ void RedirectablePrint::hexDump(const char *logLevel, unsigned char *buf, uint16 } log(logLevel, " +------------------------------------------------+ +----------------+\n"); } + +std::string RedirectablePrint::mt_sprintf(const std::string fmt_str, ...) +{ + int final_n, n = ((int)fmt_str.size()) * 2; /* Reserve two times as much as the length of the fmt_str */ + std::unique_ptr formatted; + va_list ap; + while (1) { + formatted.reset(new char[n]); /* Wrap the plain char array into the unique_ptr */ + strcpy(&formatted[0], fmt_str.c_str()); + va_start(ap, fmt_str); + final_n = vsnprintf(&formatted[0], n, fmt_str.c_str(), ap); + va_end(ap); + if (final_n < 0 || final_n >= n) + n += abs(final_n - n + 1); + else + break; + } + return std::string(formatted.get()); +} \ No newline at end of file diff --git a/src/RedirectablePrint.h b/src/RedirectablePrint.h index 659b6e10d..560021972 100644 --- a/src/RedirectablePrint.h +++ b/src/RedirectablePrint.h @@ -2,6 +2,7 @@ #include #include +#include /** * A Printable that can be switched to squirt its bytes to a different sink. @@ -40,6 +41,8 @@ class RedirectablePrint : public Print size_t vprintf(const char *format, va_list arg); void hexDump(const char *logLevel, unsigned char *buf, uint16_t len); + + std::string mt_sprintf(const std::string fmt_str, ...); }; class NoopPrint : public Print diff --git a/src/mesh/RadioInterface.cpp b/src/mesh/RadioInterface.cpp index 61c864394..94dab0bcd 100644 --- a/src/mesh/RadioInterface.cpp +++ b/src/mesh/RadioInterface.cpp @@ -241,47 +241,48 @@ uint32_t RadioInterface::getTxDelayMsecWeighted(float snr) void printPacket(const char *prefix, const meshtastic_MeshPacket *p) { - LOG_DEBUG("%s (id=0x%08x fr=0x%02x to=0x%02x, WantAck=%d, HopLim=%d Ch=0x%x", prefix, p->id, p->from & 0xff, p->to & 0xff, - p->want_ack, p->hop_limit, p->channel); + std::string out = DEBUG_PORT.mt_sprintf("%s (id=0x%08x fr=0x%02x to=0x%02x, WantAck=%d, HopLim=%d Ch=0x%x", prefix, p->id, + p->from & 0xff, p->to & 0xff, p->want_ack, p->hop_limit, p->channel); if (p->which_payload_variant == meshtastic_MeshPacket_decoded_tag) { auto &s = p->decoded; - LOG_DEBUG(" Portnum=%d", s.portnum); + out += DEBUG_PORT.mt_sprintf(" Portnum=%d", s.portnum); if (s.want_response) - LOG_DEBUG(" WANTRESP"); + out += DEBUG_PORT.mt_sprintf(" WANTRESP"); if (s.source != 0) - LOG_DEBUG(" source=%08x", s.source); + out += DEBUG_PORT.mt_sprintf(" source=%08x", s.source); if (s.dest != 0) - LOG_DEBUG(" dest=%08x", s.dest); + out += DEBUG_PORT.mt_sprintf(" dest=%08x", s.dest); if (s.request_id) - LOG_DEBUG(" requestId=%0x", s.request_id); + out += DEBUG_PORT.mt_sprintf(" requestId=%0x", s.request_id); /* now inside Data and therefore kinda opaque if (s.which_ackVariant == SubPacket_success_id_tag) - LOG_DEBUG(" successId=%08x", s.ackVariant.success_id); + out += DEBUG_PORT.mt_sprintf(" successId=%08x", s.ackVariant.success_id); else if (s.which_ackVariant == SubPacket_fail_id_tag) - LOG_DEBUG(" failId=%08x", s.ackVariant.fail_id); */ + out += DEBUG_PORT.mt_sprintf(" failId=%08x", s.ackVariant.fail_id); */ } else { - LOG_DEBUG(" encrypted"); + out += " encrypted"; } if (p->rx_time != 0) { - LOG_DEBUG(" rxtime=%u", p->rx_time); + out += DEBUG_PORT.mt_sprintf(" rxtime=%u", p->rx_time); } if (p->rx_snr != 0.0) { - LOG_DEBUG(" rxSNR=%g", p->rx_snr); + out += DEBUG_PORT.mt_sprintf(" rxSNR=%g", p->rx_snr); } if (p->rx_rssi != 0) { - LOG_DEBUG(" rxRSSI=%i", p->rx_rssi); + out += DEBUG_PORT.mt_sprintf(" rxRSSI=%i", p->rx_rssi); } if (p->priority != 0) - LOG_DEBUG(" priority=%d", p->priority); + out += DEBUG_PORT.mt_sprintf(" priority=%d", p->priority); - LOG_DEBUG(")\n"); + out + ")\n"; + LOG_DEBUG("%s", out.c_str()); } RadioInterface::RadioInterface()