переписано логгирование

This commit is contained in:
Dmitry Borisenko
2021-01-02 04:27:19 +01:00
parent f8d574d15a
commit 673bfe6b6b
3 changed files with 33 additions and 32 deletions

View File

@@ -46,14 +46,14 @@ const String writeFile(const String& filename, const String& str);
const String readFile(const String& filename, size_t max_size);
/*
* Чтение файла в строку с записью его размера
* Посчитать
*/
const String readFileSz(const String& filename, size_t max_size, size_t& size);
size_t countLines(const String filename);
/*
* Размер файла
*/
const String getFileSize(const String& filename);
size_t getFileSize(const String filename);
bool copyFile(const String& src, const String& dst, bool overwrite = true);

View File

@@ -130,31 +130,35 @@ const String readFile(const String& filename, size_t max_size) {
return temp;
}
const String readFileSz(const String& filename, size_t max_size, size_t& size) {
size_t countLines(const String filename) {
size_t cnt = -1;
String path = filepath(filename);
auto file = FileFS.open(path, "r");
if (!file) {
return "failed";
return cnt;
}
size = file.size();
if (size > max_size) {
file.close();
return "large";
}
String temp = file.readString();
file.seek(0, SeekSet);
size_t size = file.size();
size_t psn;
do {
cnt++;
file.readStringUntil('\n');
psn = file.position();
} while (psn < size);
file.close();
return temp;
return cnt;
}
const String getFileSize(const String filename) {
size_t getFileSize(const String filename) {
size_t size = -1;
String filepath(filename);
auto file = FileFS.open(filepath, "r");
if (!file) {
return "failed";
return size;
}
size_t size = file.size();
size = file.size();
file.close();
return String(size);
return size;
}
const String getFSSizeInfo() {

View File

@@ -49,27 +49,24 @@ void LoggingClass::execute(String keyOrValue) {
}
String filename = "logs/" + _key + ".txt";
size_t cnt = countLines(filename);
size_t sz = getFileSize(filename);
size_t sz = 0;
SerialPrint("I", "Logging", "http://" + WiFi.localIP().toString() + "/" + filename + " lines " + String(cnt, DEC) + ", size " + String(sz) + ", heap " + ESP.getFreeHeap());
String logData = readFileSz(filename, 10240, sz);
size_t lines_cnt = itemsCount2(logData, "\r\n");
SerialPrint("I", "Logging", "http://" + WiFi.localIP().toString() + "/" + filename + " lines " + String(lines_cnt, DEC) + ", size " + String(sz) + ", heap " + ESP.getFreeHeap());
if (logData == "large") {
SerialPrint("E", "Logging", "File is very large");
}
if ((lines_cnt > _maxPoints + 1) || !lines_cnt) {
removeFile(filename);
lines_cnt = 0;
SerialPrint("E", "Logging", "file been remooved: " + filename + " " + String(lines_cnt) + ">" + String(_maxPoints));
if ((cnt > _maxPoints + 1) || cnt == -1) {
removeFile(filename);
SerialPrint("E", "Logging", "file been remooved: " + filename + " " + String(cnt) + ">" + String(_maxPoints));
cnt = 0;
}
if (loggingValue != "") {
if (lines_cnt > _maxPoints) { //удаляем старую строку и добавляем новую
if (cnt > _maxPoints) { //удаляем старую строку и добавляем новую
String logData = readFile(filename, 10240);
if (logData == "large") {
SerialPrint("E", "Logging", "File is very large");
}
//for (int i = 0; i < 5; i++) {
logData = deleteBeforeDelimiter(logData, "\r\n");
//}