From 93048392bf1996354a65de82d519b6aea86482bb Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Wed, 6 Sep 2023 19:17:34 -1000 Subject: [PATCH] Store startup log in RTC DRAM ... so it can be preserved across panic resets --- FluidNC/esp32/StartupLog.cpp | 52 +++++++++++++++++++++++++++++++++ FluidNC/src/Main.cpp | 2 ++ FluidNC/src/ProcessSettings.cpp | 2 +- FluidNC/src/StartupLog.cpp | 21 ------------- FluidNC/src/StartupLog.h | 13 ++++----- 5 files changed, 60 insertions(+), 30 deletions(-) create mode 100644 FluidNC/esp32/StartupLog.cpp delete mode 100644 FluidNC/src/StartupLog.cpp diff --git a/FluidNC/esp32/StartupLog.cpp b/FluidNC/esp32/StartupLog.cpp new file mode 100644 index 000000000..034e10d2b --- /dev/null +++ b/FluidNC/esp32/StartupLog.cpp @@ -0,0 +1,52 @@ +// Copyright (c) 2023 - Mitch Bradley +// Use of this source code is governed by a GPLv3 license that can be found in the LICENSE file. + +#include "src/StartupLog.h" +#include "src/Protocol.h" // send_line() +#include + +// The startup log is stored in RTC RAM that is preserved across +// resets. That lets us show the previous startup log if the +// system panics and resets. + +// The size is limited by the size of RTC RAM minus system usage thereof +static const size_t _maxlen = 7000; +static RTC_NOINIT_ATTR char _messages[_maxlen]; +static RTC_NOINIT_ATTR size_t _len; +static bool _paniced; + +void StartupLog::init() { + if (esp_reset_reason() == ESP_RST_PANIC) { + _paniced = true; + } else { + _paniced = false; + _len = 0; + } +} +size_t StartupLog::write(uint8_t data) { + if (_paniced || _len >= _maxlen) { + return 0; + } + _messages[_len++] = (char)data; + return 1; +} +void StartupLog::dump(Channel& out) { + if (_paniced) { + log_error_to(out, "Showing startup log from previous panic"); + } + for (size_t i = 0; i < _len;) { + std::string line; + while (i < _len) { + char c = _messages[i++]; + if (c == '\n') { + break; + } + line += c; + } + log_to(out, line); + } +} + +StartupLog::~StartupLog() {} + +StartupLog startupLog; diff --git a/FluidNC/src/Main.cpp b/FluidNC/src/Main.cpp index 6da6ed47e..aed6a8608 100644 --- a/FluidNC/src/Main.cpp +++ b/FluidNC/src/Main.cpp @@ -34,6 +34,8 @@ void setup() { uartInit(); // Setup serial port Uart0.println(); // create some white space after ESP32 boot info + StartupLog::init(); + // Setup input polling loop after loading the configuration, // because the polling may depend on the config allChannels.init(); diff --git a/FluidNC/src/ProcessSettings.cpp b/FluidNC/src/ProcessSettings.cpp index 467556244..4c2a29dab 100644 --- a/FluidNC/src/ProcessSettings.cpp +++ b/FluidNC/src/ProcessSettings.cpp @@ -705,7 +705,7 @@ static Error showChannelInfo(const char* value, WebUI::AuthenticationLevel auth_ } static Error showStartupLog(const char* value, WebUI::AuthenticationLevel auth_level, Channel& out) { - startupLog.dump(out); + StartupLog::dump(out); return Error::Ok; } diff --git a/FluidNC/src/StartupLog.cpp b/FluidNC/src/StartupLog.cpp deleted file mode 100644 index 0e36e71fb..000000000 --- a/FluidNC/src/StartupLog.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "StartupLog.h" -#include -#include "Protocol.h" // send_line() - -size_t StartupLog::write(uint8_t data) { - _messages += (char)data; - return 1; -} -std::string StartupLog::messages() { - return _messages; -} -void StartupLog::dump(Channel& out) { - std::istringstream iss(_messages); - for (std::string line; std::getline(iss, line);) { - log_to(out, line); - } -} - -StartupLog::~StartupLog() {} - -StartupLog startupLog("Startup Log"); diff --git a/FluidNC/src/StartupLog.h b/FluidNC/src/StartupLog.h index 51176a2e8..fd678f127 100644 --- a/FluidNC/src/StartupLog.h +++ b/FluidNC/src/StartupLog.h @@ -6,19 +6,16 @@ #include "Config.h" #include "Channel.h" -#include class StartupLog : public Channel { -private: - std::string _messages; - public: - StartupLog(const char* name) : Channel(name) {} + StartupLog() : Channel("Startup Log") {} virtual ~StartupLog(); - size_t write(uint8_t data) override; - std::string messages(); - void dump(Channel& channel); + size_t write(uint8_t data) override; + + static void init(); + static void dump(Channel& channel); }; extern StartupLog startupLog;