Skip to content

Commit 0ac324b

Browse files
committed
add printf via DEBUG_PORT define see #909
1 parent 7a4c416 commit 0ac324b

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/WebSockets.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@
5050
DEBUG_ESP_PORT.flush(); \
5151
}
5252
#else
53-
// #define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ )
53+
#ifdef DEBUG_PORT
54+
#define DEBUG_WEBSOCKETS(...) debug_printf( __VA_ARGS__ )
55+
void debug_printf(const char *format, ...);
56+
#endif
5457
#endif
5558
#endif
5659

src/debug.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#include "WebSockets.h"
3+
4+
#include <stdarg.h>
5+
6+
#ifdef DEBUG_PORT
7+
8+
void debug_printf(const char *format, ...) {
9+
va_list arg;
10+
va_start(arg, format);
11+
char temp[64];
12+
char* buffer = temp;
13+
size_t len = vsnprintf(temp, sizeof(temp), format, arg);
14+
va_end(arg);
15+
if (len > sizeof(temp) - 1) {
16+
buffer = new (std::nothrow) char[len + 1];
17+
if (!buffer) {
18+
return 0;
19+
}
20+
va_start(arg, format);
21+
vsnprintf(buffer, len + 1, format, arg);
22+
va_end(arg);
23+
}
24+
len = DEBUG_PORT.write((const uint8_t*) buffer, len);
25+
if (buffer != temp) {
26+
delete[] buffer;
27+
}
28+
return len;
29+
}
30+
31+
#endif

0 commit comments

Comments
 (0)