From 2a60d391fe667c8667919f1559cec2eb8e45ba7d Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 20 Jan 2019 22:10:11 -0800 Subject: [PATCH] Qt: Add logging to file and console --- CHANGES | 2 ++ src/platform/qt/LogConfigModel.h | 2 ++ src/platform/qt/LogController.cpp | 29 +++++++++++++++++++++++++ src/platform/qt/LogController.h | 10 +++++++++ src/platform/qt/SettingsView.cpp | 16 ++++++++++++++ src/platform/qt/SettingsView.ui | 36 +++++++++++++++++++++++++++++-- 6 files changed, 93 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 7365c0ebc..4a166e445 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,6 @@ 0.8.0: (Future) +Features: + - Improved logging configuration Bugfixes: - GBA: All IRQs have 7 cycle delay (fixes mgba.io/i/539, mgba.io/i/1208) - GBA: Reset now reloads multiboot ROMs diff --git a/src/platform/qt/LogConfigModel.h b/src/platform/qt/LogConfigModel.h index b936bc43f..d64e7b22e 100644 --- a/src/platform/qt/LogConfigModel.h +++ b/src/platform/qt/LogConfigModel.h @@ -30,6 +30,8 @@ public: virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override; virtual Qt::ItemFlags flags(const QModelIndex& index) const override; + LogController* logger() { return m_controller; } + public slots: void reset(); void save(ConfigController*); diff --git a/src/platform/qt/LogController.cpp b/src/platform/qt/LogController.cpp index 95d515205..5ad220ad6 100644 --- a/src/platform/qt/LogController.cpp +++ b/src/platform/qt/LogController.cpp @@ -41,6 +41,9 @@ LogController::Stream LogController::operator()(int category, int level) { void LogController::load(const ConfigController* config) { mLogFilterLoad(&m_filter, config->config()); + setLogFile(config->getOption("logFile")); + logToStdout(config->getOption("logToStdout").toInt()); + logToFile(config->getOption("logToFile").toInt()); } void LogController::save(ConfigController* config) const { @@ -51,6 +54,17 @@ void LogController::postLog(int level, int category, const QString& string) { if (!mLogFilterTest(&m_filter, category, static_cast(level))) { return; } + if (m_logToStdout || m_logToFile) { + QString line = tr("[%1] %2: %3").arg(LogController::toString(level)).arg(mLogCategoryName(category)).arg(string); + + if (m_logToStdout) { + QTextStream out(stdout); + out << line << endl; + } + if (m_logToFile && m_logStream) { + *m_logStream << line << endl; + } + } emit logPosted(level, category, string); } @@ -94,6 +108,21 @@ void LogController::clearLevels(int category) { mLogFilterReset (&m_filter, id); } +void LogController::logToFile(bool log) { + m_logToFile = log; +} + +void LogController::logToStdout(bool log) { + m_logToStdout = log; +} + +void LogController::setLogFile(const QString& file) { + m_logStream.reset(); + m_logFile = std::make_unique(file); + m_logFile->open(QIODevice::Append | QIODevice::Text); + m_logStream = std::make_unique(m_logFile.get()); +} + LogController* LogController::global() { return &s_global; } diff --git a/src/platform/qt/LogController.h b/src/platform/qt/LogController.h index c189e03ae..a06f7bf47 100644 --- a/src/platform/qt/LogController.h +++ b/src/platform/qt/LogController.h @@ -11,6 +11,8 @@ #include #include +#include +#include namespace QGBA { @@ -71,8 +73,16 @@ public slots: void disableLevels(int levels, int category); void clearLevels(int category); + void logToFile(bool); + void logToStdout(bool); + void setLogFile(const QString&); + private: mLogFilter m_filter; + bool m_logToFile; + bool m_logToStdout; + std::unique_ptr m_logFile; + std::unique_ptr m_logStream; static LogController s_global; }; diff --git a/src/platform/qt/SettingsView.cpp b/src/platform/qt/SettingsView.cpp index b7611b148..7f2ded961 100644 --- a/src/platform/qt/SettingsView.cpp +++ b/src/platform/qt/SettingsView.cpp @@ -300,6 +300,13 @@ SettingsView::SettingsView(ConfigController* controller, InputController* inputC m_ui.loggingView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); m_ui.loggingView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); + connect(m_ui.logFileBrowse, &QAbstractButton::pressed, [this] () { + QString path = GBAApp::app()->getSaveFileName(this, "Select log file"); + if (!path.isNull()) { + m_ui.logFile->setText(path); + } + }); + ShortcutView* shortcutView = new ShortcutView(); shortcutView->setController(shortcutController); shortcutView->setInputController(inputController); @@ -379,6 +386,9 @@ void SettingsView::updateConfig() { saveSetting("cheatAutosave", m_ui.cheatAutosave); saveSetting("autoload", m_ui.autoload); saveSetting("autosave", m_ui.autosave); + saveSetting("logToFile", m_ui.logToFile); + saveSetting("logToStdout", m_ui.logToStdout); + saveSetting("logFile", m_ui.logFile); if (m_ui.fastForwardUnbounded->isChecked()) { saveSetting("fastForwardRatio", "-1"); @@ -438,6 +448,9 @@ void SettingsView::updateConfig() { } m_logModel.save(m_controller); + m_logModel.logger()->setLogFile(m_ui.logFile->text()); + m_logModel.logger()->logToFile(m_ui.logToFile->isChecked()); + m_logModel.logger()->logToStdout(m_ui.logToStdout->isChecked()); #ifdef M_CORE_GB GBModel modelGB = s_gbModelList[m_ui.gbModel->currentIndex()]; @@ -505,6 +518,9 @@ void SettingsView::reloadConfig() { loadSetting("cheatAutosave", m_ui.cheatAutosave, true); loadSetting("autoload", m_ui.autoload, true); loadSetting("autosave", m_ui.autosave, false); + loadSetting("logToFile", m_ui.logToFile); + loadSetting("logToStdout", m_ui.logToStdout); + loadSetting("logFile", m_ui.logFile); m_ui.libraryStyle->setCurrentIndex(loadSetting("libraryStyle").toInt()); diff --git a/src/platform/qt/SettingsView.ui b/src/platform/qt/SettingsView.ui index 7cd9f727f..0a2da478e 100644 --- a/src/platform/qt/SettingsView.ui +++ b/src/platform/qt/SettingsView.ui @@ -1211,8 +1211,8 @@ - - + + 0 @@ -1222,6 +1222,38 @@ + + + + + + Log to file + + + + + + + Log to console + + + + + + + + + + + + + + Select Log File + + + + +