Skip to content

Commit

Permalink
add printf via DEBUG_PORT define see #909
Browse files Browse the repository at this point in the history
  • Loading branch information
Links2004 committed Sep 28, 2024
1 parent 7a4c416 commit 0ac324b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/WebSockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 31 additions & 0 deletions src/debug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

#include "WebSockets.h"

#include <stdarg.h>

#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

0 comments on commit 0ac324b

Please sign in to comment.