From 15055fa5099aed55ea249c0236e5759f1fc54ac6 Mon Sep 17 00:00:00 2001 From: Shaheen Gandhi Date: Tue, 24 Aug 2021 22:12:03 -0700 Subject: [PATCH 1/2] Add network debug printer --- platformio.ini | 2 +- wled00/cfg.cpp | 7 + wled00/data/settings_sec.htm | 4 + wled00/html_settings.h | 5 +- wled00/html_ui.h | 1576 +++++++++++++++++----------------- wled00/net_debug.cpp | 75 ++ wled00/net_debug.h | 25 + wled00/set.cpp | 12 + wled00/wled.h | 28 +- wled00/wled00.vcxproj | 1 + wled00/xml.cpp | 5 + 11 files changed, 946 insertions(+), 794 deletions(-) create mode 100644 wled00/net_debug.cpp create mode 100644 wled00/net_debug.h diff --git a/platformio.ini b/platformio.ini index 5939e592..a0d33d6b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -284,7 +284,7 @@ lib_deps = ${esp8266.lib_deps} board = esp32dev platform = espressif32@2.0 build_unflags = ${common.build_unflags} -build_flags = ${common.build_flags_esp32} -D WLED_RELEASE_NAME=ESP32 #-D WLED_DISABLE_BROWNOUT_DET +build_flags = ${common.build_flags_esp32} -D WLED_RELEASE_NAME=ESP32 -D WLED_DEBUG_NET #-D WLED_DISABLE_BROWNOUT_DET lib_deps = ${esp32.lib_deps} [env:esp32_eth] diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 6d86b6ea..6ee85ea3 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -404,6 +404,13 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { } #endif + #ifdef WLED_DEBUG_NET + JsonObject netDebugPrint = doc["netdebug"]; + CJSON(netDebugPrintEnabled, netDebugPrint[F("enabled")]); + getStringFromJson(netDebugPrintHost, netDebugPrint[F("host")], 33); + CJSON(netDebugPrintPort, netDebugPrint[F("port")]); + #endif + DEBUG_PRINTLN(F("Starting usermod config.")); JsonObject usermods_settings = doc["um"]; if (!usermods_settings.isNull()) { diff --git a/wled00/data/settings_sec.htm b/wled00/data/settings_sec.htm index 27f36f12..212e71fb 100644 --- a/wled00/data/settings_sec.htm +++ b/wled00/data/settings_sec.htm @@ -81,6 +81,10 @@
⚠ Restoring presets/configuration will OVERWRITE your current presets/configuration.
Incorrect configuration may require a factory reset or re-flashing of your ESP.
For security reasons, passwords are not backed up. +

Network Debug Logging

+ Log to network:
+ Network logging host:
+ Network logging port:

About

WLED version ##VERSION##

Contributors, dependencies and special thanks
diff --git a/wled00/html_settings.h b/wled00/html_settings.h index 51e331d9..e4a343e8 100644 --- a/wled00/html_settings.h +++ b/wled00/html_settings.h @@ -404,7 +404,10 @@ onclick='uploadFile(d.Sf.data2,"/cfg.json")'>
⚠ Restoring presets/configuration will OVERWRITE your current presets/configuration.
Incorrect configuration may require a factory reset or re-flashing of your ESP. -
For security reasons, passwords are not backed up.

About

For security reasons, passwords are not backed up.

+Network Debug Logging

Log to network:
+Network logging host:
Network logging port: +

About

WLED version 0.13.0-b2

