Hey, I use ESP32 with arduino framework. For remote debugging, I want to store system logs like errors or warnings to the SD card together with my own log statements. Therefore, I created a callback function, which should be called when ESP_LOGE
or ESP_LOGW
is used, by exploiting esp_log_set_vprintf(sdCardLogOutput)
. However, the callback function is never called and only the ESP_LOGE
is printed to the terminal via UART.
This is my code example:
#include <Arduino.h>
#include "FS.h"
#include <SD.h>
#include "SPI.h"
#include "esp_log.h"
#define TAG "myApp"
#define LOG_LEVEL ESP_LOG_INFO
#define MY_ESP_LOG_LEVEL ESP_LOG_WARN
File logFile;
int sdCardLogOutput(const char *format, va_list args)
{
Serial.println("Callback running");
char buf[128];
int ret = vsnprintf(buf, sizeof(buf), format, args);
if (logFile)
{
logFile.println(buf);
logFile.flush();
}
return ret;
}
void setup()
{
Serial.begin(9600);
delay(1000);
while (!Serial)
{
}
if (SD.begin())
{
Serial.println("Successfully initialized SD card");
}
else
{
Serial.println("SD card failed");
}
logFile = SD.open("/log.txt", FILE_WRITE, true);
if (!logFile)
{
Serial.println("Failed to open log file");
}
else
{
Serial.println("Setting log levels and callback");
esp_log_level_set("*", MY_ESP_LOG_LEVEL);
esp_log_level_set(TAG, LOG_LEVEL);
esp_log_set_vprintf(sdCardLogOutput);
}
}
void loop()
{
ESP_LOGE(TAG, "Error message");
ESP_LOGW(TAG, "Warning message");
delay(1000);
}
Besides, this is my platformIO.ini file:
[env:firebeetle32]
platform = espressif32
board = firebeetle32
framework = arduino
build_flags = -DUSE_ESP32_LOG
Any idea why
-
My callback is never invoked
-
Only the error message is printed but not the warning message although my log levels should allow for this