Shallow search and lazy loading for Other Logs page

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad 2025-04-18 19:52:30 +01:00
parent a39edb3b59
commit d5db974008
No known key found for this signature in database
GPG Key ID: 5E39D70B4C93C38E
7 changed files with 78 additions and 54 deletions

View File

@ -198,15 +198,10 @@ class BaseInstance : public QObject, public std::enable_shared_from_this<BaseIns
virtual QProcessEnvironment createEnvironment() = 0;
virtual QProcessEnvironment createLaunchEnvironment() = 0;
/*!
* Returns a matcher that can maps relative paths within the instance to whether they are 'log files'
*/
virtual IPathMatcher::Ptr getLogFileMatcher() = 0;
/*!
* Returns the root folder to use for looking up log files
*/
virtual QString getLogFileRoot() = 0;
virtual QStringList getLogFileSearchPaths() = 0;
virtual QString getStatusbarDescription() = 0;

View File

@ -44,10 +44,7 @@ class InstancePageProvider : protected QObject, public BasePageProvider {
// values.append(new GameOptionsPage(onesix.get()));
values.append(new ScreenshotsPage(FS::PathCombine(onesix->gameRoot(), "screenshots")));
values.append(new InstanceSettingsPage(onesix));
auto logMatcher = inst->getLogFileMatcher();
if (logMatcher) {
values.append(new OtherLogsPage(inst, logMatcher));
}
values.append(new OtherLogsPage(inst));
return values;
}

View File

