"esp_log_set_vprintf" does not work ?!

GeorgeFlorian1
Posts: 119
Joined: Thu Jan 31, 2019 2:32 pm

"esp_log_set_vprintf" does not work ?!

Postby GeorgeFlorian1 » 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.

Code: Select all

void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) __attribute__ ((format (printf, 3, 4)));
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:

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);
 
}
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.

ESP_igrr
Posts: 1550
Joined: Tue Dec 01, 2015 8:37 am

Re: "esp_log_set_vprintf" does not work ?!

Postby ESP_igrr » Mon Mar 18, 2019 12:34 pm

esp_log_write is normally invoked via ESP_LOGI (LOGE, LOGD, LOGW, LOGV) macro, which constructs the actual log format string. esp_log_write does not do anything with formatting by itself.

GeorgeFlorian1
Posts: 119
Joined: Thu Jan 31, 2019 2:32 pm

Re: "esp_log_set_vprintf" does not work ?!

Postby GeorgeFlorian1 » Mon Mar 18, 2019 12:48 pm

ESP_igrr wrote:
Mon Mar 18, 2019 12:34 pm
esp_log_write is normally invoked via ESP_LOGI (LOGE, LOGD, LOGW, LOGV) macro, which constructs the actual log format string. esp_log_write does not do anything with formatting by itself.
Oh, ok. So, basically, in my code, esp_log_write() acts just like a Serial.println(), right ?

How do I redirect the debug messages, instead of creating them myself ?

usmanf1
Posts: 1
Joined: Mon Nov 18, 2019 7:37 am

Re: "esp_log_set_vprintf" does not work ?!

Postby usmanf1 » Mon Nov 18, 2019 8:41 am

Hello,

Did you able to solve the issue? i also want to store log data in spiffs. But i could'n't get much help.

Who is online

Users browsing this forum: No registered users and 11 guests