Use LogView to implement level highlighting for other logs

Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
Yihe Li 2025-04-15 03:42:28 +08:00
parent 41a790d258
commit 4ac6a0629b
No known key found for this signature in database
6 changed files with 158 additions and 95 deletions

View File

@ -46,7 +46,7 @@ class InstancePageProvider : protected QObject, public BasePageProvider {
values.append(new InstanceSettingsPage(onesix)); values.append(new InstanceSettingsPage(onesix));
auto logMatcher = inst->getLogFileMatcher(); auto logMatcher = inst->getLogFileMatcher();
if (logMatcher) { if (logMatcher) {
values.append(new OtherLogsPage(inst->getLogFileRoot(), logMatcher)); values.append(new OtherLogsPage(inst, logMatcher));
} }
return values; return values;
} }

View File

@ -52,11 +52,8 @@
#include <BuildConfig.h> #include <BuildConfig.h>
class LogFormatProxyModel : public QIdentityProxyModel { QVariant LogFormatProxyModel::data(const QModelIndex& index, int role) const
public: {
LogFormatProxyModel(QObject* parent = nullptr) : QIdentityProxyModel(parent) {}
QVariant data(const QModelIndex& index, int role) const override
{
const LogColors& colors = APPLICATION->themeManager()->getLogColors(); const LogColors& colors = APPLICATION->themeManager()->getLogColors();
switch (role) { switch (role) {
@ -83,12 +80,10 @@ class LogFormatProxyModel : public QIdentityProxyModel {
} }
return QIdentityProxyModel::data(index, role); return QIdentityProxyModel::data(index, role);
} }
void setFont(QFont font) { m_font = font; } QModelIndex LogFormatProxyModel::find(const QModelIndex& start, const QString& value, bool reverse) const
{
QModelIndex find(const QModelIndex& start, const QString& value, bool reverse) const
{
QModelIndex parentIndex = parent(start); QModelIndex parentIndex = parent(start);
auto compare = [this, start, parentIndex, value](int r) -> QModelIndex { auto compare = [this, start, parentIndex, value](int r) -> QModelIndex {
QModelIndex idx = index(r, start.column(), parentIndex); QModelIndex idx = index(r, start.column(), parentIndex);
@ -131,11 +126,7 @@ class LogFormatProxyModel : public QIdentityProxyModel {
} }
} }
return QModelIndex(); return QModelIndex();
} }
private:
QFont m_font;
};
LogPage::LogPage(InstancePtr instance, QWidget* parent) : QWidget(parent), ui(new Ui::LogPage), m_instance(instance) LogPage::LogPage(InstancePtr instance, QWidget* parent) : QWidget(parent), ui(new Ui::LogPage), m_instance(instance)
{ {

View File

@ -35,6 +35,7 @@
#pragma once #pragma once
#include <QIdentityProxyModel>
#include <QWidget> #include <QWidget>
#include <Application.h> #include <Application.h>
@ -46,7 +47,18 @@ namespace Ui {
class LogPage; class LogPage;
} }
class QTextCharFormat; class QTextCharFormat;
class LogFormatProxyModel;
class LogFormatProxyModel : public QIdentityProxyModel {
public:
LogFormatProxyModel(QObject* parent = nullptr) : QIdentityProxyModel(parent) {}
QVariant data(const QModelIndex& index, int role) const override;
QFont getFont() const { return m_font; }
void setFont(QFont font) { m_font = font; }
QModelIndex find(const QModelIndex& start, const QString& value, bool reverse) const;
private:
QFont m_font;
};
class LogPage : public QWidget, public BasePage { class LogPage : public QWidget, public BasePage {
Q_OBJECT Q_OBJECT

View File

@ -46,12 +46,38 @@
#include <QShortcut> #include <QShortcut>
#include "RecursiveFileSystemWatcher.h" #include "RecursiveFileSystemWatcher.h"
OtherLogsPage::OtherLogsPage(QString path, IPathMatcher::Ptr fileFilter, QWidget* parent) OtherLogsPage::OtherLogsPage(InstancePtr instance, IPathMatcher::Ptr fileFilter, QWidget* parent)
: QWidget(parent), ui(new Ui::OtherLogsPage), m_path(path), m_fileFilter(fileFilter), m_watcher(new RecursiveFileSystemWatcher(this)) : QWidget(parent)
, ui(new Ui::OtherLogsPage)
, m_instance(instance)
, m_path(instance->getLogFileRoot())
, m_fileFilter(fileFilter)
, m_watcher(new RecursiveFileSystemWatcher(this))
, m_model(new LogModel())
{ {
ui->setupUi(this); ui->setupUi(this);
ui->tabWidget->tabBar()->hide(); ui->tabWidget->tabBar()->hide();
m_proxy = new LogFormatProxyModel(this);
// set up fonts in the log proxy
{
QString fontFamily = APPLICATION->settings()->get("ConsoleFont").toString();
bool conversionOk = false;
int fontSize = APPLICATION->settings()->get("ConsoleFontSize").toInt(&conversionOk);
if (!conversionOk) {
fontSize = 11;
}
m_proxy->setFont(QFont(fontFamily, fontSize));
}
ui->text->setModel(m_proxy);
m_model->setMaxLines(m_instance->getConsoleMaxLines());
m_model->setStopOnOverflow(m_instance->shouldStopOnConsoleOverflow());
m_model->setOverflowMessage(tr("Cannot display this log since the log length surpassed %1 lines.").arg(m_model->getMaxLines()));
m_proxy->setSourceModel(m_model.get());
m_watcher->setMatcher(fileFilter); m_watcher->setMatcher(fileFilter);
m_watcher->setRootDir(QDir::current().absoluteFilePath(m_path)); m_watcher->setRootDir(QDir::current().absoluteFilePath(m_path));
@ -139,14 +165,8 @@ void OtherLogsPage::on_btnReload_clicked()
QMessageBox::critical(this, tr("Error"), tr("Unable to open %1 for reading: %2").arg(m_currentFile, file.errorString())); QMessageBox::critical(this, tr("Error"), tr("Unable to open %1 for reading: %2").arg(m_currentFile, file.errorString()));
} else { } else {
auto setPlainText = [this](const QString& text) { auto setPlainText = [this](const QString& text) {
QString fontFamily = APPLICATION->settings()->get("ConsoleFont").toString();
bool conversionOk = false;
int fontSize = APPLICATION->settings()->get("ConsoleFontSize").toInt(&conversionOk);
if (!conversionOk) {
fontSize = 11;
}
QTextDocument* doc = ui->text->document(); QTextDocument* doc = ui->text->document();
doc->setDefaultFont(QFont(fontFamily, fontSize)); doc->setDefaultFont(m_proxy->getFont());
ui->text->setPlainText(text); ui->text->setPlainText(text);
}; };
auto showTooBig = [setPlainText, &file]() { auto showTooBig = [setPlainText, &file]() {
@ -173,7 +193,32 @@ void OtherLogsPage::on_btnReload_clicked()
showTooBig(); showTooBig();
return; return;
} }
// If the file is not too big for display, but too slow for syntax highlighting, just show content as plain text
if (content.size() >= 10000000ll || content.isEmpty()) {
setPlainText(content); setPlainText(content);
return;
}
// Try to determine a level for each line
if (content.back() == '\n')
content = content.removeLast();
for (auto& line : content.split('\n')) {
MessageLevel::Enum level = MessageLevel::Unknown;
// if the launcher part set a log level, use it
auto innerLevel = MessageLevel::fromLine(line);
if (innerLevel != MessageLevel::Unknown) {
level = innerLevel;
}
// If the level is still undetermined, guess level
if (level == MessageLevel::StdErr || level == MessageLevel::StdOut || level == MessageLevel::Unknown) {
level = m_instance->guessLevel(line, level);
}
m_model->append(level, line);
}
} }
} }
@ -273,27 +318,21 @@ void OtherLogsPage::setControlsEnabled(const bool enabled)
ui->btnClean->setEnabled(enabled); ui->btnClean->setEnabled(enabled);
} }
// FIXME: HACK, use LogView instead?
static void findNext(QPlainTextEdit* _this, const QString& what, bool reverse)
{
_this->find(what, reverse ? QTextDocument::FindFlag::FindBackward : QTextDocument::FindFlag(0));
}
void OtherLogsPage::on_findButton_clicked() void OtherLogsPage::on_findButton_clicked()
{ {
auto modifiers = QApplication::keyboardModifiers(); auto modifiers = QApplication::keyboardModifiers();
bool reverse = modifiers & Qt::ShiftModifier; bool reverse = modifiers & Qt::ShiftModifier;
findNext(ui->text, ui->searchBar->text(), reverse); ui->text->findNext(ui->searchBar->text(), reverse);
} }
void OtherLogsPage::findNextActivated() void OtherLogsPage::findNextActivated()
{ {
findNext(ui->text, ui->searchBar->text(), false); ui->text->findNext(ui->searchBar->text(), false);
} }
void OtherLogsPage::findPreviousActivated() void OtherLogsPage::findPreviousActivated()
{ {
findNext(ui->text, ui->searchBar->text(), true); ui->text->findNext(ui->searchBar->text(), true);
} }
void OtherLogsPage::findActivated() void OtherLogsPage::findActivated()

