From d3d241c7b91831b159b45c17c9cf80aef0230e65 Mon Sep 17 00:00:00 2001 From: Slider0007 <115730895+Slider0007@users.noreply.github.com> Date: Wed, 1 Mar 2023 20:55:12 +0100 Subject: [PATCH] Logfile: Print start indication block after time sync (#2106) * logfile: Print start indication after time sync * No time set at boot -> keep log_1970-01-01.txt --- .../jomjol_logfile/ClassLogFile.cpp | 25 +++++++++++++------ .../components/jomjol_time_sntp/time_sntp.cpp | 14 ++++++++++- code/components/jomjol_time_sntp/time_sntp.h | 1 + 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/code/components/jomjol_logfile/ClassLogFile.cpp b/code/components/jomjol_logfile/ClassLogFile.cpp index d702ecc7..b4ec02c9 100644 --- a/code/components/jomjol_logfile/ClassLogFile.cpp +++ b/code/components/jomjol_logfile/ClassLogFile.cpp @@ -15,6 +15,7 @@ extern "C" { #endif #include "Helper.h" +#include "time_sntp.h" #include "../../include/defines.h" static const char *TAG = "LOGFILE"; @@ -321,15 +322,23 @@ void ClassLogFile::RemoveOldLogFile() //ESP_LOGD(TAG, "compare log file: %s to %s", entry->d_name, cmpfilename); if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) { //ESP_LOGD(TAG, "delete log file: %s", entry->d_name); - std::string filepath = logroot + "/" + entry->d_name; - if (unlink(filepath.c_str()) == 0) { - deleted ++; - } else { - ESP_LOGE(TAG, "can't delete file: %s", entry->d_name); - notDeleted ++; + std::string filepath = logroot + "/" + entry->d_name; + if ((strcmp(entry->d_name, "log_1970-01-01.txt") == 0) && getTimeWasNotSetAtBoot()) { // keep logfile log_1970-01-01.txt if time was not set at boot (some boot logs are in there) + //ESP_LOGD(TAG, "Skip deleting this file: %s", entry->d_name); + notDeleted++; } - } else { - notDeleted ++; + else { + if (unlink(filepath.c_str()) == 0) { + deleted++; + } + else { + ESP_LOGE(TAG, "can't delete file: %s", entry->d_name); + notDeleted++; + } + } + } + else { + notDeleted++; } } } diff --git a/code/components/jomjol_time_sntp/time_sntp.cpp b/code/components/jomjol_time_sntp/time_sntp.cpp index 25b0b0f3..5a15d26f 100644 --- a/code/components/jomjol_time_sntp/time_sntp.cpp +++ b/code/components/jomjol_time_sntp/time_sntp.cpp @@ -25,6 +25,7 @@ static const char *TAG = "SNTP"; static std::string timeZone = ""; static std::string timeServer = "undefined"; static bool useNtp = true; +static bool timeWasNotSetAtBoot = false; std::string getNtpStatusText(sntp_sync_status_t status); static void setTimeZone(std::string _tzstring); @@ -59,7 +60,12 @@ std::string getCurrentTimeString(const char * frm) void time_sync_notification_cb(struct timeval *tv) { - LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Time is now successfully synced with NTP Server " + + if (timeWasNotSetAtBoot) { + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "================================================="); + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "==================== Start ======================"); + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "== Logs before time sync -> log_1970-01-01.txt =="); + } + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Time is synced with NTP Server " + getServerName() + ": " + getCurrentTimeString("%Y-%m-%d %H:%M:%S")); } @@ -111,6 +117,11 @@ bool getUseNtp(void) { return useNtp; } +bool getTimeWasNotSetAtBoot(void) +{ + return timeWasNotSetAtBoot; +} + std::string getServerName(void) { char buf[100]; @@ -225,6 +236,7 @@ bool setupTime() { LogFile.WriteToFile(ESP_LOG_INFO, TAG, "The local time is unknown, starting with " + std::string(strftime_buf)); if (useNtp) { LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Once the NTP server provides a time, we will switch to that one"); + timeWasNotSetAtBoot = true; } } diff --git a/code/components/jomjol_time_sntp/time_sntp.h b/code/components/jomjol_time_sntp/time_sntp.h index 99f5ba1e..2faf324f 100644 --- a/code/components/jomjol_time_sntp/time_sntp.h +++ b/code/components/jomjol_time_sntp/time_sntp.h @@ -22,6 +22,7 @@ std::string ConvertTimeToString(time_t _time, const char * frm); bool getTimeIsSet(void); +bool getTimeWasNotSetAtBoot(void); bool getUseNtp(void); bool setupTime();