From e65b1e3075f9ef87899eb73b073a35f36cc52cd0 Mon Sep 17 00:00:00 2001 From: bogdan Date: Mon, 8 May 2023 19:19:05 +0200 Subject: [PATCH] formated string file logging --- include/webguiapp.h | 2 +- src/WebGUIAppMain.c | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/include/webguiapp.h b/include/webguiapp.h index 5a0cf57..799971e 100644 --- a/include/webguiapp.h +++ b/include/webguiapp.h @@ -40,6 +40,6 @@ esp_err_t spi_device_polling_transmit_synchronized(spi_device_handle_t handle, s bool GetUserAppNeedReset(void); void SetUserAppNeedReset(bool res); -void LogFile(char *fname, char *mess); +void LogFile(char *fname, char *format, ...); #endif /* COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_ */ diff --git a/src/WebGUIAppMain.c b/src/WebGUIAppMain.c index d84864c..5d3bd87 100644 --- a/src/WebGUIAppMain.c +++ b/src/WebGUIAppMain.c @@ -510,9 +510,10 @@ void SetUserAppNeedReset(bool res) isUserAppNeedReset = res; } -void LogFile(char *fname, char *mess) +void LogFile(char *fname, char *format, ...) { char filename[32]; + char tstamp[16]; strcpy(filename, "/data/"); strcat(filename, fname); FILE *f = fopen(filename, "a"); @@ -521,10 +522,14 @@ void LogFile(char *fname, char *mess) ESP_LOGE(TAG, "Failed to open file %s for writing", filename); return; } - fwrite(esp_log_system_timestamp(), 1, 12, f); - fwrite(" ", 1, 1, f); - fwrite(mess, 1, strlen(mess), f); - fwrite("\r\n", 1, 2, f); + va_list arg; + va_start(arg, format); + va_end(arg); + strcpy(tstamp, "\r\n"); + strcat(tstamp, esp_log_system_timestamp()); + strcat(tstamp, " "); + fwrite(tstamp, 1, 15, f); + vfprintf(f, format, arg); fclose(f); ESP_LOGI(TAG, "File written to %s", filename); }