From 0ac324b15e8a30807edbede39cf86223ee8e48e6 Mon Sep 17 00:00:00 2001 From: Links2004 Date: Sat, 28 Sep 2024 18:19:34 +0200 Subject: [PATCH] add printf via DEBUG_PORT define see #909 --- src/WebSockets.h | 5 ++++- src/debug.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/debug.cpp diff --git a/src/WebSockets.h b/src/WebSockets.h index c918c9f..4cb90e4 100644 --- a/src/WebSockets.h +++ b/src/WebSockets.h @@ -50,7 +50,10 @@ DEBUG_ESP_PORT.flush(); \ } #else -// #define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ ) +#ifdef DEBUG_PORT +#define DEBUG_WEBSOCKETS(...) debug_printf( __VA_ARGS__ ) + void debug_printf(const char *format, ...); +#endif #endif #endif diff --git a/src/debug.cpp b/src/debug.cpp new file mode 100644 index 0000000..0ef7551 --- /dev/null +++ b/src/debug.cpp @@ -0,0 +1,31 @@ + +#include "WebSockets.h" + +#include + +#ifdef DEBUG_PORT + +void debug_printf(const char *format, ...) { + va_list arg; + va_start(arg, format); + char temp[64]; + char* buffer = temp; + size_t len = vsnprintf(temp, sizeof(temp), format, arg); + va_end(arg); + if (len > sizeof(temp) - 1) { + buffer = new (std::nothrow) char[len + 1]; + if (!buffer) { + return 0; + } + va_start(arg, format); + vsnprintf(buffer, len + 1, format, arg); + va_end(arg); + } + len = DEBUG_PORT.write((const uint8_t*) buffer, len); + if (buffer != temp) { + delete[] buffer; + } + return len; +} + +#endif \ No newline at end of file