Esp_log_set_vprintf от Arduino не работает на ESP32 - PullRequest
0 голосов
/ 28 февраля 2020

Использование ESP32.

Я пытаюсь перенаправить всю запись в файл SPIFFS. Я следую инструкциям, изложенным в документации Espressif , но по некоторым причинам функции ESP_LOG, похоже, не принимают перезаписанную функцию esp_log_set_vprintf.

Любая идея, что я могу делать неправильно?

#include <Arduino.h>
#include "SPIFFS.h"

static char log_print_buffer[512];
static char APP_NAME[] = "MyApp";
static char filePath[] = "/LOGS.txt";



void setup() {

    // Begin listening on baud rate CMD+SHIFT+P
    Serial.begin(9600);
    Serial.println("Listening on baud 9600");

    // Mount the file system
    if (!SPIFFS.begin(true)){
        Serial.println("An Error has occurred while mounting SPIFFS");
        return;
    }

    // Redirect all ESP_LOG functions to our custom function
    esp_log_set_vprintf(redirectToSpiffs);
    // 
    esp_log_level_set("*", ESP_LOG_VERBOSE);

    // Remove any preexisting log files we had written to
    SPIFFS.remove(filePath);

    // In theory, this should redirect to our function, but does not
    ESP_LOGI(APP_NAME, "Writing via ESP_LOGI!\n");

    // From the esp_log.h library
    /**
     * @brief Write message into the log
     *
     * This function is not intended to be used directly. Instead, use one of
     * ESP_LOGE, ESP_LOGW, ESP_LOGI, ESP_LOGD, ESP_LOGV macros.
     *
     * This function or these macros should not be used from an interrupt.
     */
    // This does work, and writes to our new SPIFFS function
    esp_log_write(ESP_LOG_VERBOSE, APP_NAME, "I can write like this all day.\n");


    Serial.println();
    Serial.println("Opening the log file and start reading the contents");
    Serial.println();
    File file = SPIFFS.open(filePath);

    if (!file){
        Serial.println("Failed to open file for reading");
        return;
    }

    while (file.available()){
        Serial.write(file.read());
    }


    file.close();

    Serial.println();
    Serial.println("End of file content");
}

void loop() {}


int redirectToSpiffs(const char *szFormat, va_list args) {
    //write evaluated format string into buffer
    int ret = vsnprintf(log_print_buffer, sizeof(log_print_buffer), szFormat, args);

    //output is now in buffer. write to file.
    if (ret >= 0){
        if (!SPIFFS.exists(filePath)){
            File writeLog = SPIFFS.open(filePath, FILE_WRITE);
            if (!writeLog){
                Serial.print("Couldn't open ");
                Serial.println(filePath);
            }
            delay(50);
            writeLog.close();
        }

        File spiffsLogFile = SPIFFS.open(filePath, 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;
}

Когда я открываю Serial Monitor, я получаю следующий вывод:

Listening on baud 9600
[I][v3-os.ino:31] setup(): Writing via ESP_LOGI!

[Writing to SPIFFS] I can write like this all day.

Opening the log file and start reading the contents

I can write like this all day.

End of file content

Похоже, что только esp_log_write(ESP_LOG_VERBOSE, APP_NAME, "I can write like this all day.\n"); фактически принимает переопределение функции.

...