add a define to configure the logfile handling (#1709)

Co-authored-by: CaCO3 <caco@ruinelli.ch>
This commit is contained in:
jomjol
2022-12-28 14:28:00 +01:00
committed by GitHub
parent ee3b15990d
commit f2f117aea1
2 changed files with 30 additions and 12 deletions

View File

@@ -347,7 +347,7 @@ static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
// Since the log file is still open for writing, we need to close it first // Since the log file is still could open for writing, we need to close it first
LogFile.CloseLogFileAppendHandle(); LogFile.CloseLogFileAppendHandle();
fd = fopen(currentfilename.c_str(), "r"); fd = fopen(currentfilename.c_str(), "r");

View File

@@ -19,6 +19,10 @@ extern "C" {
static const char *TAG = "LOGFILE"; static const char *TAG = "LOGFILE";
/* Uncomment this to keep the logfile open for appending.
* If commented out, the logfile gets opened/closed for each log measage (old behaviour) */
//#define KEEP_LOGFILE_OPEN_FOR_APPENDING
ClassLogFile LogFile("/sdcard/log/message", "log_%Y-%m-%d.txt", "/sdcard/log/data", "data_%Y-%m-%d.csv"); ClassLogFile LogFile("/sdcard/log/message", "log_%Y-%m-%d.txt", "/sdcard/log/data", "data_%Y-%m-%d.csv");
void ClassLogFile::WriteHeapInfo(std::string _id) void ClassLogFile::WriteHeapInfo(std::string _id)
@@ -161,7 +165,7 @@ bool ClassLogFile::GetDataLogToSD(){
return doDataLogToSD; return doDataLogToSD;
} }
static FILE* logFileAppendHande = NULL; static FILE* logFileAppendHandle = NULL;
std::string fileNameDate; std::string fileNameDate;
@@ -231,35 +235,49 @@ void ClassLogFile::WriteToFile(esp_log_level_t level, std::string tag, std::stri
std::string fullmessage = "[" + formatedUptime + "] " + ntpTime + "\t<" + loglevelString + ">\t" + message + "\n"; std::string fullmessage = "[" + formatedUptime + "] " + ntpTime + "\t<" + loglevelString + ">\t" + message + "\n";
#ifdef KEEP_LOGFILE_OPEN_FOR_APPENDING
if (fileNameDateNew != fileNameDate) { // Filename changed if (fileNameDateNew != fileNameDate) { // Filename changed
// Make sure each day gets its own logfile // Make sure each day gets its own logfile
// Also we need to re-open it in case it needed to get closed for reading // Also we need to re-open it in case it needed to get closed for reading
std::string logpath = logroot + "/" + fileNameDateNew; std::string logpath = logroot + "/" + fileNameDateNew;
ESP_LOGI(TAG, "Opening logfile %s for appending", logpath.c_str()); ESP_LOGI(TAG, "Opening logfile %s for appending", logpath.c_str());
logFileAppendHande = fopen(logpath.c_str(), "a+"); logFileAppendHandle = fopen(logpath.c_str(), "a+");
if (logFileAppendHande==NULL) { if (logFileAppendHandle==NULL) {
ESP_LOGE(TAG, "Can't open log file %s", logpath.c_str()); ESP_LOGE(TAG, "Can't open log file %s", logpath.c_str());
return; return;
} }
fileNameDate = fileNameDateNew; fileNameDate = fileNameDateNew;
} }
#else
std::string logpath = logroot + "/" + fileNameDateNew;
logFileAppendHandle = fopen(logpath.c_str(), "a+");
if (logFileAppendHandle==NULL) {
ESP_LOGE(TAG, "Can't open log file %s", logpath.c_str());
return;
}
#endif
fputs(fullmessage.c_str(), logFileAppendHandle);
fputs(fullmessage.c_str(), logFileAppendHande); #ifdef KEEP_LOGFILE_OPEN_FOR_APPENDING
fflush(logFileAppendHandle);
fflush(logFileAppendHande); fsync(fileno(logFileAppendHandle));
fsync(fileno(logFileAppendHande)); #else
CloseLogFileAppendHandle();
#endif
} }
void ClassLogFile::CloseLogFileAppendHandle() { void ClassLogFile::CloseLogFileAppendHandle() {
if (logFileAppendHandle != NULL) {
fclose(logFileAppendHande); fclose(logFileAppendHandle);
logFileAppendHande = NULL; logFileAppendHandle = NULL;
fileNameDate = ""; fileNameDate = "";
} }
}
void ClassLogFile::WriteToFile(esp_log_level_t level, std::string tag, std::string message) { void ClassLogFile::WriteToFile(esp_log_level_t level, std::string tag, std::string message) {