Merge pull request #2870 from Aircoookie/net_debug

UDP Network debugging
This commit is contained in:
Christian Schwinne 2022-11-06 22:50:35 +01:00 committed by GitHub
commit 8143be29d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 134 additions and 9 deletions

View File

@ -15,13 +15,19 @@ uint32_t colorBalanceFromKelvin(uint16_t kelvin, uint32_t rgb);
void colorRGBtoRGBW(byte* rgb);
// enable additional debug output
#if defined(WLED_DEBUG_HOST)
#define DEBUGOUT NetDebug
#else
#define DEBUGOUT Serial
#endif
#ifdef WLED_DEBUG
#ifndef ESP8266
#include <rom/rtc.h>
#endif
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#define DEBUG_PRINTF(x...) Serial.printf(x)
#define DEBUG_PRINT(x) DEBUGOUT.print(x)
#define DEBUG_PRINTLN(x) DEBUGOUT.println(x)
#define DEBUG_PRINTF(x...) DEBUGOUT.printf(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)

75
wled00/net_debug.cpp Normal file
View File

@ -0,0 +1,75 @@
#include "wled.h"
#ifdef WLED_DEBUG_HOST
NetworkDebugPrinter NetDebug;
void NetworkDebugPrinter::print(const char *s, bool newline) {
if (!WLED_CONNECTED || !udpConnected || s == nullptr) return;
if (!debugPrintHostIP && !debugPrintHostIP.fromString(netDebugPrintHost)) {
#ifdef ESP8266
WiFi.hostByName(netDebugPrintHost, debugPrintHostIP, 750);
#else
#ifdef WLED_USE_ETHERNET
ETH.hostByName(netDebugPrintHost, debugPrintHostIP);
#else
WiFi.hostByName(netDebugPrintHost, debugPrintHostIP);
#endif
#endif
}
WiFiUDP debugUdp;
debugUdp.beginPacket(debugPrintHostIP, netDebugPrintPort);
debugUdp.write(reinterpret_cast<const uint8_t *>(s), strlen(s));
if (newline) debugUdp.write('\n');
debugUdp.endPacket();
}
void NetworkDebugPrinter::print(const __FlashStringHelper* s, bool newline) {
char buf[512];
strncpy_P(buf, (PGM_P)s, 512);
print(buf, newline);
}
void NetworkDebugPrinter::print(String s) {
print(s.c_str());
}
void NetworkDebugPrinter::print(unsigned int n, bool newline) {
char s[10];
snprintf_P(s, sizeof(s), PSTR("%d"), n);
s[9] = '\0';
print(s, newline);
}
void NetworkDebugPrinter::println() {
print("", true);
}
void NetworkDebugPrinter::println(const char *s) {
print(s, true);
}
void NetworkDebugPrinter::println(const __FlashStringHelper* s) {
print(s, true);
}
void NetworkDebugPrinter::println(String s) {
print(s.c_str(), true);
}
void NetworkDebugPrinter::println(unsigned int n) {
print(n, true);
}
void NetworkDebugPrinter::printf(const char *fmt...) {
va_list args;
va_start(args, fmt);
char s[1024];
vsnprintf(s, sizeof(s), fmt, args);
va_end(args);
print(s);
}
#endif

25
wled00/net_debug.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef WLED_NET_DEBUG_H
#define WLED_NET_DEBUG_H
#include <WString.h>
#include <WiFiUdp.h>
class NetworkDebugPrinter {
private:
IPAddress debugPrintHostIP;
public:
void print(const char *s, bool newline = false);
void print(const __FlashStringHelper* s, bool newline = false);
void print(String s);
void print(unsigned int n, bool newline = false);
void println();
void println(const char *s);
void println(const __FlashStringHelper* s);
void println(String s);
void println(unsigned int n);
void printf(const char *fmt, ...);
};
extern NetworkDebugPrinter NetDebug;
#endif

View File

@ -95,6 +95,10 @@
#include "my_config.h"
#endif
#ifdef WLED_DEBUG_HOST
#include "net_debug.h"
#endif
#include <ESPAsyncWebServer.h>
#ifdef WLED_ADD_EEPROM_SUPPORT
#include <EEPROM.h>
@ -669,13 +673,27 @@ WLED_GLOBAL StaticJsonDocument<JSON_BUFFER_SIZE> doc;
WLED_GLOBAL volatile uint8_t jsonBufferLock _INIT(0);
// enable additional debug output
#if defined(WLED_DEBUG_HOST)
// On the host side, use netcat to receive the log statements: nc -l 7868 -u
// use -D WLED_DEBUG_HOST='"192.168.xxx.xxx"' or FQDN within quotes
#define DEBUGOUT NetDebug
WLED_GLOBAL char netDebugPrintHost[33] _INIT(WLED_DEBUG_HOST);
#if defined(WLED_DEBUG_NET_PORT)
WLED_GLOBAL int netDebugPrintPort _INIT(WLED_DEBUG_PORT);
#else
WLED_GLOBAL int netDebugPrintPort _INIT(7868);
#endif
#else
#define DEBUGOUT Serial
#endif
#ifdef WLED_DEBUG
#ifndef ESP8266
#include <rom/rtc.h>
#endif
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#define DEBUG_PRINTF(x...) Serial.printf(x)
#define DEBUG_PRINT(x) DEBUGOUT.print(x)
#define DEBUG_PRINTLN(x) DEBUGOUT.println(x)
#define DEBUG_PRINTF(x...) DEBUGOUT.printf(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
@ -683,9 +701,9 @@ WLED_GLOBAL volatile uint8_t jsonBufferLock _INIT(0);
#endif
#ifdef WLED_DEBUG_FS
#define DEBUGFS_PRINT(x) Serial.print(x)
#define DEBUGFS_PRINTLN(x) Serial.println(x)
#define DEBUGFS_PRINTF(x...) Serial.printf(x)
#define DEBUGFS_PRINT(x) DEBUGOUT.print(x)
#define DEBUGFS_PRINTLN(x) DEBUGOUT.println(x)
#define DEBUGFS_PRINTF(x...) DEBUGOUT.printf(x)
#else
#define DEBUGFS_PRINT(x)
#define DEBUGFS_PRINTLN(x)

View File

@ -227,6 +227,7 @@
<ClCompile Include="wled_eeprom.cpp" />
<ClCompile Include="wled_server.cpp" />
<ClCompile Include="xml.cpp" />
<ClCompile Include="net_debug.cpp" />
</ItemGroup>
<PropertyGroup>
<DebuggerFlavor>VisualMicroDebugger</DebuggerFlavor>