Skip to content

Commit

Permalink
WebSocket debug logging (#22)
Browse files Browse the repository at this point in the history
* WebSocket debug logging

* Fix static analysis issues. TIL about [[maybe_unused]]

* Fix linker errors on ESP32
  • Loading branch information
floatplane authored Jan 25, 2024
1 parent d9f58aa commit 9819622
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 48 deletions.
14 changes: 14 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions src/mitsubishi2mqtt/logger.cpp
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);
}
13 changes: 13 additions & 0 deletions src/mitsubishi2mqtt/logger.hpp
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
Loading

0 comments on commit 9819622

Please sign in to comment.