From 21ce6d587023c077659ed7c49c0b5af9b02181c1 Mon Sep 17 00:00:00 2001 From: Martin Bauer Date: Fri, 8 Sep 2023 10:26:49 +0200 Subject: [PATCH] Log streaming over websocket activated --- firmware/lib/logging/Logger.h | 5 +++-- firmware/platformio.ini | 3 ++- firmware/src/LoggingAPI.h | 31 +++++++++++++++++-------------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/firmware/lib/logging/Logger.h b/firmware/lib/logging/Logger.h index dc57ce7..d88a600 100644 --- a/firmware/lib/logging/Logger.h +++ b/firmware/lib/logging/Logger.h @@ -64,6 +64,7 @@ public: } else return false; + return true; } static Logger *getInstance(); @@ -107,6 +108,6 @@ private: Logger(); char *data_; - size_t totalSize_; - size_t currentSize_; + size_t totalSize_ = 0; + size_t currentSize_ = 0; }; diff --git a/firmware/platformio.ini b/firmware/platformio.ini index 3b95b30..c52de07 100644 --- a/firmware/platformio.ini +++ b/firmware/platformio.ini @@ -42,4 +42,5 @@ platform = native test_ignore = test_embedded build_flags = -g -DPLATFORM_NATIVE -std=c++17 -O0 build_src_filter = +<*> - - - -lib_compat_mode = off \ No newline at end of file +lib_compat_mode = off + diff --git a/firmware/src/LoggingAPI.h b/firmware/src/LoggingAPI.h index b121f9d..268f368 100644 --- a/firmware/src/LoggingAPI.h +++ b/firmware/src/LoggingAPI.h @@ -16,6 +16,11 @@ class LoggingAPI switch(code) { case MessageCode::LOG_STREAMING_START: running_ = true; + if(firstCall_) { + const Logger * logger = Logger::getInstance(); + lastEnd_ = logger->begin(); + firstCall_ = false; + } return true; case MessageCode::LOG_STREAMING_STOP: running_ = false; @@ -28,27 +33,25 @@ class LoggingAPI template void iteration(TServer &server) { - const Logger * logger = Logger::getInstance(); - if(running_) { - StaticJsonDocument<1024> data; - for (int i = 0; i < MAX_WEBSOCKET_CONNECTIONS; ++i) - { - auto &c = server.client(i); - if (c.available()) - { - - } + const Logger * logger = Logger::getInstance(); + const auto beginIt = lastEnd_; + const auto endIt = logger->end(); + + StaticJsonDocument<512> data; + for(auto it = beginIt; it != endIt; ++beginIt) { + data["time"] = it.time_millis(); + data["msg"] = it.message(); + server.template sendToAll<512>(MessageCode::LOG_UPDATE, data); } + lastEnd_ = endIt; } - - - lastOffset_ = logger->currentSize(); } private: bool running_ = false; - size_t lastOffset_ = 0; + Logger::iterator lastEnd_; + bool firstCall_ = false; };