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

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 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); 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; 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); String path = filepath(filename);
auto file = FileFS.open(path, "r"); auto file = FileFS.open(path, "r");
if (!file) { if (!file) {
return "failed"; return cnt;
} }
size = file.size(); file.seek(0, SeekSet);
if (size > max_size) { size_t size = file.size();
file.close(); size_t psn;
return "large"; do {
} cnt++;
String temp = file.readString(); file.readStringUntil('\n');
psn = file.position();
} while (psn < size);
file.close(); 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); String filepath(filename);
auto file = FileFS.open(filepath, "r"); auto file = FileFS.open(filepath, "r");
if (!file) { if (!file) {
return "failed"; return size;
} }
size_t size = file.size(); size = file.size();
file.close(); file.close();
return String(size); return size;
} }
const String getFSSizeInfo() { const String getFSSizeInfo() {

View File

@@ -50,26 +50,23 @@ void LoggingClass::execute(String keyOrValue) {
String filename = "logs/" + _key + ".txt"; String filename = "logs/" + _key + ".txt";
size_t sz = 0; size_t cnt = countLines(filename);
size_t sz = getFileSize(filename);
String logData = readFileSz(filename, 10240, sz); SerialPrint("I", "Logging", "http://" + WiFi.localIP().toString() + "/" + filename + " lines " + String(cnt, DEC) + ", size " + String(sz) + ", heap " + ESP.getFreeHeap());
size_t lines_cnt = itemsCount2(logData, "\r\n"); if ((cnt > _maxPoints + 1) || cnt == -1) {
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); removeFile(filename);
lines_cnt = 0; SerialPrint("E", "Logging", "file been remooved: " + filename + " " + String(cnt) + ">" + String(_maxPoints));
SerialPrint("E", "Logging", "file been remooved: " + filename + " " + String(lines_cnt) + ">" + String(_maxPoints)); cnt = 0;
} }
if (loggingValue != "") { 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++) { //for (int i = 0; i < 5; i++) {
logData = deleteBeforeDelimiter(logData, "\r\n"); logData = deleteBeforeDelimiter(logData, "\r\n");
//} //}