From 53d3fcb2fd62358e4eb7cdc1bfcc1f89955e06aa Mon Sep 17 00:00:00 2001 From: Bob van der Linden Date: Sun, 6 Mar 2022 14:45:48 +0100 Subject: [PATCH] introduce widget that shows logs --- src/libcalamaresui/CMakeLists.txt | 1 + src/libcalamaresui/widgets/LogWidget.cpp | 75 ++++++++++++++++++++++++ src/libcalamaresui/widgets/LogWidget.h | 37 ++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 src/libcalamaresui/widgets/LogWidget.cpp create mode 100644 src/libcalamaresui/widgets/LogWidget.h diff --git a/src/libcalamaresui/CMakeLists.txt b/src/libcalamaresui/CMakeLists.txt index bd2e79f10..48e4c4b4d 100644 --- a/src/libcalamaresui/CMakeLists.txt +++ b/src/libcalamaresui/CMakeLists.txt @@ -30,6 +30,7 @@ set( calamaresui_SOURCES widgets/ErrorDialog.cpp widgets/FixedAspectRatioLabel.cpp widgets/PrettyRadioButton.cpp + widgets/LogWidget.cpp widgets/TranslationFix.cpp widgets/WaitingWidget.cpp ${CMAKE_SOURCE_DIR}/3rdparty/waitingspinnerwidget.cpp diff --git a/src/libcalamaresui/widgets/LogWidget.cpp b/src/libcalamaresui/widgets/LogWidget.cpp new file mode 100644 index 000000000..a4af6c522 --- /dev/null +++ b/src/libcalamaresui/widgets/LogWidget.cpp @@ -0,0 +1,75 @@ +#include "LogWidget.h" +#include +#include "utils/Logger.h" +#include +#include +#include + +namespace Calamares +{ + +LogThread::LogThread(QObject *parent) + : QThread(parent) { + +} + +void LogThread::run() +{ + auto filePath = Logger::logFile(); + + qint64 lastPosition = 0; + + while (!QThread::currentThread()->isInterruptionRequested()) { + auto filePath = Logger::logFile(); + QFile file(filePath); + + qint64 fileSize = file.size(); + // Check whether the file size has changed since last time + // we read the file. + if (lastPosition != fileSize && file.open(QFile::ReadOnly | QFile::Text)) { + + // Start reading at the position we ended up last time we read the file. + file.seek(lastPosition); + + QTextStream in(&file); + auto chunk = in.readAll(); + qint64 newPosition = in.pos(); + + lastPosition = newPosition; + + onLogChunk(chunk); + } + QThread::msleep(100); + } +} + +LogWidget::LogWidget(QWidget *parent) + : QWidget(parent) + , m_text( new QPlainTextEdit ) + , m_log_thread( this ) +{ + auto layout = new QStackedLayout(this); + setLayout(layout); + + m_text->setReadOnly(true); + + QFont monospaceFont("monospace"); + monospaceFont.setStyleHint(QFont::Monospace); + m_text->setFont(monospaceFont); + + layout->addWidget(m_text); + + connect(&m_log_thread, &LogThread::onLogChunk, this, &LogWidget::handleLogChunk); + + m_log_thread.start(); +} + +void +LogWidget::handleLogChunk(const QString &logChunk) +{ + m_text->appendPlainText(logChunk); + m_text->moveCursor(QTextCursor::End); + m_text->ensureCursorVisible(); +} + +} diff --git a/src/libcalamaresui/widgets/LogWidget.h b/src/libcalamaresui/widgets/LogWidget.h new file mode 100644 index 000000000..2def81845 --- /dev/null +++ b/src/libcalamaresui/widgets/LogWidget.h @@ -0,0 +1,37 @@ +#ifndef LIBCALAMARESUI_LOGWIDGET_H +#define LIBCALAMARESUI_LOGWIDGET_H + +#include +#include +#include + +namespace Calamares +{ + +class LogThread : public QThread +{ + Q_OBJECT + + void run() override; + +public: + explicit LogThread(QObject *parent = nullptr); + +signals: + void onLogChunk(const QString &logChunk); +}; + +class LogWidget : public QWidget +{ + Q_OBJECT + + QPlainTextEdit* m_text; + LogThread m_log_thread; +public: + explicit LogWidget(QWidget *parent = nullptr); + + void handleLogChunk(const QString &logChunk); +}; + +} +#endif // LOGWIDGET_H