View File

@ -39,6 +39,7 @@
#include <Application.h> #include <Application.h>
#include <pathmatcher/IPathMatcher.h> #include <pathmatcher/IPathMatcher.h>
#include "LogPage.h"
#include "ui/pages/BasePage.h" #include "ui/pages/BasePage.h"
namespace Ui { namespace Ui {
@ -51,7 +52,7 @@ class OtherLogsPage : public QWidget, public BasePage {
Q_OBJECT Q_OBJECT
public: public:
explicit OtherLogsPage(QString path, IPathMatcher::Ptr fileFilter, QWidget* parent = 0); explicit OtherLogsPage(InstancePtr instance, IPathMatcher::Ptr fileFilter, QWidget* parent = 0);
~OtherLogsPage(); ~OtherLogsPage();
QString id() const override { return "logs"; } QString id() const override { return "logs"; }
@ -82,8 +83,12 @@ class OtherLogsPage : public QWidget, public BasePage {
private: private:
Ui::OtherLogsPage* ui; Ui::OtherLogsPage* ui;
InstancePtr m_instance;
QString m_path; QString m_path;
QString m_currentFile; QString m_currentFile;
IPathMatcher::Ptr m_fileFilter; IPathMatcher::Ptr m_fileFilter;
RecursiveFileSystemWatcher* m_watcher; RecursiveFileSystemWatcher* m_watcher;
LogFormatProxyModel* m_proxy;
shared_qobject_ptr<LogModel> m_model;
}; };

View File

@ -44,16 +44,25 @@
</widget> </widget>
</item> </item>
<item row="1" column="0" colspan="4"> <item row="1" column="0" colspan="4">
<widget class="QPlainTextEdit" name="text"> <widget class="LogView" name="text">
<property name="enabled"> <property name="enabled">
<bool>false</bool> <bool>false</bool>
</property> </property>
<property name="undoRedoEnabled">
<bool>false</bool>
</property>
<property name="readOnly"> <property name="readOnly">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="plainText">
<string notr="true"/>
</property>
<property name="textInteractionFlags"> <property name="textInteractionFlags">
<set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set> <set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property> </property>
<property name="centerOnScroll">
<bool>false</bool>
</property>
</widget> </widget>
</item> </item>
<item row="0" column="0" colspan="4"> <item row="0" column="0" colspan="4">
@ -130,6 +139,13 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>LogView</class>
<extends>QPlainTextEdit</extends>
<header>ui/widgets/LogView.h</header>
</customwidget>
</customwidgets>
<tabstops> <tabstops>
<tabstop>tabWidget</tabstop> <tabstop>tabWidget</tabstop>
<tabstop>selectLogBox</tabstop> <tabstop>selectLogBox</tabstop>