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