From 26e104afe58c0252b0de3522b785882bb82967b3 Mon Sep 17 00:00:00 2001 From: bogdan Date: Sun, 7 Jul 2024 13:22:46 +0200 Subject: [PATCH] implemented system log with file rotation --- include/webguiapp.h | 1 + src/SysConfiguration.c | 67 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/include/webguiapp.h b/include/webguiapp.h index 0ce31fc..6717c47 100644 --- a/include/webguiapp.h +++ b/include/webguiapp.h @@ -42,5 +42,6 @@ void SetAppVars( rest_var_t* appvars, int size); bool GetUserAppNeedReset(void); void SetUserAppNeedReset(bool res); void LogFile(char *fname, char *format, ...); +void SysLog(char *format, ...); #endif /* COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_ */ diff --git a/src/SysConfiguration.c b/src/SysConfiguration.c index 7ca872d..1688809 100644 --- a/src/SysConfiguration.c +++ b/src/SysConfiguration.c @@ -109,7 +109,7 @@ esp_err_t WebGuiAppInit(void) if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND || MANUAL_RESET == 1 - #if (MAIN_FUNCTIONAL_BUTTON_GPIO >= 0) + #if (MAIN_FUNCTIONAL_BUTTON_GPIO >= 0) || gpio_get_level(MAIN_FUNCTIONAL_BUTTON_GPIO) == 0 #endif ) @@ -592,6 +592,71 @@ void SetUserAppNeedReset(bool res) isUserAppNeedReset = res; } +#define LOG_MAX_CHUNK_SIZE 10 +#define LOG_MAX_CHUNKS 4 +#define DEFAULT_LOG_FILE_NAME "syslog" + +static void ComposeLogFilename(int chunk, char *filename) +{ + char chunkstr[2]; + strcpy(filename, "/data/"); + strcat(filename, DEFAULT_LOG_FILE_NAME); + itoa(chunk, chunkstr, 10); + strcat(filename, chunkstr); + strcat(filename, ".log"); +} + +void SysLog(char *format, ...) +{ + char tstamp[ISO8601_TIMESTAMP_LENGTH + 2]; + static int cur_chunk = 0, isstart = 1; + char filename[32]; + struct stat file_stat; + FILE *f; + ComposeLogFilename(cur_chunk, filename); + + //If first call after reboot, try to find not full chunk + if (isstart) + { + while (file_stat.st_size > LOG_MAX_CHUNK_SIZE * 1024 && cur_chunk <= LOG_MAX_CHUNKS) + { + cur_chunk++; + ComposeLogFilename(cur_chunk, filename); + } + isstart = 0; + } + + + stat(filename, &file_stat); + if (file_stat.st_size > LOG_MAX_CHUNK_SIZE * 1024) + { + if (++cur_chunk > LOG_MAX_CHUNKS) + cur_chunk = 0; + ComposeLogFilename(cur_chunk, filename); + f = fopen(filename, "w"); + } + else + f = fopen(filename, "a"); + + if (f == NULL) + { + ESP_LOGE(TAG, "Failed to open file %s for writing", filename); + return; + } + va_list arg; + va_start(arg, format); + va_end(arg); + strcpy(tstamp, "\r\n"); + char ts[ISO8601_TIMESTAMP_LENGTH]; + GetISO8601Time(ts); + strcat(tstamp, ts); + strcat(tstamp, " "); + fwrite(tstamp, 1, strlen(tstamp), f); + vfprintf(f, format, arg); + fclose(f); + ESP_LOGI(TAG, "File written to %s", filename); +} + void LogFile(char *fname, char *format, ...) { char filename[32];