(s), strlen(s)); + debugUdp.endPacket(); +} + +void NetworkDebugPrinter::print(const __FlashStringHelper* s) { + print(reinterpret_cast(s)); +} + +void NetworkDebugPrinter::print(String s) { + print(s.c_str()); +} + +void NetworkDebugPrinter::print(unsigned int n) { + char s[10]; + snprintf(s, sizeof(s), "%d", n); + s[9] = '\0'; + print(s); +} + +void NetworkDebugPrinter::println() { + print("\n"); +} + +void NetworkDebugPrinter::println(const char *s) { + print(s); + print("\n"); +} + +void NetworkDebugPrinter::println(const __FlashStringHelper* s) { + print(s); + print("\n"); +} + +void NetworkDebugPrinter::println(String s) { + print(s); + print("\n"); +} + +void NetworkDebugPrinter::println(unsigned int n) { + print(n); + print("\n"); +} + +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 diff --git a/wled00/net_debug.h b/wled00/net_debug.h new file mode 100644 index 00000000..19dad41a --- /dev/null +++ b/wled00/net_debug.h @@ -0,0 +1,25 @@ +#ifndef WLED_NET_DEBUG_H +#define WLED_NET_DEBUG_H + +#include +#include + +class NetworkDebugPrinter { +private: + WiFiUDP debugUdp; +public: + void print(const char *s); + void print(const __FlashStringHelper* s); + void print(String s); + void print(unsigned int n); + 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 \ No newline at end of file diff --git a/wled00/set.cpp b/wled00/set.cpp index 37004dd0..495f9c89 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -387,6 +387,18 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) wifiLock = request->hasArg(F("OW")); aOtaEnabled = request->hasArg(F("AO")); } + + #ifdef WLED_DEBUG_NET + netDebugPrintEnabled = request->hasArg(F("NDE")); + if (request->hasArg(F("NDH"))) + { + strlcpy(netDebugPrintHost, request->arg(F("NDH")).c_str(), 33); + } + if (request->hasArg(F("NDP"))) + { + netDebugPrintPort = request->arg(F("NDP")).toInt(); + } + #endif } #ifdef WLED_ENABLE_DMX // include only if DMX is enabled if (subPage == 7) diff --git a/wled00/wled.h b/wled00/wled.h index fc390ae2..864cd936 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -80,6 +80,10 @@ #include "my_config.h" #endif +#ifdef WLED_DEBUG_NET +#include "net_debug.h" +#endif + #include #include #include @@ -599,17 +603,21 @@ WLED_GLOBAL UsermodManager usermods _INIT(UsermodManager()); #endif // enable additional debug output + +#define DEBUG_PRINT(x) +#define DEBUG_PRINTLN(x) +#define DEBUG_PRINTF(x...) + #ifdef WLED_DEBUG #ifndef ESP8266 #include #endif + #undef DEBUG_PRINT + #undef DEBUG_PRINTLN + #undef DEBUG_PRINTF #define DEBUG_PRINT(x) Serial.print(x) #define DEBUG_PRINTLN(x) Serial.println(x) #define DEBUG_PRINTF(x...) Serial.printf(x) -#else - #define DEBUG_PRINT(x) - #define DEBUG_PRINTLN(x) - #define DEBUG_PRINTF(x...) #endif #ifdef WLED_DEBUG_FS @@ -622,6 +630,18 @@ WLED_GLOBAL UsermodManager usermods _INIT(UsermodManager()); #define DEBUGFS_PRINTF(x...) #endif +#ifdef WLED_DEBUG_NET + #undef DEBUG_PRINT + #undef DEBUG_PRINTLN + #undef DEBUG_PRINTF + #define DEBUG_PRINT(x) NetDebug.print(x) + #define DEBUG_PRINTLN(x) NetDebug.println(x) + #define DEBUG_PRINTF(x...) NetDebug.printf(x) + WLED_GLOBAL bool netDebugPrintEnabled _INIT(false); + WLED_GLOBAL char netDebugPrintHost[33] _INIT(""); + WLED_GLOBAL int netDebugPrintPort _INIT(7868); +#endif + // debug macro variable definitions #ifdef WLED_DEBUG WLED_GLOBAL unsigned long debugTime _INIT(0); diff --git a/wled00/wled00.vcxproj b/wled00/wled00.vcxproj index e095b8f7..e372a4e8 100644 --- a/wled00/wled00.vcxproj +++ b/wled00/wled00.vcxproj @@ -227,6 +227,7 @@ + VisualMicroDebugger diff --git a/wled00/xml.cpp b/wled00/xml.cpp index 45320606..0e6e5298 100644 --- a/wled00/xml.cpp +++ b/wled00/xml.cpp @@ -544,6 +544,11 @@ void getSettingsJS(byte subPage, char* dest) oappend(SET_F(" (build ")); oappendi(VERSION); oappend(SET_F(")\";")); +#ifdef WLED_DEBUG_NET + sappend('c',SET_F("NDE"),netDebugPrintEnabled); + sappends('s',SET_F("NDH"),netDebugPrintHost); + sappend('v',SET_F("NDP"),netDebugPrintPort); +#endif } #ifdef WLED_ENABLE_DMX // include only if DMX is enabled From 1d8c9ac020bfb951129037692625a93b58bff035 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Sun, 6 Nov 2022 16:50:12 +0100 Subject: [PATCH 2/2] Net debug optimizations Fix ESP8266 (unaligned progmem flash string reads) Do not send an extra package for \n in println Only resolve IP/hostname once --- wled00/net_debug.cpp | 40 +++++++++++++++++----------------------- wled00/net_debug.h | 9 ++++----- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/wled00/net_debug.cpp b/wled00/net_debug.cpp index f1242bb1..9f451ca5 100644 --- a/wled00/net_debug.cpp +++ b/wled00/net_debug.cpp @@ -4,16 +4,10 @@ NetworkDebugPrinter NetDebug; -void NetworkDebugPrinter::printchar(char s) { - debugUdp.write(s); -} +void NetworkDebugPrinter::print(const char *s, bool newline) { + if (!WLED_CONNECTED || !udpConnected || s == nullptr) return; -void NetworkDebugPrinter::print(const char *s) { - if (!WLED_CONNECTED || s == nullptr) { - return; - } - IPAddress debugPrintHostIP; - if (!debugPrintHostIP.fromString(netDebugPrintHost)) { + if (!debugPrintHostIP && !debugPrintHostIP.fromString(netDebugPrintHost)) { #ifdef ESP8266 WiFi.hostByName(netDebugPrintHost, debugPrintHostIP, 750); #else @@ -24,49 +18,49 @@ void NetworkDebugPrinter::print(const char *s) { #endif #endif } + + WiFiUDP debugUdp; debugUdp.beginPacket(debugPrintHostIP, netDebugPrintPort); - //for (size_t i=0; i(s), strlen(s)); + if (newline) debugUdp.write('\n'); debugUdp.endPacket(); } -void NetworkDebugPrinter::print(const __FlashStringHelper* s) { - print(reinterpret_cast(s)); +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) { +void NetworkDebugPrinter::print(unsigned int n, bool newline) { char s[10]; snprintf_P(s, sizeof(s), PSTR("%d"), n); s[9] = '\0'; - print(s); + print(s, newline); } void NetworkDebugPrinter::println() { - print("\n"); + print("", true); } void NetworkDebugPrinter::println(const char *s) { - print(s); - print("\n"); + print(s, true); } void NetworkDebugPrinter::println(const __FlashStringHelper* s) { - print(s); - print("\n"); + print(s, true); } void NetworkDebugPrinter::println(String s) { - print(s); - print("\n"); + print(s.c_str(), true); } void NetworkDebugPrinter::println(unsigned int n) { - print(n); - print("\n"); + print(n, true); } void NetworkDebugPrinter::printf(const char *fmt...) { diff --git a/wled00/net_debug.h b/wled00/net_debug.h index d15a9f76..1d9aa6c2 100644 --- a/wled00/net_debug.h +++ b/wled00/net_debug.h @@ -6,13 +6,12 @@ class NetworkDebugPrinter { private: - WiFiUDP debugUdp; - void printchar(char c); + IPAddress debugPrintHostIP; public: - void print(const char *s); - void print(const __FlashStringHelper* s); + 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); + void print(unsigned int n, bool newline = false); void println(); void println(const char *s); void println(const __FlashStringHelper* s);