"esp_log_set_vprintf" does not work ?!
Posted: Mon Mar 18, 2019 11:02 am
Hello !
I am currently trying to redirect the log messages to a SPIFFS file.
I have the function esp_log_write(ESP_LOG_DEBUG, "TAG", "text"); and it prints text instead of an actual debug message.
From its signature I thought that the const char* format is actually the format of the debug. So it would format it in text and not ASCII or binary, but the function actually prints text.
This is how far I've come:
As you can see I'm receiving GET requests from a URL every 10 seconds, meaning that there will be an issue regarding the size of the SPIFFS File. As such, I thought of using a circular buffer to read the last 5 lines of the file after the file reaches a certain size. This is just an idea. Any advice is welcomed !
Thank you !
I've also been taking a look at this: https://github.com/MalteJ/embedded-esp3 ... dp_logging
But the documentation is barely there and I don't understand how should it work. I also don't know python.
I am currently trying to redirect the log messages to a SPIFFS file.
I have the function esp_log_write(ESP_LOG_DEBUG, "TAG", "text"); and it prints text instead of an actual debug message.
Code: Select all
void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) __attribute__ ((format (printf, 3, 4)));
This is how far I've come:
Code: Select all
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ESPAsyncWebServer.h>
#include <stdarg.h>
#include <stdio.h>
#include <SPIFFS.h>
const char* ssid = "ssid";
const char* password = "password";
AsyncWebServer server(80);
static char log_print_buffer[512];
int vprintf_into_spiffs(const char* szFormat, va_list args) {
//write evaluated format string into buffer
int ret = vsnprintf (log_print_buffer, sizeof(log_print_buffer), szFormat, args);
Serial.println(log_print_buffer);
//output is now in buffer. write to file.
if(ret >= 0) {
if(!SPIFFS.exists("/LOGS.txt")) {
File writeLog = SPIFFS.open("/LOGS.txt", FILE_WRITE);
if(!writeLog) Serial.println("Couldn't open spiffs_log.txt");
delay(50);
writeLog.close();
}
File spiffsLogFile = SPIFFS.open("/LOGS.txt", FILE_APPEND);
//debug output
//printf("[Writing to SPIFFS] %.*s", ret, log_print_buffer);
spiffsLogFile.write((uint8_t*) log_print_buffer, (size_t) ret);
//to be safe in case of crashes: flush the output
spiffsLogFile.flush();
spiffsLogFile.close();
}
return ret;
}
void setup() {
Serial.begin(115200);
delay(1000);
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
esp_log_set_vprintf(&vprintf_into_spiffs);
//install new logging function
esp_log_level_set("TAG", ESP_LOG_DEBUG);
//write into log
esp_log_write(ESP_LOG_DEBUG, "TAG", "text");
delay(1000);
WiFi.begin(ssid, password);
delay(1000);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
Serial.println(WiFi.localIP());
server.on("/events", HTTP_GET, [](AsyncWebServerRequest *request){
if(SPIFFS.exists("/LOGS.txt")) {
request->send(SPIFFS, "/LOGS.txt", "text/plain");
} else {
request->send(200, "text/plain", "LOGS not found ! Restarting in 5 seconds..");
delay(5000);
ESP.restart();
}
});
server.begin();
File file2 = SPIFFS.open("/LOGS.txt");
if(!file2){
Serial.println("Failed to open file for reading");
return;
}
Serial.println("File Content:");
while(file2.available()){
Serial.write(file2.read());
}
file2.close();
Serial.println("End of file content");
}
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);
}
Thank you !
I've also been taking a look at this: https://github.com/MalteJ/embedded-esp3 ... dp_logging
But the documentation is barely there and I don't understand how should it work. I also don't know python.