-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WebSocket debug logging * Fix static analysis issues. TIL about [[maybe_unused]] * Fix linker errors on ESP32
- Loading branch information
1 parent
d9f58aa
commit 9819622
Showing
4 changed files
with
135 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ framework = arduino | |
lib_deps = | ||
bblanchon/ArduinoJson @ ^6.21.3 | ||
knolleary/PubSubClient @ ^2.8 | ||
me-no-dev/ESP Async [email protected] | ||
https://github.com/SwiCago/HeatPump | ||
check_tool = clangtidy, cppcheck | ||
check_flags = | ||
|
@@ -45,12 +46,25 @@ monitor_speed = 115200 | |
upload_speed = 460800 | ||
board_build.ldscript = eagle.flash.4m2m.ld | ||
|
||
[env:WEMOS_D1_Mini_Testing] | ||
platform = ${env:WEMOS_D1_Mini.platform} | ||
board = ${env:WEMOS_D1_Mini.board} | ||
monitor_speed = ${env:WEMOS_D1_Mini.monitor_speed} | ||
upload_speed = ${env:WEMOS_D1_Mini.upload_speed} | ||
board_build.ldscript = ${env:WEMOS_D1_Mini.board_build.ldscript} | ||
build_flags = ${env:WEMOS_D1_Mini.build_flags} -D ENABLE_LOGGING | ||
|
||
|
||
[env:ESP32DEV] | ||
platform = espressif32 | ||
board = esp32dev | ||
lib_deps = | ||
ArduinoJson @6.20.0 | ||
PubSubClient @2.8 | ||
me-no-dev/[email protected] | ||
; latest master required for ESP32 because released version causes linker failures | ||
; https://github.com/me-no-dev/ESPAsyncWebServer/pull/999 | ||
https://github.com/me-no-dev/ESPAsyncWebServer.git | ||
https://github.com/SwiCago/HeatPump | ||
build_unflags = -std=gnu++11 | ||
build_flags = -std=gnu++17 -D CORE_DEBUG_LEVEL=0 -Wall | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "logger.hpp" | ||
|
||
#include <ESPAsyncWebServer.h> | ||
|
||
static AsyncWebServer server(81); | ||
static AsyncWebSocket webSocket("/log"); | ||
|
||
const auto LOG_BUFFER_SIZE = 256; | ||
|
||
void onEvent(const AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, | ||
[[maybe_unused]] void *arg, [[maybe_unused]] uint8_t *data, | ||
[[maybe_unused]] size_t len) { | ||
if (type == WS_EVT_CONNECT) { | ||
client->ping(); | ||
} else if (type == WS_EVT_PONG) { | ||
Logger::log(F("log client connected ws://%s client ID:%u\n"), server->url(), client->id()); | ||
} | ||
} | ||
|
||
// cppcheck-suppress unusedFunction | ||
void Logger::initialize() { | ||
webSocket.onEvent(onEvent); | ||
server.addHandler(&webSocket); | ||
server.begin(); | ||
} | ||
|
||
void Logger::log(const char *format, ...) { | ||
char logBuffer[LOG_BUFFER_SIZE]; | ||
va_list args; | ||
va_start(args, format); | ||
vsnprintf(logBuffer, LOG_BUFFER_SIZE, format, args); | ||
webSocket.printfAll(logBuffer); | ||
va_end(args); | ||
} | ||
|
||
void Logger::log(const __FlashStringHelper *format, ...) { | ||
char logBuffer[LOG_BUFFER_SIZE]; | ||
va_list args; | ||
va_start(args, format); | ||
// See https://arduino-esp8266.readthedocs.io/en/latest/PROGMEM.html for more about PGM_P, PSTR, | ||
// F, FlashStringHelper, etc. | ||
vsnprintf_P(logBuffer, LOG_BUFFER_SIZE, reinterpret_cast<PGM_P>(format), args); | ||
webSocket.printfAll(logBuffer); | ||
va_end(args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef LOGGER_HPP | ||
#define LOGGER_HPP | ||
|
||
#include <Arduino.h> | ||
|
||
namespace Logger { | ||
void initialize(); | ||
void log(const char* format, ...) __attribute__((format(printf, 1, 2))); | ||
void log(const __FlashStringHelper* format, ...); | ||
inline void log(const String& message) { return log(message.c_str()); } | ||
} // namespace Logger | ||
|
||
#endif // LOGGER_HPP |
Oops, something went wrong.