Thread safe access to log messages list

This commit is contained in:
Max Risuhin 2018-08-16 00:27:13 +03:00
parent 915a8e7522
commit e3aa8a598d
No known key found for this signature in database
GPG Key ID: BF733F5ACA0B4448
1 changed files with 14 additions and 5 deletions

View File

@ -25,8 +25,11 @@
#include "utilities.h" #include "utilities.h"
#ifdef BUILD_FOR_BUNDLE #ifdef BUILD_FOR_BUNDLE
#include <QMutexLocker>
QStringList consoleOutputStrings; QStringList consoleOutputStrings;
bool ubuntuServerStarted = false; bool ubuntuServerStarted = false;
QMutex consoleOutputMutex;
#endif #endif
const int MAIN_WINDOW_WIDTH = 1024; const int MAIN_WINDOW_WIDTH = 1024;
@ -210,6 +213,7 @@ QString getDataStoragePath() {
} }
void writeLogsToFile() { void writeLogsToFile() {
QMutexLocker locker(&consoleOutputMutex);
QFile logFile(getDataStoragePath() + "/StatusIm.log"); QFile logFile(getDataStoragePath() + "/StatusIm.log");
if (logFile.open(QIODevice::WriteOnly | QIODevice::Append)) { if (logFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
for (QString message : consoleOutputStrings) { for (QString message : consoleOutputStrings) {
@ -261,6 +265,11 @@ void runUbuntuServer() {
qDebug() << "waiting finished"; qDebug() << "waiting finished";
} }
void appendConsoleString(const QString &msg) {
QMutexLocker locker(&consoleOutputMutex);
consoleOutputStrings << msg;
}
void saveMessage(QtMsgType type, const QMessageLogContext &context, void saveMessage(QtMsgType type, const QMessageLogContext &context,
const QString &msg) { const QString &msg) {
@ -269,20 +278,20 @@ void saveMessage(QtMsgType type, const QMessageLogContext &context,
switch (type) { switch (type) {
case QtDebugMsg: case QtDebugMsg:
consoleOutputStrings << "Debug: " << message << "\n"; appendConsoleString(QString("Debug: %1 \n").arg(message));
break; break;
case QtInfoMsg: case QtInfoMsg:
consoleOutputStrings << "Info: " << message << "\n"; appendConsoleString(QString("Info: %1 \n").arg(message));
break; break;
case QtWarningMsg: case QtWarningMsg:
consoleOutputStrings << "Warning: " << message << "\n"; appendConsoleString(QString("Warning: %1 \n").arg(message));
break; break;
case QtCriticalMsg: case QtCriticalMsg:
consoleOutputStrings << "Critical: " << message << "\n"; appendConsoleString(QString("Critical: %1 \n").arg(message));
break; break;
case QtFatalMsg: case QtFatalMsg:
consoleOutputStrings << "Fatal: " << message << "\n"; appendConsoleString(QString("Fatal: %1 \n").arg(message));
writeLogsToFile(); writeLogsToFile();
abort(); abort();
} }