55 lines
1.4 KiB
C++
55 lines
1.4 KiB
C++
#include "logs_port.h"
|
|
|
|
#include <QFile>
|
|
#include <QDateTime>
|
|
|
|
logs::LogsOutput::LogsOutput(const QDir& target, int size) : _cache_size(size) {
|
|
auto dt = QDateTime::currentDateTime();
|
|
auto base_name = QString(u8"logs_%1.u8txt").arg(dt.toString(u8"yyyyMMdd_hhmmss"));
|
|
auto full_path = target.filePath(base_name);
|
|
|
|
_target_file = std::make_shared<QFile>(full_path);
|
|
if (!_target_file->open(QIODevice::WriteOnly | QIODevice::Text)) {
|
|
throw - 1;
|
|
}
|
|
}
|
|
|
|
void logs::LogsOutput::write(const QString& type, const QString& title, const QString& msg) {
|
|
auto dt = QDateTime::currentDateTime();
|
|
|
|
_mutex.lock();
|
|
_message_buffer << QString(u8"%1[%2]£º%3£º%4").arg(type, dt.toString(u8"hh:mm:ss"), title, msg);
|
|
_mutex.unlock();
|
|
|
|
flush();
|
|
}
|
|
|
|
void logs::LogsOutput::information(const QString& title, const QString& msg) {
|
|
write(u8"Information", title, msg);
|
|
}
|
|
|
|
void logs::LogsOutput::warning(const QString& title, const QString& msg) {
|
|
write(u8"Warning", title, msg);
|
|
}
|
|
|
|
void logs::LogsOutput::error(const QString& title, const QString& msg) {
|
|
write(u8"Error", title, msg);
|
|
flush(true);
|
|
}
|
|
|
|
#include <QTextStream>
|
|
void logs::LogsOutput::flush(bool fouce) {
|
|
QMutexLocker l(&_mutex);
|
|
|
|
if (!fouce && _message_buffer.size() < _cache_size)
|
|
return;
|
|
|
|
QTextStream tout(_target_file.get());
|
|
tout.setCodec(u8"UTF-8");
|
|
for (auto& line : _message_buffer) {
|
|
tout << line << u8"\n";
|
|
}
|
|
tout.flush();
|
|
_message_buffer.clear();
|
|
}
|