new method PILog::readAllLogs

This commit is contained in:
2024-11-18 22:47:02 +03:00
parent c9a5ddd89f
commit 24112498ce
3 changed files with 59 additions and 3 deletions

View File

@@ -188,7 +188,7 @@ int answer_to_connection(void * cls,
return MHD_NO;
}
piCout << "answer" << url << method << (int)m << server;
// piCout << "answer" << url << method << (int)m << server;
MicrohttpdServerConnection *& conn((MicrohttpdServerConnection *&)(*con_cls));
if (!conn) {
conn = new MicrohttpdServerConnection();

View File

@@ -110,6 +110,58 @@ void PILog::stop() {
}
PIStringList PILog::readAllLogs() const {
PIMap<PISystemTime, PIString> names;
auto dir = PIDir(log_dir);
auto fil = dir.entries();
for (auto fi: fil) {
if (!fi.isFile()) continue;
if (!fi.name().contains(".log.")) continue;
names[PIDateTime::current().fromString(fi.baseName(), "yyyy_MM_dd__hh_mm_ss").toSystemTime()] = dir.relative(fi.path);
}
PIStringList ret;
PIString cur_filename = dir.relative(log_file.path());
auto it = names.makeIterator();
bool was_own = false;
auto readFile = [&ret](PIFile * f) {
PIIOTextStream ts(f);
PIString line;
while (!ts.isEnd()) {
line = ts.readLine().trim();
if (line.isNotEmpty()) ret << line;
}
};
while (it.next()) {
PIFile * f = nullptr;
bool own = true;
if (it.value() == cur_filename) {
log_mutex.lock();
f = &log_file;
f->seekToBegin();
own = false;
was_own = true;
} else {
f = new PIFile(log_dir + "/" + it.value(), PIIODevice::ReadOnly);
}
readFile(f);
if (own)
delete f;
else {
f->seekToEnd();
log_mutex.unlock();
}
}
if (!was_own) {
log_mutex.lock();
log_file.seekToBegin();
readFile(&log_file);
log_file.seekToEnd();
log_mutex.unlock();
}
return ret;
}
void PILog::coutDone(int id, PIString * buffer) {
if (!buffer) return;
if (!id_by_cat.containsValue(id)) return;

View File

@@ -144,6 +144,10 @@ public:
//! \~russian Записывает все строки из очереди и останавливается. Также вызывается в деструкторе.
void stop();
//! \~english Read all previous and current log content and returns them as %PIStringList.
//! \~russian Читает все предыдущие и текущий логи и возвращает их как %PIStringList.
PIStringList readAllLogs() const;
private:
EVENT_HANDLER2(void, coutDone, int, id, PIString *, buff);
@@ -160,8 +164,8 @@ private:
void newFile();
void run() override;
PIMutex log_mutex;
PIFile log_file;
mutable PIMutex log_mutex;
mutable PIFile log_file;
PIIOTextStream log_ts;
PITimeMeasurer split_tm;
PISystemTime split_time;