Fix crash on logger; make log window modalless (#2659)

This commit is contained in:
ctrlaltca 2017-04-26 21:05:24 +02:00 committed by Zach H
parent 9dd3a04a08
commit ce77d51a8f
6 changed files with 39 additions and 18 deletions

View file

@ -33,3 +33,8 @@ void DlgViewLog::logEntryAdded(QString message)
{
logArea->appendPlainText(message);
}
void DlgViewLog::closeEvent(QCloseEvent * /* event */)
{
logArea->clear();
}

View file

@ -4,11 +4,14 @@
#include <QDialog>
class QPlainTextEdit;
class QCloseEvent;
class DlgViewLog : public QDialog {
Q_OBJECT
public:
DlgViewLog(QWidget *parent);
protected:
void closeEvent(QCloseEvent *event);
private:
QPlainTextEdit *logArea;

View file

@ -48,21 +48,24 @@ void Logger::closeLogfileSession()
}
void Logger::log(QtMsgType /* type */, const QMessageLogContext & /* ctx */, const QString &message)
void Logger::log(QtMsgType /* type */, const QMessageLogContext & /* ctx */, const QString message)
{
QMetaObject::invokeMethod(this, "internalLog", Qt::QueuedConnection,
Q_ARG(const QString &, message)
);
}
void Logger::internalLog(const QString message)
{
QMutexLocker locker(&mutex);
logBuffer.append(message);
if (logBuffer.size() > LOGGER_MAX_ENTRIES)
logBuffer.clear();
logBuffer.removeFirst();
if (message.size() > 0) {
emit logEntryAdded(message);
std::cerr << message.toStdString() << std::endl; // Print to stdout
emit logEntryAdded(message);
std::cerr << message.toStdString() << std::endl; // Print to stdout
if (logToFileEnabled)
fileStream << message << endl; // Print to fileStream
}
}
if (logToFileEnabled)
fileStream << message << endl; // Print to fileStream
}

View file

@ -5,6 +5,7 @@
#include <QFile>
#include <QVector>
#include <QString>
#include <QMutex>
class Logger : public QObject {
Q_OBJECT
@ -24,15 +25,17 @@ private:
bool logToFileEnabled;
QTextStream fileStream;
QFile fileHandle;
QList<QString> logBuffer;
QMutex mutex;
public:
void logToFile(bool enabled);
void log(QtMsgType type, const QMessageLogContext &ctx, const QString &message);
void log(QtMsgType type, const QMessageLogContext &ctx, const QString message);
QList<QString> getLogBuffer() { return logBuffer; }
protected:
void openLogfileSession();
void closeLogfileSession();
protected slots:
void internalLog(const QString message);
signals:
void logEntryAdded(QString message);
};

View file

@ -314,8 +314,13 @@ void MainWindow::actUpdate()
void MainWindow::actViewLog()
{
DlgViewLog dlg(this);
dlg.exec();
if (logviewDialog == nullptr) {
logviewDialog = new DlgViewLog(this);
}
logviewDialog->show();
logviewDialog->raise();
logviewDialog->activateWindow();
}
void MainWindow::serverTimeout()
@ -652,7 +657,7 @@ void MainWindow::createMenus()
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), localServer(0), bHasActivated(false), cardUpdateProcess(0)
: QMainWindow(parent), localServer(0), bHasActivated(false), cardUpdateProcess(0), logviewDialog(0)
{
connect(settingsCache, SIGNAL(pixmapCacheSizeChanged(int)), this, SLOT(pixmapCacheSizeChanged(int)));
pixmapCacheSizeChanged(settingsCache->getPixmapCacheSize());

View file

@ -36,6 +36,7 @@ class LocalClient;
class LocalServer;
class ServerInfo_User;
class QThread;
class DlgViewLog;
class MainWindow : public QMainWindow {
Q_OBJECT
@ -125,6 +126,7 @@ private:
QMessageBox serverShutdownMessageBox;
QProcess * cardUpdateProcess;
DlgViewLog * logviewDialog;
public:
MainWindow(QWidget *parent = 0);
~MainWindow();