Write logs without QTextStream.

This commit is contained in:
John Preston 2018-06-10 10:46:15 +03:00
parent bf775cb4ca
commit 68c2f563c6
1 changed files with 19 additions and 21 deletions

View File

@ -69,9 +69,9 @@ public:
void closeMain() { void closeMain() {
QMutexLocker lock(_logsMutex(LogDataMain)); QMutexLocker lock(_logsMutex(LogDataMain));
if (files[LogDataMain]) { const auto file = files[LogDataMain].get();
streams[LogDataMain].setDevice(0); if (file && file->isOpen()) {
files[LogDataMain]->close(); file->close();
} }
} }
@ -80,11 +80,12 @@ public:
} }
QString full() { QString full() {
if (!streams[LogDataMain].device()) { const auto file = files[LogDataMain].get();
if (!!file || !file->isOpen()) {
return QString(); return QString();
} }
QFile out(files[LogDataMain]->fileName()); QFile out(file->fileName());
if (out.open(QIODevice::ReadOnly)) { if (out.open(QIODevice::ReadOnly)) {
return QString::fromUtf8(out.readAll()); return QString::fromUtf8(out.readAll());
} }
@ -93,27 +94,29 @@ public:
void write(LogDataType type, const QString &msg) { void write(LogDataType type, const QString &msg) {
QMutexLocker lock(_logsMutex(type)); QMutexLocker lock(_logsMutex(type));
if (type != LogDataMain) reopenDebug(); if (type != LogDataMain) {
if (!streams[type].device()) return; reopenDebug();
}
streams[type] << msg; const auto file = files[type].get();
streams[type].flush(); if (!file || !file->isOpen()) {
return;
}
file->write(msg.toUtf8());
file->flush();
} }
private: private:
std::unique_ptr<QFile> files[LogDataCount]; std::unique_ptr<QFile> files[LogDataCount];
QTextStream streams[LogDataCount];
int32 part = -1; int32 part = -1;
bool reopen(LogDataType type, int32 dayIndex, const QString &postfix) { bool reopen(LogDataType type, int32 dayIndex, const QString &postfix) {
if (streams[type].device()) { if (files[type] && files[type]->isOpen()) {
if (type == LogDataMain) { if (type == LogDataMain) {
if (!postfix.isEmpty()) { if (!postfix.isEmpty()) {
return true; return true;
} }
} else { } else {
streams[type].setDevice(0);
files[type]->close(); files[type]->close();
} }
} }
@ -134,8 +137,6 @@ private:
} }
if (to->open(mode | QIODevice::Append)) { if (to->open(mode | QIODevice::Append)) {
std::swap(files[type], to); std::swap(files[type], to);
streams[type].setDevice(files[type].get());
streams[type].setCodec("UTF-8");
LOG(("Moved logging from '%1' to '%2'!").arg(to->fileName()).arg(files[type]->fileName())); LOG(("Moved logging from '%1' to '%2'!").arg(to->fileName()).arg(files[type]->fileName()));
to->remove(); to->remove();
@ -192,17 +193,14 @@ private:
} }
} }
if (files[type]->open(mode)) { if (files[type]->open(mode)) {
streams[type].setDevice(files[type].get());
streams[type].setCodec("UTF-8");
if (type != LogDataMain) { if (type != LogDataMain) {
streams[type] << ((mode & QIODevice::Append) files[type]->write(((mode & QIODevice::Append)
? qsl("\ ? qsl("\
----------------------------------------------------------------\n\ ----------------------------------------------------------------\n\
NEW LOGGING INSTANCE STARTED!!!\n\ NEW LOGGING INSTANCE STARTED!!!\n\
----------------------------------------------------------------\n") ----------------------------------------------------------------\n")
: qsl("%1\n").arg(dayIndex)); : qsl("%1\n").arg(dayIndex)).toUtf8());
streams[type].flush(); files[type]->flush();
} }
return true; return true;