@ -35,6 +35,7 @@
*/
#pragma once
#include <qglobal.h>
#include "BaseInstance.h"
#include "launch/LaunchTask.h"
@ -57,8 +58,7 @@ class NullInstance : public BaseInstance {
QProcessEnvironment createEnvironment() override { return QProcessEnvironment(); }
QProcessEnvironment createLaunchEnvironment() override { return QProcessEnvironment(); }
QMap<QString, QString> getVariables() override { return QMap<QString, QString>(); }
IPathMatcher::Ptr getLogFileMatcher() override { return nullptr; }
QString getLogFileRoot() override { return instanceRoot(); }
QStringList getLogFileSearchPaths() override { return {}; }
QString typeName() const override { return "Null"; }
bool canExport() const override { return false; }
bool canEdit() const override { return false; }

View File

@ -1055,19 +1055,9 @@ MessageLevel::Enum MinecraftInstance::guessLevel(const QString& line, MessageLev
return level;
}
IPathMatcher::Ptr MinecraftInstance::getLogFileMatcher()
QStringList MinecraftInstance::getLogFileSearchPaths()
{
auto combined = std::make_shared<MultiMatcher>();
combined->add(std::make_shared<RegexpMatcher>(".*\\.log(\\.[0-9]*)?(\\.gz)?$"));
combined->add(std::make_shared<RegexpMatcher>("crash-.*\\.txt"));
combined->add(std::make_shared<RegexpMatcher>("IDMap dump.*\\.txt$"));
combined->add(std::make_shared<RegexpMatcher>("ModLoader\\.txt(\\..*)?$"));
return combined;
}
QString MinecraftInstance::getLogFileRoot()
{
return gameRoot();
return { FS::PathCombine(gameRoot(), "logs"), FS::PathCombine(gameRoot(), "crash-reports"), gameRoot() };
}
QString MinecraftInstance::getStatusbarDescription()

View File

@ -142,9 +142,7 @@ class MinecraftInstance : public BaseInstance {
/// guess log level from a line of minecraft log
MessageLevel::Enum guessLevel(const QString& line, MessageLevel::Enum level) override;
IPathMatcher::Ptr getLogFileMatcher() override;
QString getLogFileRoot() override;
QStringList getLogFileSearchPaths() override;
QString getStatusbarDescription() override;

View File

@ -43,16 +43,18 @@
#include <FileSystem.h>
#include <GZip.h>
#include <qdir.h>
#include <qdiriterator.h>
#include <qfilesystemwatcher.h>
#include <qurl.h>
#include <QShortcut>
#include "RecursiveFileSystemWatcher.h"
OtherLogsPage::OtherLogsPage(InstancePtr instance, IPathMatcher::Ptr fileFilter, QWidget* parent)
OtherLogsPage::OtherLogsPage(InstancePtr instance, QWidget* parent)
: QWidget(parent)
, ui(new Ui::OtherLogsPage)
, m_instance(instance)
, m_path(instance->getLogFileRoot())
, m_fileFilter(fileFilter)
, m_watcher(new RecursiveFileSystemWatcher(this))
, m_basePath(instance->gameRoot())
, m_logSearchPaths(instance->getLogFileSearchPaths())
, m_model(new LogModel(this))
{
ui->setupUi(this);
@ -78,11 +80,7 @@ OtherLogsPage::OtherLogsPage(InstancePtr instance, IPathMatcher::Ptr fileFilter,
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->setRootDir(QDir::current().absoluteFilePath(m_path));
connect(m_watcher, &RecursiveFileSystemWatcher::filesChanged, this, &OtherLogsPage::populateSelectLogBox);
populateSelectLogBox();
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &OtherLogsPage::populateSelectLogBox);
auto findShortcut = new QShortcut(QKeySequence(QKeySequence::Find), this);
connect(findShortcut, &QShortcut::activated, this, &OtherLogsPage::findActivated);
@ -108,22 +106,39 @@ void OtherLogsPage::retranslate()
void OtherLogsPage::openedImpl()
{
m_watcher->enable();
const QStringList failedPaths = m_watcher.addPaths(m_logSearchPaths);
for (const QString& path : m_logSearchPaths) {
if (failedPaths.contains(path))
qDebug() << "Failed to start watching" << path;
else
qDebug() << "Started watching" << path;
}
populateSelectLogBox();
}
void OtherLogsPage::closedImpl()
{
m_watcher->disable();
const QStringList failedPaths = m_watcher.removePaths(m_logSearchPaths);
for (const QString& path : m_logSearchPaths) {
if (failedPaths.contains(path))
qDebug() << "Failed to stop watching" << path;
else
qDebug() << "Stopped watching" << path;
}
}
void OtherLogsPage::populateSelectLogBox()
{
QString prevCurrentFile = m_currentFile;
ui->selectLogBox->clear();
ui->selectLogBox->addItems(m_watcher->files());
if (m_currentFile.isEmpty()) {
setControlsEnabled(false);
ui->selectLogBox->setCurrentIndex(-1);
} else {
const int index = ui->selectLogBox->findText(m_currentFile);
ui->selectLogBox->addItems(getPaths());
if (!prevCurrentFile.isEmpty()) {
const int index = ui->selectLogBox->findText(prevCurrentFile);
if (index != -1) {
ui->selectLogBox->setCurrentIndex(index);
setControlsEnabled(true);
@ -140,7 +155,7 @@ void OtherLogsPage::on_selectLogBox_currentIndexChanged(const int index)
file = ui->selectLogBox->itemText(index);
}
if (file.isEmpty() || !QFile::exists(FS::PathCombine(m_path, file))) {
if (file.isEmpty() || !QFile::exists(FS::PathCombine(m_basePath, file))) {
m_currentFile = QString();
ui->text->clear();
setControlsEnabled(false);
@ -199,7 +214,7 @@ void OtherLogsPage::on_btnReload_clicked()
setControlsEnabled(false);
return;
}
QFile file(FS::PathCombine(m_path, m_currentFile));
QFile file(FS::PathCombine(m_basePath, m_currentFile));
if (!file.open(QFile::ReadOnly)) {
setControlsEnabled(false);
ui->btnReload->setEnabled(true); // allow reload
@ -284,7 +299,7 @@ void OtherLogsPage::on_btnDelete_clicked()
QMessageBox::Yes, QMessageBox::No) == QMessageBox::No) {
return;
}
QFile file(FS::PathCombine(m_path, m_currentFile));
QFile file(FS::PathCombine(m_basePath, m_currentFile));
if (FS::trash(file.fileName())) {
return;
@ -297,7 +312,7 @@ void OtherLogsPage::on_btnDelete_clicked()
void OtherLogsPage::on_btnClean_clicked()
{
auto toDelete = m_watcher->files();
auto toDelete = getPaths();
if (toDelete.isEmpty()) {
return;
}
@ -320,7 +335,9 @@ void OtherLogsPage::on_btnClean_clicked()
}
QStringList failed;
for (auto item : toDelete) {
QFile file(FS::PathCombine(m_path, item));
QString absolutePath = FS::PathCombine(m_basePath, item);
QFile file(absolutePath);
qDebug() << "Deleting log" << absolutePath;
if (FS::trash(file.fileName())) {
continue;
}
@ -374,6 +391,28 @@ void OtherLogsPage::setControlsEnabled(const bool enabled)
ui->btnClean->setEnabled(enabled);
}
QStringList OtherLogsPage::getPaths()
{
QDir baseDir(m_basePath);
QStringList result;
for (QString searchPath : m_logSearchPaths) {
QDirIterator iterator(searchPath, QDir::Files | QDir::Readable);
while (iterator.hasNext()) {
const QString name = iterator.next();
if (!name.endsWith(".log") && !name.endsWith(".log.gz"))
continue;
result.append(baseDir.relativeFilePath(name));
}
}
return result;
}
void OtherLogsPage::on_findButton_clicked()
{
auto modifiers = QApplication::keyboardModifiers();

View File

@ -39,6 +39,8 @@
#include <Application.h>
#include <pathmatcher/IPathMatcher.h>
#include <qfilesystemwatcher.h>
#include <qglobal.h>
#include "LogPage.h"
#include "ui/pages/BasePage.h"
@ -52,7 +54,7 @@ class OtherLogsPage : public QWidget, public BasePage {
Q_OBJECT
public:
explicit OtherLogsPage(InstancePtr instance, IPathMatcher::Ptr fileFilter, QWidget* parent = 0);
explicit OtherLogsPage(InstancePtr instance, QWidget* parent = 0);
~OtherLogsPage();
QString id() const override { return "logs"; }
@ -85,13 +87,16 @@ class OtherLogsPage : public QWidget, public BasePage {
private:
void setControlsEnabled(bool enabled);
QStringList getPaths();
private:
Ui::OtherLogsPage* ui;
InstancePtr m_instance;
QString m_path;
/** Path to display log paths relative to. */
QString m_basePath;
QStringList m_logSearchPaths;
QString m_currentFile;
IPathMatcher::Ptr m_fileFilter;
RecursiveFileSystemWatcher* m_watcher;
QFileSystemWatcher m_watcher;
LogFormatProxyModel* m_proxy;
shared_qobject_ptr<LogModel> m_model;