From 8db10be892e8c35464e37b420a2479ec532716ca Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Fri, 24 Jun 2016 10:39:44 +0200 Subject: [PATCH] Log UI --- cockatrice/CMakeLists.txt | 2 ++ cockatrice/src/dlg_viewlog.cpp | 35 +++++++++++++++++++ cockatrice/src/dlg_viewlog.h | 20 +++++++++++ cockatrice/src/logger.cpp | 62 ++++++++++++++++++++++++++++++++++ cockatrice/src/logger.h | 40 ++++++++++++++++++++++ cockatrice/src/main.cpp | 15 +++----- cockatrice/src/pictureloader.h | 2 +- cockatrice/src/window_main.cpp | 11 ++++++ cockatrice/src/window_main.h | 3 +- 9 files changed, 178 insertions(+), 12 deletions(-) create mode 100644 cockatrice/src/dlg_viewlog.cpp create mode 100644 cockatrice/src/dlg_viewlog.h create mode 100644 cockatrice/src/logger.cpp create mode 100644 cockatrice/src/logger.h diff --git a/cockatrice/CMakeLists.txt b/cockatrice/CMakeLists.txt index 9f5b6223..d4c7845f 100644 --- a/cockatrice/CMakeLists.txt +++ b/cockatrice/CMakeLists.txt @@ -17,6 +17,7 @@ SET(cockatrice_SOURCES src/dlg_edit_user.cpp src/dlg_register.cpp src/dlg_update.cpp + src/dlg_viewlog.cpp src/abstractclient.cpp src/remoteclient.cpp src/main.cpp @@ -110,6 +111,7 @@ SET(cockatrice_SOURCES src/settings/layoutssettings.cpp src/update_checker.cpp src/update_downloader.cpp + src/logger.cpp ${VERSION_STRING_CPP} ) diff --git a/cockatrice/src/dlg_viewlog.cpp b/cockatrice/src/dlg_viewlog.cpp new file mode 100644 index 00000000..99e81cef --- /dev/null +++ b/cockatrice/src/dlg_viewlog.cpp @@ -0,0 +1,35 @@ +#include "dlg_viewlog.h" +#include "logger.h" + +#include +#include + +DlgViewLog::DlgViewLog(QWidget *parent) +: QDialog(parent) +{ + logArea = new QPlainTextEdit; + logArea->setReadOnly(true); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(logArea); + + setLayout(mainLayout); + + setWindowTitle(tr("View debug log")); + resize(800, 500); + + loadInitialLogBuffer(); + connect(&Logger::getInstance(), SIGNAL(logEntryAdded(QString)), this, SLOT(logEntryAdded(QString))); +} + +void DlgViewLog::loadInitialLogBuffer() +{ + QVector logBuffer = Logger::getInstance().getLogBuffer(); + foreach(QString message, logBuffer) + logEntryAdded(message); +} + +void DlgViewLog::logEntryAdded(QString message) +{ + logArea->appendPlainText(message); +} diff --git a/cockatrice/src/dlg_viewlog.h b/cockatrice/src/dlg_viewlog.h new file mode 100644 index 00000000..5d420b1c --- /dev/null +++ b/cockatrice/src/dlg_viewlog.h @@ -0,0 +1,20 @@ +#ifndef DLG_VIEWLOG_H +#define DLG_VIEWLOG_H + +#include + +class QPlainTextEdit; + +class DlgViewLog : public QDialog { +Q_OBJECT +public: + DlgViewLog(QWidget *parent); +private: + QPlainTextEdit *logArea; + + void loadInitialLogBuffer(); +private slots: + void logEntryAdded(QString message); +}; + +#endif diff --git a/cockatrice/src/logger.cpp b/cockatrice/src/logger.cpp new file mode 100644 index 00000000..96986d70 --- /dev/null +++ b/cockatrice/src/logger.cpp @@ -0,0 +1,62 @@ +#include "logger.h" + +#include + +#define LOGGER_MAX_ENTRIES 128 +#define LOGGER_FILENAME "qdebug.txt" + +Logger::Logger() +: logToFileEnabled(false) +{ +} + +Logger::~Logger() +{ + closeLogfileSession(); + logBuffer.clear(); +} + +void Logger::logToFile(bool enabled) +{ + if(enabled) + openLogfileSession(); + else + closeLogfileSession(); +} + +void Logger::openLogfileSession() +{ + if(logToFileEnabled) + return; + + fileHandle.setFileName(LOGGER_FILENAME); + fileHandle.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text); + fileStream.setDevice(&fileHandle); + fileStream << "Log session started at " << QDateTime::currentDateTime().toString() << endl; + logToFileEnabled = true; +} + +void Logger::closeLogfileSession() +{ + if(!logToFileEnabled) + return; + + logToFileEnabled = false; + fileStream << "Log session closed at " << QDateTime::currentDateTime().toString() << endl; + fileHandle.close(); +} + + +void Logger::log(QtMsgType /* type */, const QMessageLogContext & /* ctx */, const QString &message) +{ + logBuffer.append(message); + if(logBuffer.size() > LOGGER_MAX_ENTRIES) + logBuffer.removeFirst(); + + emit logEntryAdded(message); + + if(logToFileEnabled) + { + fileStream << message << endl; + } +} \ No newline at end of file diff --git a/cockatrice/src/logger.h b/cockatrice/src/logger.h new file mode 100644 index 00000000..c20140a3 --- /dev/null +++ b/cockatrice/src/logger.h @@ -0,0 +1,40 @@ +#ifndef LOGGER_H +#define LOGGER_H + +#include +#include +#include +#include + +class Logger : public QObject { +Q_OBJECT +public: + static Logger& getInstance() + { + static Logger instance; + return instance; + } +private: + Logger(); + ~Logger(); + // Singleton - Don't implement copy constructor and assign operator + Logger(Logger const&); + void operator=(Logger const&); + + bool logToFileEnabled; + QTextStream fileStream; + QFile fileHandle; + + QVector logBuffer; +public: + void logToFile(bool enabled); + void log(QtMsgType type, const QMessageLogContext &ctx, const QString &message); + QVector getLogBuffer() { return logBuffer; } +protected: + void openLogfileSession(); + void closeLogfileSession(); +signals: + void logEntryAdded(QString message); +}; + +#endif diff --git a/cockatrice/src/main.cpp b/cockatrice/src/main.cpp index 793a008a..6559cc05 100644 --- a/cockatrice/src/main.cpp +++ b/cockatrice/src/main.cpp @@ -41,8 +41,7 @@ #include "rng_sfmt.h" #include "soundengine.h" #include "featureset.h" - -//Q_IMPORT_PLUGIN(qjpeg) +#include "logger.h" CardDatabase *db; QTranslator *translator, *qtTranslator; @@ -55,13 +54,8 @@ ThemeManager *themeManager; const QString translationPrefix = "cockatrice"; QString translationPath; -static void myMessageOutput(QtMsgType /*type*/, const QMessageLogContext &, const QString &msg) -{ - QFile file("qdebug.txt"); - file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text); - QTextStream out(&file); - out << msg << endl; - file.close(); +static void CockatriceLogger(QtMsgType type, const QMessageLogContext &ctx, const QString &message) { + Logger::getInstance().log(type, ctx, message); } void installNewTranslator() @@ -91,8 +85,9 @@ int main(int argc, char *argv[]) { QApplication app(argc, argv); + qInstallMessageHandler(CockatriceLogger); if (app.arguments().contains("--debug-output")) - qInstallMessageHandler(myMessageOutput); + Logger::getInstance().logToFile(true); #ifdef Q_OS_WIN app.addLibraryPath(app.applicationDirPath() + "/plugins"); diff --git a/cockatrice/src/pictureloader.h b/cockatrice/src/pictureloader.h index 58c52232..899d6a72 100644 --- a/cockatrice/src/pictureloader.h +++ b/cockatrice/src/pictureloader.h @@ -75,7 +75,7 @@ public: private: PictureLoader(); ~PictureLoader(); - // Don't implement + // Singleton - Don't implement copy constructor and assign operator PictureLoader(PictureLoader const&); void operator=(PictureLoader const&); diff --git a/cockatrice/src/window_main.cpp b/cockatrice/src/window_main.cpp index fbf46937..69977cd1 100644 --- a/cockatrice/src/window_main.cpp +++ b/cockatrice/src/window_main.cpp @@ -39,6 +39,7 @@ #include "dlg_register.h" #include "dlg_settings.h" #include "dlg_update.h" +#include "dlg_viewlog.h" #include "tab_supervisor.h" #include "remoteclient.h" #include "localserver.h" @@ -304,6 +305,12 @@ void MainWindow::actUpdate() dlg.exec(); } +void MainWindow::actViewLog() +{ + DlgViewLog dlg(this); + dlg.exec(); +} + void MainWindow::serverTimeout() { QMessageBox::critical(this, tr("Error"), tr("Server timeout")); @@ -511,6 +518,7 @@ void MainWindow::retranslateUi() aAbout->setText(tr("&About Cockatrice")); aUpdate->setText(tr("&Update Cockatrice")); + aViewLog->setText(tr("View &debug log")); helpMenu->setTitle(tr("&Help")); aCheckCardUpdates->setText(tr("Check for card updates...")); tabSupervisor->retranslateUi(); @@ -543,6 +551,8 @@ void MainWindow::createActions() connect(aAbout, SIGNAL(triggered()), this, SLOT(actAbout())); aUpdate = new QAction(this); connect(aUpdate, SIGNAL(triggered()), this, SLOT(actUpdate())); + aViewLog = new QAction(this); + connect(aViewLog, SIGNAL(triggered()), this, SLOT(actViewLog())); aCheckCardUpdates = new QAction(this); connect(aCheckCardUpdates, SIGNAL(triggered()), this, SLOT(actCheckCardUpdates())); @@ -610,6 +620,7 @@ void MainWindow::createMenus() helpMenu = menuBar()->addMenu(QString()); helpMenu->addAction(aAbout); helpMenu->addAction(aUpdate); + helpMenu->addAction(aViewLog); } MainWindow::MainWindow(QWidget *parent) diff --git a/cockatrice/src/window_main.h b/cockatrice/src/window_main.h index 113a5d97..b5ed81d6 100644 --- a/cockatrice/src/window_main.h +++ b/cockatrice/src/window_main.h @@ -70,6 +70,7 @@ private slots: void actAbout(); void actUpdate(); + void actViewLog(); void iconActivated(QSystemTrayIcon::ActivationReason reason); @@ -106,7 +107,7 @@ private: QList tabMenus; QMenu *cockatriceMenu, *dbMenu, *helpMenu; QAction *aConnect, *aDisconnect, *aSinglePlayer, *aWatchReplay, *aDeckEditor, *aFullScreen, *aSettings, *aExit, - *aAbout, *aCheckCardUpdates, *aRegister, *aUpdate; + *aAbout, *aCheckCardUpdates, *aRegister, *aUpdate, *aViewLog; QAction *aEditSets, *aEditTokens, *aOpenCustomFolder, *aOpenCustomsetsFolder, *aAddCustomSet; TabSupervisor *tabSupervisor;