PILog works
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include "pilog.h"
|
||||
|
||||
#include "pidir.h"
|
||||
#include "piliterals_string.h"
|
||||
#include "piliterals_time.h"
|
||||
#include "pitime.h"
|
||||
|
||||
@@ -71,12 +72,16 @@
|
||||
|
||||
|
||||
PILog::PILog(): PIThread(), log_ts(&log_file) {
|
||||
split_time = 8_h;
|
||||
setName("PILog");
|
||||
split_time = 8_h;
|
||||
timestamp_format = "yyyy-MM-dd hh:mm:ss.zzz";
|
||||
setLineFormat("t - c: m");
|
||||
CONNECTU(PICout::Notifier::object(), finished, this, coutDone);
|
||||
}
|
||||
|
||||
|
||||
PILog::~PILog() {
|
||||
stopAndWait();
|
||||
stop();
|
||||
}
|
||||
|
||||
|
||||
@@ -89,16 +94,91 @@ void PILog::setDir(const PIString & d) {
|
||||
}
|
||||
|
||||
|
||||
void PILog::enqueue(const PIString & msg) {
|
||||
void PILog::setLineFormat(const PIString & f) {
|
||||
line_format = f;
|
||||
line_format_p = line_format;
|
||||
line_format_p.replace("t", "${t}").replace("c", "${c}").replace("m", "${m}");
|
||||
}
|
||||
|
||||
|
||||
PICout PILog::debug(PIObject * context) {
|
||||
return makePICout(context, Category::Debug);
|
||||
}
|
||||
|
||||
|
||||
PICout PILog::warning(PIObject * context) {
|
||||
return makePICout(context, Category::Warning);
|
||||
}
|
||||
|
||||
|
||||
PICout PILog::error(PIObject * context) {
|
||||
return makePICout(context, Category::Error);
|
||||
}
|
||||
|
||||
|
||||
void PILog::stop() {
|
||||
while (true) {
|
||||
log_mutex.lock();
|
||||
bool done = queue.isEmpty();
|
||||
log_mutex.unlock();
|
||||
if (done) break;
|
||||
piMinSleep();
|
||||
}
|
||||
PIThread::stopAndWait();
|
||||
}
|
||||
|
||||
|
||||
void PILog::coutDone(int id, PIString * buffer) {
|
||||
cout_mutex.lock();
|
||||
if (!cout_cat_by_id.contains(id)) {
|
||||
cout_mutex.unlock();
|
||||
return;
|
||||
}
|
||||
auto cat = cout_cat_by_id.take(id, PILog::Category::Debug);
|
||||
cout_mutex.unlock();
|
||||
if (!buffer) return;
|
||||
enqueue(*buffer, cat);
|
||||
delete buffer;
|
||||
}
|
||||
|
||||
|
||||
PICout PILog::makePICout(PIObject * context, Category cat) {
|
||||
cout_mutex.lock();
|
||||
int id = ++cout_id;
|
||||
auto buffer = new PIString();
|
||||
cout_cat_by_id[id] = cat;
|
||||
cout_mutex.unlock();
|
||||
if (context) {
|
||||
*buffer = "["_a + context->className();
|
||||
if (context->name().isNotEmpty()) *buffer += " \"" + context->name() + "\"";
|
||||
*buffer += "] ";
|
||||
}
|
||||
return PICout::withExternalBuffer(buffer, id, PICoutManipulators::AddSpaces);
|
||||
}
|
||||
|
||||
|
||||
void PILog::enqueue(const PIString & msg, Category cat) {
|
||||
if (log_file.isClosed()) return;
|
||||
auto cur_dt = PIDateTime::fromSystemTime(PISystemTime::current());
|
||||
auto t = PIDateTime::fromSystemTime(PISystemTime::current());
|
||||
PIMutexLocker ml(log_mutex);
|
||||
queue.enqueue(cur_dt.toString("hh:mm:ss.zzz") + " - " + msg);
|
||||
queue.enqueue({cat, t, msg});
|
||||
}
|
||||
|
||||
|
||||
PIString PILog::entryToString(const Entry & e) const {
|
||||
static PIStringList categories{"debug", "warn ", "error"};
|
||||
PIString t = e.time.toString(timestamp_format);
|
||||
PIString ret = line_format_p;
|
||||
ret.replace("${t}", t).replace("${c}", categories[static_cast<int>(e.cat)]).replace("${m}", e.msg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void PILog::newFile() {
|
||||
log_file.open(log_dir + "/" + PIDateTime::current().toString("yyyy_MM_dd__hh_mm_ss__p" + PIString::fromNumber(++part_number) + ".txt"),
|
||||
PIString aname = app_name;
|
||||
if (aname.isNotEmpty()) aname += "__";
|
||||
log_file.open(log_dir + "/" + aname + PIDateTime::current().toString("yyyy_MM_dd__hh_mm_ss") + ".log." +
|
||||
PIString::fromNumber(++part_number),
|
||||
PIIODevice::ReadWrite);
|
||||
}
|
||||
|
||||
@@ -123,6 +203,8 @@ void PILog::run() {
|
||||
}
|
||||
auto qi = queue.dequeue();
|
||||
log_mutex.unlock();
|
||||
log_ts << qi << "\n";
|
||||
auto str = entryToString(qi);
|
||||
log_ts << str << "\n";
|
||||
piCout << str;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user