Hello !
Do you think it’s possible to output logs like ESP_LOGV("TAG", "Verbose");
to a SPIFFS file that’s stored in the ESP’s flash memory ?
I would like to display log messages to a webpage hosted on the ESP32.
And I want to do that by writing them first in a SPIFFS file.
I was thinking of some sort of function that works using the first in, last out principle after 3 chunks of logs.
1 chunk of log would be comprised of a number of logs resulted from one passing through the void loop() function, for example.
The following lines of code enable debug messages to be outputted on the Serial Monitor.
esp_log_level_set("*", ESP_LOG_VERBOSE);
ESP_LOGE("TAG", "Error");
ESP_LOGW("TAG", "Warning");
// ESP_LOGI("TAG", "Info");
ESP_LOGD("TAG", "Debug");
ESP_LOGV("TAG", "Verbose");
The following line is inside platformio.ini :
build_flags = -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
Let’s say I have a html page events.html and a HTTP GET with a URL inside the void loop(). For example, I would like to check logs for the HTTP GET request. Or let’s say that the ESP crashes out of nowhere and I would like to access some logs and see what went wrong.
#include <Arduino.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
AsyncWebServer server(80);
void setup() {
Serial.begin(115200);
delay(2000);
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
File events = SPIFFS.open("/events.txt", FILE_WRITE);
if(!events) Serial.println("Couldn't create /events.txt");
WiFi.begin("mySSID","password");
server.on("/events", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/events.html", "text/html");
});
.
.
.
events.close();
}
void loop() {
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
HTTPClient http;
http.begin("http://jsonplaceholder.typicode.com/comments?id=10"); //Specify the URL
int httpCode = http.GET(); //Make the request
if (httpCode > 0) { //Check for the returning code
Serial.println();
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
Serial.println();
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end(); //Free the resources
}
delay(10000);
}
I am guessing that the answer has something to do with the following functions:
/**
* @brief Set function used to output log entries
*
* By default, log output goes to UART0. This function can be used to redirect log
* output to some other destination, such as file or network. Returns the original
* log handler, which may be necessary to return output to the previous destination.
*
* @param func new Function used for output. Must have same signature as vprintf.
*
* @return func old Function used for output.
*/
vprintf_like_t esp_log_set_vprintf(vprintf_like_t func);
/**
* @brief Function which returns timestamp to be used in log output
*
* This function is used in expansion of ESP_LOGx macros.
* In the 2nd stage bootloader, and at early application startup stage
* this function uses CPU cycle counter as time source. Later when
* FreeRTOS scheduler start running, it switches to FreeRTOS tick count.
*
* For now, we ignore millisecond counter overflow.
*
* @return timestamp, in milliseconds
*/
uint32_t esp_log_timestamp(void);
I’ve been reading here but it’s still ambiguous to me.
I haven’t had any experience with logs and I am still reading about it. I thought that by posting here I will have a bigger change of someone guiding me towards my answer.
Thank you !