[SOLVED] espsavecrash: print to string or similar variable

I have an ESP8266 device and want to use this to save crash reports to EEPROM. This is important as my device is remote, and not easily accessible. The remote example sketch involves using client.print() however I am using server.on() for all my purposes.

How the library example does things:

       client.print("HTTP/1.1 200 OK\r\n");
          client.print("Content-Type: text/plain\r\n");
          client.print("Connection: close\r\n");
          client.print("\r\n");
          // send crash information to the web browser
          SaveCrash.print(client);

Where SaveCrash gets info from the EEPROM and prints it via printf and such:

void EspSaveCrash::print(Print& outputDev)
{
  // Note that 'EEPROM.begin' method is reserving a RAM buffer
  // The buffer size is SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_SPACE_SIZE
  EEPROM.begin(_offset + _size);
  byte crashCounter = EEPROM.read(_offset + SAVE_CRASH_COUNTER);
  if (crashCounter == 0)
  {
    outputDev.println("No crashes saved");
    return;
  }

I'd like to avoid modifying the library, and was wondering if there was a way to print to a string?

So I can utilize it in the following manner (I know this won't work, just pseudo-code):

server.on("/get_crash_report", HTTP_GET, []() {
    String outputDev = "";
    SaveCrash.print(outputDev)
    server.send(200, "text/html", outputDev);
  });

I ended up changing up a bit in the lib (attached).

Notably:

extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack, uint32_t stack_end )
{
 // define a class to print to string
 class PrintToString : public Print {
 public:
 String str = "";
 PrintToString(){};
 virtual size_t write(const uint8_t character){str+=character;};
 virtual size_t write(const uint8_t *buffer, size_t size){
 String str2 = String((const char *)buffer);
 str2.remove(size);
 str += str2;
 };
 } printToString;
 // save to spiffs
 getStackTrace(rst_info, stack, stack_end, printToString);
 String fn;
 if (staticfn) {
 fn = String(staticfn);
 } else {
 fn = String(F(CRASHFILEPATH));
 }
 File f = SPIFFS.open(fn, "a");
 if (!f) { 
 f = SPIFFS.open(fn, "w"); 
 }
 if (f) {
 unsigned int w = f.write((uint8_t*)printToString.str.c_str(), printToString.str.length());
 f.close();
 }
}

For converting the print to a string.

Also, here is another way of doing it: crash log can be stored to char buffer by brainelectronics · Pull Request #9 · krzychb/EspSaveCrash · GitHub

EspSaveCrashSpiffs.cpp (2.43 KB)

EspSaveCrashSpiffs.h (719 Bytes)