From 7eb968891677be33854bcf36a17ff9568fbc230c 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 | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/debug.cpp diff --git a/src/WebSockets.h b/src/WebSockets.h index c918c9f..0b26189 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(...) websocket_debug_printf(__VA_ARGS__) +void websocket_debug_printf(const char * format, ...); +#endif #endif #endif diff --git a/src/debug.cpp b/src/debug.cpp new file mode 100644 index 0000000..a4e6391 --- /dev/null +++ b/src/debug.cpp @@ -0,0 +1,32 @@ + +#include "WebSockets.h" + +#include + +#ifdef DEBUG_PORT + +void websocket_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; + } + DEBUG_PORT.flush(); + return len; +} + +#endif \ No newline at end of file