Fix file retention, remove/correct logs (#1377)

This commit is contained in:
Slider0007
2022-11-18 19:23:18 +01:00
committed by GitHub
parent 513e300676
commit 0dd63b9b7a
6 changed files with 49 additions and 35 deletions

View File

@@ -27,6 +27,7 @@ ClassFlowCNNGeneral::ClassFlowCNNGeneral(ClassFlowAlignment *_flowalign, t_CNNTy
CNNType = AutoDetect; CNNType = AutoDetect;
CNNType = _cnntype; CNNType = _cnntype;
flowpostalignment = _flowalign; flowpostalignment = _flowalign;
logfileRetentionInDays = 5;
} }
string ClassFlowCNNGeneral::getReadout(int _analog = 0, bool _extendedResolution, int prev, float _vorgaengerAnalog, float analogDigitalTransitionStart) string ClassFlowCNNGeneral::getReadout(int _analog = 0, bool _extendedResolution, int prev, float _vorgaengerAnalog, float analogDigitalTransitionStart)

View File

@@ -23,7 +23,7 @@ ClassFlowImage::ClassFlowImage(const char* logTag)
this->logTag = logTag; this->logTag = logTag;
isLogImage = false; isLogImage = false;
disabled = false; disabled = false;
this->logfileRetentionInDays = 5;
} }
ClassFlowImage::ClassFlowImage(std::vector<ClassFlow*> * lfc, const char* logTag) : ClassFlow(lfc) ClassFlowImage::ClassFlowImage(std::vector<ClassFlow*> * lfc, const char* logTag) : ClassFlow(lfc)
@@ -31,6 +31,7 @@ ClassFlowImage::ClassFlowImage(std::vector<ClassFlow*> * lfc, const char* logTag
this->logTag = logTag; this->logTag = logTag;
isLogImage = false; isLogImage = false;
disabled = false; disabled = false;
this->logfileRetentionInDays = 5;
} }
ClassFlowImage::ClassFlowImage(std::vector<ClassFlow*> * lfc, ClassFlow *_prev, const char* logTag) : ClassFlow(lfc, _prev) ClassFlowImage::ClassFlowImage(std::vector<ClassFlow*> * lfc, ClassFlow *_prev, const char* logTag) : ClassFlow(lfc, _prev)
@@ -38,6 +39,7 @@ ClassFlowImage::ClassFlowImage(std::vector<ClassFlow*> * lfc, ClassFlow *_prev,
this->logTag = logTag; this->logTag = logTag;
isLogImage = false; isLogImage = false;
disabled = false; disabled = false;
this->logfileRetentionInDays = 5;
} }
@@ -91,7 +93,7 @@ void ClassFlowImage::RemoveOldLogs()
if (!isLogImage) if (!isLogImage)
return; return;
ESP_LOGI(logTag, "remove old log images"); ESP_LOGI(TAG, "remove old images");
if (logfileRetentionInDays == 0) { if (logfileRetentionInDays == 0) {
return; return;
} }
@@ -101,16 +103,17 @@ void ClassFlowImage::RemoveOldLogs()
char cmpfilename[30]; char cmpfilename[30];
time(&rawtime); time(&rawtime);
rawtime = addDays(rawtime, -logfileRetentionInDays); rawtime = addDays(rawtime, -logfileRetentionInDays + 1);
timeinfo = localtime(&rawtime); timeinfo = localtime(&rawtime);
//ESP_LOGD(TAG, "ImagefileRetentionInDays: %d", logfileRetentionInDays);
strftime(cmpfilename, 30, LOGFILE_TIME_FORMAT, timeinfo); strftime(cmpfilename, 30, LOGFILE_TIME_FORMAT, timeinfo);
//ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename); //ESP_LOGD(TAG, "file name to compare: %s", cmpfilename);
string folderName = string(cmpfilename).LOGFILE_TIME_FORMAT_DATE_EXTR; string folderName = string(cmpfilename).LOGFILE_TIME_FORMAT_DATE_EXTR;
DIR *dir = opendir(LogImageLocation.c_str()); DIR *dir = opendir(LogImageLocation.c_str());
if (!dir) { if (!dir) {
ESP_LOGI(logTag, "Failed to stat dir : %s", LogImageLocation.c_str()); ESP_LOGE(TAG, "Failed to stat dir : %s", LogImageLocation.c_str());
return; return;
} }
@@ -120,8 +123,8 @@ void ClassFlowImage::RemoveOldLogs()
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
string folderPath = LogImageLocation + "/" + entry->d_name; string folderPath = LogImageLocation + "/" + entry->d_name;
if (entry->d_type == DT_DIR) { if (entry->d_type == DT_DIR) {
//ESP_LOGI(logTag, "Compare %s %s", entry->d_name, folderName.c_str()); //ESP_LOGD(TAG, "Compare %s to %s", entry->d_name, folderName.c_str());
if ((strlen(entry->d_name) == folderName.length()) && (strcmp(entry->d_name, folderName.c_str()) == 0)) { if ((strlen(entry->d_name) == folderName.length()) && (strcmp(entry->d_name, folderName.c_str()) < 0)) {
removeFolder(folderPath.c_str(), logTag); removeFolder(folderPath.c_str(), logTag);
deleted++; deleted++;
} else { } else {
@@ -129,7 +132,7 @@ void ClassFlowImage::RemoveOldLogs()
} }
} }
} }
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Image folder deleted: " + std::to_string(deleted) + ". Image folder not deleted: " + std::to_string(notDeleted)); ESP_LOGI(TAG, "Image folder deleted: %d | Image folder not deleted: %d", deleted, notDeleted);
closedir(dir); closedir(dir);
} }

