Skip to content

Commit

Permalink
feat: add a way to display log in a ScriptDialog
Browse files Browse the repository at this point in the history
Added a QPlainTextEdit widget to allow real-time log display in the user interface. The actual code did not allow windows users to view logs when launching knut from the command line.

The QPlainTextEdit can be made visible using the qml setDisplayLogs method and taking as parameter the minimum log level that we want to have.

Fixes #26
  • Loading branch information
smnppKDAB committed Aug 30, 2024
1 parent 223f682 commit 191813b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/API/knut/scriptdialog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Knut
||**[firstStep](#firstStep)**(string firstStep)|
||**[nextStep](#nextStep)**(string title)|
||**[runSteps](#runSteps)**(function generator)|
||**[setDisplayLogs](#setDisplayLogs)**(const QString &level)|
||**[setStepCount](#setStepCount)**(int stepCount)|
||**[verify](#verify)**(bool value, string message = {})|

Expand Down Expand Up @@ -171,6 +172,11 @@ function convert() {
}
```

#### <a name="setDisplayLogs"></a>**setDisplayLogs**(const QString &level)

This method enables real-time logging display within the ScriptDialog by configuring a QPlainTextEdit widget as a log output panel.
It filters the logs to show only those that are of the specified log level or higher.

#### <a name="setStepCount"></a>**setStepCount**(int stepCount)

Sets the number of steps to show in the progress bar.
Expand Down
1 change: 1 addition & 0 deletions examples/ex_gui_interactive.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ScriptDialog {
// This can be done using the `stepCount` property of ScriptDialog
// Note: if you don't set the step count, a moving scrollbar will be displayed
setStepCount(5);
setDisplayLogs("warn");

// 3) Initialize the first step using `firstStep` and run the different commands for this step
firstStep("Step 1...");
Expand Down
32 changes: 32 additions & 0 deletions src/core/scriptdialogitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

#include "scriptdialogitem.h"
#include "loghighlighter.h"
#include "scriptdialogitem_p.h"
#include "scriptprogressdialog.h"
#include "scriptrunner.h"
Expand Down Expand Up @@ -44,6 +45,7 @@
#include <QToolButton>
#include <QUiLoader>
#include <QVBoxLayout>
#include <spdlog/sinks/qt_sinks.h>

namespace Core {

Expand Down Expand Up @@ -431,6 +433,36 @@ void ScriptDialogItem::verify(bool value, QString message)
ScriptRunner::verify(this, value, message);
}

/*!
* \qmlmethod ScriptDialog::setDisplayLogs(const QString &level)
* This method enables real-time logging display within the ScriptDialog by configuring a QPlainTextEdit widget as a log
* output panel. It filters the logs to show only those that are of the specified log level or higher.
*/
void ScriptDialogItem::setDisplayLogs(const QString &level)
{
QPlainTextEdit *logPanel = m_progressDialog->setLogsAvailable();
logPanel->setReadOnly(true);

new Core::LogHighlighter(logPanel->document());

const std::vector<const QString> levels = {"trace", "debug", "info", "warn", "error", "critical"};
const auto it = std::find(levels.begin(), levels.end(), level);
const int index = (it != levels.end()) ? std::distance(levels.begin(), it) : 0;

spdlog::level::level_enum logLevel = static_cast<spdlog::level::level_enum>(index);

auto logger = spdlog::default_logger();

auto currentLogLevel = logger->level();
auto logLevelMin = static_cast<spdlog::level::level_enum>(std::min(static_cast<int>(currentLogLevel), index));

logger->set_level(logLevelMin);

auto sink = std::make_shared<spdlog::sinks::qt_sink_mt>(logPanel, "appendPlainText");
sink->set_level(logLevel);
logger->sinks().push_back(sink);
}

void ScriptDialogItem::showProgressDialog()
{
// If there's no interaction or the progress bar is already displayed, do nothing
Expand Down
3 changes: 3 additions & 0 deletions src/core/scriptdialogitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

class ScriptProgressDialog;
class QTextDocument;
class QPlainTextEdit;

namespace KSyntaxHighlighting {
}
Expand Down Expand Up @@ -62,6 +63,8 @@ class ScriptDialogItem : public QDialog
Q_INVOKABLE void compare(const QJSValue &actual, const QJSValue &expected, QString message = {});
Q_INVOKABLE void verify(bool value, QString message = {});

Q_INVOKABLE void setDisplayLogs(const QString &level);

public slots:
void setStepCount(int stepCount);
void done(int code) override;
Expand Down
7 changes: 7 additions & 0 deletions src/core/scriptprogressdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ScriptProgressDialog::ScriptProgressDialog(QWidget *parent)
setAttribute(Qt::WA_DeleteOnClose);

ui->buttonBox->button(QDialogButtonBox::Yes)->setText(tr("Continue"));
ui->logsPanel->hide();
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &ScriptProgressDialog::apply);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &ScriptProgressDialog::abort);
}
Expand Down Expand Up @@ -63,3 +64,9 @@ void ScriptProgressDialog::setReadOnly(bool readOnly)
{
ui->buttonBox->setEnabled(!readOnly);
}

QPlainTextEdit *ScriptProgressDialog::setLogsAvailable()
{
ui->logsPanel->show();
return ui->logsPanel;
}
3 changes: 3 additions & 0 deletions src/core/scriptprogressdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Ui {
class ScriptProgressDialog;
}
class QPlainTextEdit;

class ScriptProgressDialog : public QDialog
{
Expand All @@ -34,6 +35,8 @@ class ScriptProgressDialog : public QDialog

int value() const;

QPlainTextEdit *setLogsAvailable();

signals:
void apply();
void abort();
Expand Down
5 changes: 4 additions & 1 deletion src/core/scriptprogressdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>360</width>
<height>94</height>
<height>189</height>
</rect>
</property>
<property name="minimumSize">
Expand Down Expand Up @@ -48,6 +48,9 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QPlainTextEdit" name="logsPanel"/>
</item>
</layout>
</widget>
<resources>
Expand Down

0 comments on commit 191813b

Please sign in to comment.