Qt: Add logging to file and console
Vicki Pfau vi@endrift.com
Sun, 20 Jan 2019 22:10:11 -0800
6 files changed,
93 insertions(+),
2 deletions(-)
M
src/platform/qt/LogConfigModel.h
→
src/platform/qt/LogConfigModel.h
@@ -30,6 +30,8 @@ virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
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*);
M
src/platform/qt/LogController.cpp
→
src/platform/qt/LogController.cpp
@@ -41,6 +41,9 @@ }
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 {@@ -50,6 +53,17 @@
void LogController::postLog(int level, int category, const QString& string) { if (!mLogFilterTest(&m_filter, category, static_cast<mLogLevel>(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); }@@ -92,6 +106,21 @@
void LogController::clearLevels(int category) { auto id = mLogCategoryId(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<QFile>(file); + m_logFile->open(QIODevice::Append | QIODevice::Text); + m_logStream = std::make_unique<QTextStream>(m_logFile.get()); } LogController* LogController::global() {
M
src/platform/qt/LogController.h
→
src/platform/qt/LogController.h
@@ -11,6 +11,8 @@ #include <mgba/core/log.h>
#include <QObject> #include <QStringList> +#include <QTextStream> +#include <memory> namespace QGBA {@@ -71,8 +73,16 @@ void enableLevels(int levels, int category);
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<QFile> m_logFile; + std::unique_ptr<QTextStream> m_logStream; static LogController s_global; };
M
src/platform/qt/SettingsView.cpp
→
src/platform/qt/SettingsView.cpp
@@ -300,6 +300,13 @@ m_ui.loggingView->setHorizontalHeader(new RotatedHeaderView(Qt::Horizontal));
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 @@ saveSetting("cheatAutoload", m_ui.cheatAutoload);
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 @@ emit languageChanged();
} 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 @@ loadSetting("cheatAutoload", m_ui.cheatAutoload, true);
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());
M
src/platform/qt/SettingsView.ui
→
src/platform/qt/SettingsView.ui
@@ -1211,8 +1211,8 @@ </item>
</layout> </widget> <widget class="QWidget" name="logging"> - <layout class="QGridLayout" name="a"> - <item row="0" column="0"> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> <widget class="QTableView" name="loggingView"> <attribute name="horizontalHeaderDefaultSectionSize"> <number>0</number>@@ -1221,6 +1221,38 @@ <attribute name="horizontalHeaderMinimumSectionSize">
<number>0</number> </attribute> </widget> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_19"> + <item> + <widget class="QCheckBox" name="logToFile"> + <property name="text"> + <string>Log to file</string> + </property> + </widget> + </item> + <item> + <widget class="QCheckBox" name="logToStdout"> + <property name="text"> + <string>Log to console</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_20"> + <item> + <widget class="QLineEdit" name="logFile"/> + </item> + <item> + <widget class="QPushButton" name="logFileBrowse"> + <property name="text"> + <string>Select Log File</string> + </property> + </widget> + </item> + </layout> </item> </layout> </widget>