diff --git a/include/Utils/FileUtils.h b/include/Utils/FileUtils.h index 7e176723..686b3a2b 100644 --- a/include/Utils/FileUtils.h +++ b/include/Utils/FileUtils.h @@ -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); diff --git a/src/Utils/FileUtils.cpp b/src/Utils/FileUtils.cpp index 02aa9d9c..aa8cbaed 100644 --- a/src/Utils/FileUtils.cpp +++ b/src/Utils/FileUtils.cpp @@ -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() { diff --git a/src/items/vLogging.cpp b/src/items/vLogging.cpp index e6b999f4..cff4371d 100644 --- a/src/items/vLogging.cpp +++ b/src/items/vLogging.cpp @@ -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"); //}