From da461ef2ec4c541b8463f06d964ad8db1cda8539 Mon Sep 17 00:00:00 2001 From: woblight Date: Sat, 21 Sep 2024 19:32:36 +0200 Subject: [PATCH] Add logging file and window --- AddonsPanel.qml | 1 - CMakeLists.txt | 1 + Options.qml | 5 +++-- control.cpp | 34 ++++++++++++++++++++++++++++++++-- control.h | 5 ++++- main.cpp | 18 ++++++++++++++++++ main.qml | 48 ++++++++++++++++++++++++++++++++++-------------- qml.qrc | 1 + 8 files changed, 93 insertions(+), 20 deletions(-) diff --git a/AddonsPanel.qml b/AddonsPanel.qml index d42c1a2..13f0564 100644 --- a/AddonsPanel.qml +++ b/AddonsPanel.qml @@ -22,7 +22,6 @@ import QtQuick.Layouts Page { title: "Addons" header: ToolBar { - Layout.fillWidth: true RowLayout { anchors.fill: parent ToolButton { diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ac1cbf..a0e9b9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,7 @@ set(qml_files Tray.qml UniversalSettings.qml Update.qml + LogWindow.qml ) set(${PROJECT_NAME}_BUILD_NAME "" CACHE STRING "name of the build pipeline for self updates") diff --git a/Options.qml b/Options.qml index becd2ca..f598a43 100644 --- a/Options.qml +++ b/Options.qml @@ -20,9 +20,10 @@ import GitAddonsManager.engine import QtQuick.Layouts import QtCore -Pane { +ScrollView { + id: optPage ColumnLayout { - width: parent.width + width: Math.max(optPage.availableWidth, implicitWidth) Repeater { model: Engine.addonsPaths.length RowLayout { diff --git a/control.cpp b/control.cpp index 471b9f8..f257270 100644 --- a/control.cpp +++ b/control.cpp @@ -100,16 +100,19 @@ void Control::delegate(QString taskname, auto work, auto callback) using ret_t = typename std::invoke_result::type; QFuture fut = QtConcurrent::run(m_pool, work); QFutureWatcher *fw = new QFutureWatcher(); - connect(fw, &QFutureWatcher::finished, [this, callback, fw, lock=std::move(lock)](){ + connect(fw, &QFutureWatcher::finished, [this, callback, taskname, fw, lock=std::move(lock)](){ m_lock = false; TaskQueue old; m_tasks.swap(old); + qInfo() << "Executing callback for " << taskname; if constexpr (std::is_same::value) callback(); else callback(fw->result()); - + + qInfo() << "Completed " << taskname; + m_tasks.append(old); if (m_status != Status::Error) { @@ -732,3 +735,30 @@ Control::BusyLock::~BusyLock() if (!--count && m_locked->m_tasks.empty() && m_locked->status() != Status::Error) m_locked->setStatus(Status::Ready); } + +void Control::log(QtMsgType type, const QMessageLogContext& context, const QString& msg) +{ + QString message = QString("%2"); + QString color; + switch(type) + { + case QtDebugMsg: + color = "silver"; + break; + case QtInfoMsg: + color = "black"; + break; + case QtWarningMsg: + color = "orange"; + break; + case QtFatalMsg: + color = "red"; + break; + case QtCriticalMsg: + color = "darkred"; + break; + } + + emit logMessage(message.arg(color, msg)); +} + diff --git a/control.h b/control.h index f23688b..ec2a096 100644 --- a/control.h +++ b/control.h @@ -79,7 +79,6 @@ public: Q_PROPERTY(QStringList addonsPaths READ addonsPaths WRITE setAddonsPaths NOTIFY addonsPathsChanged) Q_PROPERTY(bool useRepoDirectory READ useRepoDirectory WRITE setUseRepoDirectory NOTIFY useRepoDirectoryChanged) - static QStringList m_availableStyles; ~Control(); @@ -149,6 +148,8 @@ public: QString version() const; bool selfUpdates() const; + + void log(QtMsgType type, const QMessageLogContext &context, const QString &msg); private: static Control *m_instance; @@ -219,6 +220,8 @@ signals: void useRepoDirectoryChanged(bool use); + void logMessage(QString log); + public slots: void setAddons(QList addons); void saveAddonsPaths(); diff --git a/main.cpp b/main.cpp index e1c4e61..d13aab2 100644 --- a/main.cpp +++ b/main.cpp @@ -26,11 +26,29 @@ #include #include #include +#include + +QtMessageHandler originalHandler = nullptr; + +void logger(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + QString message = qFormatLogMessage(type, context, msg); + static FILE *f = fopen("log.txt", "w"); + fprintf(f, "%s\n", qPrintable(message)); + fflush(f); + + Control::instance()->log(type, context, msg); + if (originalHandler) + originalHandler(type, context, msg); +} int main(int argc, char *argv[]) { QApplication app(argc, argv); + qSetMessagePattern("[%{time yyyyMMdd h:mm:ss.zzz t} %{if-debug}D%{endif}%{if-info}I%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{message}"); + originalHandler = qInstallMessageHandler(&logger); + QCommandLineParser parser; parser.addOption({"update", "directory to update", "updateLocation"}); parser.process(app); diff --git a/main.qml b/main.qml index 0c7f292..b95c6ee 100644 --- a/main.qml +++ b/main.qml @@ -95,7 +95,10 @@ ApplicationWindow { Action { id: exportList icon.name: "export-symbolic" - onTriggered: exportListDialog.visible = true + onTriggered: { + exportListDialog.targetPath = null + exportListDialog.visible = true + } enabled: Engine.status == Engine.Ready && addonsReady shortcut: StandardKey.SaveAs @@ -106,10 +109,30 @@ ApplicationWindow { enabled: availableUpdates > 0 } - ColumnLayout{ + Page { anchors.fill: parent - TabBar { - Layout.fillWidth: true + footer: ToolBar { + height: messageLabel.implicitHeight + RowLayout + { + anchors.fill: parent + Label { + id: messageLabel + text: Engine.statusMessage + } + Label { + id: logButton + text: qsTr("[View log]") + MouseArea { + onClicked: logWindow.visible = !logWindow.visible + anchors.fill: parent + } + Layout.fillHeight: true + Layout.alignment: Qt.AlignRight + } + } + } + header: TabBar { id: bar TabButton { text: qsTr("Addons") @@ -122,19 +145,12 @@ ApplicationWindow { } } - StackLayout { - Layout.fillWidth: true - Layout.fillHeight: true + contentItem: StackLayout { currentIndex: bar.currentIndex AddonsPanel { - Layout.fillHeight: true - Layout.fillWidth: true - } Options { - Layout.fillWidth: true - Layout.fillHeight: true } ColumnLayout { @@ -304,7 +320,7 @@ ApplicationWindow { x: (parent.width - width) / 2 y: (parent.height - height) / 2 title: "An error occourred" - id: errorDialong + id: errorDialog Label { text: Engine.statusMessage } @@ -437,8 +453,12 @@ ApplicationWindow { } FileDialog { id: exportListDialog - onAccepted: Engine.exportAddonList(selectedFile) + property string targetPath + onAccepted: Engine.exportAddonList(selectedFile, targetPath) fileMode: FileDialog.SaveFile defaultSuffix: "txt" } + LogWindow { + id: logWindow + } } diff --git a/qml.qrc b/qml.qrc index 439c42c..f4ac005 100644 --- a/qml.qrc +++ b/qml.qrc @@ -5,6 +5,7 @@ Options.qml AddonUpdateButton.qml AddonsPanel.qml + LogWindow.qml github_clone_url.jpg gitlab_clone_url.jpg icons/breeze/svg/delete.svg