Skip to content

Commit

Permalink
Merge pull request #1018 from bdring/PreserveStartupLog
Browse files Browse the repository at this point in the history
Store startup log in RTC DRAM
  • Loading branch information
MitchBradley authored Sep 12, 2023
2 parents bfdac37 + 9304839 commit c7c1a85
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
52 changes: 52 additions & 0 deletions FluidNC/esp32/StartupLog.cpp
Original file line number Diff line number Diff line change
@@ -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 <sstream>

// 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;
2 changes: 2 additions & 0 deletions FluidNC/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion FluidNC/src/ProcessSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
21 changes: 0 additions & 21 deletions FluidNC/src/StartupLog.cpp

This file was deleted.

13 changes: 5 additions & 8 deletions FluidNC/src/StartupLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
#include "Config.h"

#include "Channel.h"
#include <queue>

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;

0 comments on commit c7c1a85

Please sign in to comment.