View File

@@ -57,6 +57,8 @@ void ClassFlowMakeImage::SetInitialParameter(void)
ClassFlowMakeImage::ClassFlowMakeImage(std::vector<ClassFlow*>* lfc) : ClassFlowImage(lfc, TAG) ClassFlowMakeImage::ClassFlowMakeImage(std::vector<ClassFlow*>* lfc) : ClassFlowImage(lfc, TAG)
{ {
LogImageLocation = "/log/source";
logfileRetentionInDays = 5;
SetInitialParameter(); SetInitialParameter();
} }
@@ -104,6 +106,10 @@ bool ClassFlowMakeImage::ReadParameter(FILE* pfile, string& aktparamgraph)
waitbeforepicture = stoi(zerlegt[1]); waitbeforepicture = stoi(zerlegt[1]);
} }
if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1))
{
this->logfileRetentionInDays = std::stoi(zerlegt[1]);
}
if ((toUpper(zerlegt[0]) == "BRIGHTNESS") && (zerlegt.size() > 1)) if ((toUpper(zerlegt[0]) == "BRIGHTNESS") && (zerlegt.size() > 1))
{ {

View File

@@ -90,7 +90,7 @@ string getSDCardPartitionSize(){
f_getfree("0:", (DWORD *)&fre_clust, &fs); f_getfree("0:", (DWORD *)&fre_clust, &fs);
tot_sect = ((fs->n_fatent - 2) * fs->csize) /1024 /(1024/SDCardCsd.sector_size); //corrected by SD Card sector size (usually 512 bytes) and convert to MB tot_sect = ((fs->n_fatent - 2) * fs->csize) /1024 /(1024/SDCardCsd.sector_size); //corrected by SD Card sector size (usually 512 bytes) and convert to MB
printf("%d MB total drive space (Sector size [bytes]: %d)\n", (int)tot_sect, (int)fs->csize*512); //ESP_LOGD(TAG, "%d MB total drive space (Sector size [bytes]: %d)", (int)tot_sect, (int)fs->ssize);
return std::to_string(tot_sect); return std::to_string(tot_sect);
} }
@@ -103,7 +103,7 @@ string getSDCardFreePartitionSpace(){
f_getfree("0:", (DWORD *)&fre_clust, &fs); f_getfree("0:", (DWORD *)&fre_clust, &fs);
fre_sect = (fre_clust * fs->csize) / 1024 /(1024/SDCardCsd.sector_size); //corrected by SD Card sector size (usually 512 bytes) and convert to MB fre_sect = (fre_clust * fs->csize) / 1024 /(1024/SDCardCsd.sector_size); //corrected by SD Card sector size (usually 512 bytes) and convert to MB
printf("%d MB free drive space (Sector size [bytes]: %d)\n", (int)fre_sect, (int)fs->ssize); //ESP_LOGD(TAG, "%d MB free drive space (Sector size [bytes]: %d)", (int)fre_sect, (int)fs->ssize);
return std::to_string(fre_sect); return std::to_string(fre_sect);
} }
@@ -116,7 +116,7 @@ string getSDCardPartitionAllocationSize(){
f_getfree("0:", (DWORD *)&fre_clust, &fs); f_getfree("0:", (DWORD *)&fre_clust, &fs);
allocation_size = fs->ssize; allocation_size = fs->ssize;
printf("SD Card Partition Allocation Size (bytes): %d)\n", allocation_size); //ESP_LOGD(TAG, "SD Card Partition Allocation Size: %d bytes", allocation_size);
return std::to_string(allocation_size); return std::to_string(allocation_size);
} }
@@ -129,28 +129,28 @@ void SaveSDCardInfo(sdmmc_card_t* card) {
string getSDCardManufacturer(){ string getSDCardManufacturer(){
string SDCardManufacturer = SDCardParseManufacturerIDs(SDCardCid.mfg_id); string SDCardManufacturer = SDCardParseManufacturerIDs(SDCardCid.mfg_id);
printf("SD Card Manufactuer: %s\n", SDCardManufacturer.c_str()); //ESP_LOGD(TAG, "SD Card Manufacturer: %s", SDCardManufacturer.c_str());
return (SDCardManufacturer + " (ID: " + std::to_string(SDCardCid.mfg_id) + ")"); return (SDCardManufacturer + " (ID: " + std::to_string(SDCardCid.mfg_id) + ")");
} }
string getSDCardName(){ string getSDCardName(){
char *SDCardName = SDCardCid.name; char *SDCardName = SDCardCid.name;
printf("SD Card Name: %s\n", SDCardName); //ESP_LOGD(TAG, "SD Card Name: %s", SDCardName);
return std::string(SDCardName); return std::string(SDCardName);
} }
string getSDCardCapacity(){ string getSDCardCapacity(){
int SDCardCapacity = SDCardCsd.capacity / (1024/SDCardCsd.sector_size) / 1024; // total sectors * sector size --> Byte to MB (1024*1024) int SDCardCapacity = SDCardCsd.capacity / (1024/SDCardCsd.sector_size) / 1024; // total sectors * sector size --> Byte to MB (1024*1024)
printf("SD Card Capacity: %s\n", std::to_string(SDCardCapacity).c_str()); //ESP_LOGD(TAG, "SD Card Capacity: %s", std::to_string(SDCardCapacity).c_str());
return std::to_string(SDCardCapacity); return std::to_string(SDCardCapacity);
} }
string getSDCardSectorSize(){ string getSDCardSectorSize(){
int SDCardSectorSize = SDCardCsd.sector_size; int SDCardSectorSize = SDCardCsd.sector_size;
printf("SD Card Sector Size: %s\n", std::to_string(SDCardSectorSize).c_str()); //ESP_LOGD(TAG, "SD Card Sector Size: %s bytes", std::to_string(SDCardSectorSize).c_str());
return std::to_string(SDCardSectorSize); return std::to_string(SDCardSectorSize);
} }
@@ -500,11 +500,11 @@ time_t addDays(time_t startTime, int days) {
} }
int removeFolder(const char* folderPath, const char* logTag) { int removeFolder(const char* folderPath, const char* logTag) {
ESP_LOGI(logTag, "Delete folder %s", folderPath); //ESP_LOGD(logTag, "Delete content in path %s", folderPath);
DIR *dir = opendir(folderPath); DIR *dir = opendir(folderPath);
if (!dir) { if (!dir) {
ESP_LOGI(logTag, "Failed to stat dir : %s", folderPath); ESP_LOGE(logTag, "Failed to stat dir : %s", folderPath);
return -1; return -1;
} }
@@ -513,6 +513,7 @@ int removeFolder(const char* folderPath, const char* logTag) {
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
std::string path = string(folderPath) + "/" + entry->d_name; std::string path = string(folderPath) + "/" + entry->d_name;
if (entry->d_type == DT_REG) { if (entry->d_type == DT_REG) {
//ESP_LOGD(logTag, "Delete file %s", path.c_str());
if (unlink(path.c_str()) == 0) { if (unlink(path.c_str()) == 0) {
deleted ++; deleted ++;
} else { } else {
@@ -525,9 +526,9 @@ int removeFolder(const char* folderPath, const char* logTag) {
closedir(dir); closedir(dir);
if (rmdir(folderPath) != 0) { if (rmdir(folderPath) != 0) {
ESP_LOGE(logTag, "can't delete file : %s", folderPath); ESP_LOGE(logTag, "can't delete folder : %s", folderPath);
} }
ESP_LOGI(logTag, "%d older log files in folder %s deleted.", deleted, folderPath); ESP_LOGD(logTag, "%d files in folder %s deleted.", deleted, folderPath);
return deleted; return deleted;
} }

View File

@@ -294,17 +294,18 @@ void ClassLogFile::RemoveOld()
char cmpfilename[30]; char cmpfilename[30];
time(&rawtime); time(&rawtime);
rawtime = addDays(rawtime, -retentionInDays); rawtime = addDays(rawtime, -retentionInDays + 1);
timeinfo = localtime(&rawtime); timeinfo = localtime(&rawtime);
//ESP_LOGD(TAG, "logfileRetentionInDays: %d", retentionInDays);
////////////////////// message ///////////////////////////////////////// ////////////////////// message /////////////////////////////////////////
ESP_LOGI(TAG, "remove old log files");
strftime(cmpfilename, 30, logfile.c_str(), timeinfo); strftime(cmpfilename, 30, logfile.c_str(), timeinfo);
//ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename); //ESP_LOGD(TAG, "log file name to compare: %s", cmpfilename);
DIR *dir = opendir(logroot.c_str()); DIR *dir = opendir(logroot.c_str());
if (!dir) { if (!dir) {
ESP_LOGI(TAG, "Failed to stat dir : %s", logroot.c_str()); ESP_LOGE(TAG, "Failed to stat dir : %s", logroot.c_str());
return; return;
} }
@@ -313,31 +314,33 @@ void ClassLogFile::RemoveOld()
int notDeleted = 0; int notDeleted = 0;
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) { if (entry->d_type == DT_REG) {
//ESP_LOGI(TAG, "list log file : %s %s", entry->d_name, cmpfilename); //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)) { if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) {
ESP_LOGI(TAG, "delete log file : %s", entry->d_name); //ESP_LOGD(TAG, "delete log file : %s", entry->d_name);
std::string filepath = logroot + "/" + entry->d_name; std::string filepath = logroot + "/" + entry->d_name;
if (unlink(filepath.c_str()) == 0) { if (unlink(filepath.c_str()) == 0) {
deleted ++; deleted ++;
} else { } else {
ESP_LOGE(TAG, "can't delete file : %s", entry->d_name); ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
notDeleted ++;
} }
} else { } else {
notDeleted ++; notDeleted ++;
} }
} }
} }
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "logfiles deleted: " + std::to_string(deleted) + " files not deleted (incl. leer.txt): " + std::to_string(notDeleted)); ESP_LOGI(TAG, "data files deleted: %d | files not deleted (incl. leer.txt): %d", deleted, notDeleted);
closedir(dir); closedir(dir);
////////////////////// data ///////////////////////////////////////// ////////////////////// data /////////////////////////////////////////
ESP_LOGI(TAG, "remove old data files");
strftime(cmpfilename, 30, datafile.c_str(), timeinfo); strftime(cmpfilename, 30, datafile.c_str(), timeinfo);
//ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename); //ESP_LOGD(TAG, "data file name to compare: %s", cmpfilename);
dir = opendir(dataroot.c_str()); dir = opendir(dataroot.c_str());
if (!dir) { if (!dir) {
ESP_LOGI(TAG, "Failed to stat dir : %s", dataroot.c_str()); ESP_LOGE(TAG, "Failed to stat dir : %s", dataroot.c_str());
return; return;
} }
@@ -345,21 +348,22 @@ void ClassLogFile::RemoveOld()
notDeleted = 0; notDeleted = 0;
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) { if (entry->d_type == DT_REG) {
//ESP_LOGI(TAG, "list log file : %s %s", entry->d_name, cmpfilename); //ESP_LOGD(TAG, "Compare data file : %s to %s", entry->d_name, cmpfilename);
if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) { if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) {
ESP_LOGI(TAG, "delete data file : %s", entry->d_name); //ESP_LOGD(TAG, "delete data file : %s", entry->d_name);
std::string filepath = logroot + "/" + entry->d_name; std::string filepath = dataroot + "/" + entry->d_name;
if (unlink(filepath.c_str()) == 0) { if (unlink(filepath.c_str()) == 0) {
deleted ++; deleted ++;
} else { } else {
ESP_LOGE(TAG, "can't delete file : %s", entry->d_name); ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
notDeleted ++;
} }
} else { } else {
notDeleted ++; notDeleted ++;
} }
} }
} }
ESP_LOGI(TAG, "%d older log files deleted. %d current log files not deleted.", deleted, notDeleted); ESP_LOGI(TAG, "data files deleted: %d | files not deleted (incl. leer.txt): %d", deleted, notDeleted);
closedir(dir); closedir(dir);
} }
@@ -382,5 +386,4 @@ ClassLogFile::ClassLogFile(std::string _logroot, std::string _logfile, std::stri
dataroot = _logdatapath; dataroot = _logdatapath;
retentionInDays = 10; retentionInDays = 10;
loglevel = ESP_LOG_INFO; loglevel = ESP_LOG_INFO;
MakeDir("/sdcard/log/data");
} }

View File

@@ -231,7 +231,7 @@ void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostna
ESP_LOGE(TAG,"failed to set hostname:%d",ret); ESP_LOGE(TAG,"failed to set hostname:%d",ret);
} }
else { else {
ESP_LOGE(TAG,"Set Hostname to:%s", _hostname); ESP_LOGI(TAG,"Set Hostname to:%s", _hostname);
} }
} }