Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a define to configure the logfile handling #1709

Merged
merged 1 commit into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion code/components/jomjol_fileserver_ota/server_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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", "*");


// 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();

fd = fopen(currentfilename.c_str(), "r");
Expand Down
40 changes: 29 additions & 11 deletions code/components/jomjol_logfile/ClassLogFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ extern "C" {

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");

void ClassLogFile::WriteHeapInfo(std::string _id)
Expand Down Expand Up @@ -161,7 +165,7 @@ bool ClassLogFile::GetDataLogToSD(){
return doDataLogToSD;
}

static FILE* logFileAppendHande = NULL;
static FILE* logFileAppendHandle = NULL;
std::string fileNameDate;


Expand Down Expand Up @@ -231,34 +235,48 @@ void ClassLogFile::WriteToFile(esp_log_level_t level, std::string tag, std::stri

std::string fullmessage = "[" + formatedUptime + "] " + ntpTime + "\t<" + loglevelString + ">\t" + message + "\n";


#ifdef KEEP_LOGFILE_OPEN_FOR_APPENDING
if (fileNameDateNew != fileNameDate) { // Filename changed
// Make sure each day gets its own logfile
// Also we need to re-open it in case it needed to get closed for reading
std::string logpath = logroot + "/" + fileNameDateNew;

ESP_LOGI(TAG, "Opening logfile %s for appending", logpath.c_str());
logFileAppendHande = fopen(logpath.c_str(), "a+");
if (logFileAppendHande==NULL) {
logFileAppendHandle = fopen(logpath.c_str(), "a+");
if (logFileAppendHandle==NULL) {
ESP_LOGE(TAG, "Can't open log file %s", logpath.c_str());
return;
}

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(), logFileAppendHande);
fputs(fullmessage.c_str(), logFileAppendHandle);

fflush(logFileAppendHande);
fsync(fileno(logFileAppendHande));
#ifdef KEEP_LOGFILE_OPEN_FOR_APPENDING
fflush(logFileAppendHandle);
fsync(fileno(logFileAppendHandle));
#else
CloseLogFileAppendHandle();
#endif
}


void ClassLogFile::CloseLogFileAppendHandle() {

fclose(logFileAppendHande);
logFileAppendHande = NULL;
fileNameDate = "";
if (logFileAppendHandle != NULL) {
fclose(logFileAppendHandle);
logFileAppendHandle = NULL;
fileNameDate = "";
}
}


Expand Down