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
Fixes KDAB#26
  • Loading branch information
smnppKDAB committed Aug 29, 2024
1 parent 223f682 commit 7b106d9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 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 @@ -576,6 +578,8 @@ void ScriptDialogItem::createProperties(QWidget *dialogWidget)
});
m_data->addProperty((widget->objectName() + "Syntax").toLocal8Bit(), "QString", QMetaType::QString,
QString());
m_data->addProperty((widget->objectName() + "DisplayLogs").toLocal8Bit(), "QString", QMetaType::QString,
QString());
}
}

Expand Down Expand Up @@ -637,6 +641,14 @@ void ScriptDialogItem::changeValue(const QString &key, const QVariant &value)
return;
}

// It may be a log panel
if (key.endsWith("DisplayLogs")) {
if (auto logOutput = findChild<QPlainTextEdit *>(key.left(key.length() - 11))) {
displayLogs(logOutput, value.toString());
return;
}
}

if (!widget) {
spdlog::warn("No widget found for the key '{}'.", key.toStdString());
} else {
Expand All @@ -645,6 +657,29 @@ void ScriptDialogItem::changeValue(const QString &key, const QVariant &value)
}
}

void ScriptDialogItem::displayLogs(QPlainTextEdit *logOutput, const QString &level)
{
if (logOutput) {
logOutput->setReadOnly(true);

new LogHighlighter(logOutput->document());

auto logger = spdlog::default_logger();

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

logger->sinks().push_back(std::make_shared<spdlog::sinks::qt_sink_mt>(logOutput, "appendPlainText"));
for (auto &sink : logger->sinks()) {
sink->set_level(logLevel);
}
}
return;
}

void ScriptDialogItem::applySyntaxHighlighting(QTextDocument *document, const QString &syntax)
{
if (!syntax.isEmpty()) {
Expand Down
2 changes: 2 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 @@ -90,6 +91,7 @@ public slots:
static void clearChildren(QQmlListProperty<QObject> *list);

void applySyntaxHighlighting(QTextDocument *document, const QString &syntax);
void displayLogs(QPlainTextEdit *logOutput, const QString &level);

private:
DynamicObject *m_data;
Expand Down

0 comments on commit 7b106d9

Please sign in to comment.