-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warnings and errors viewable in log dialog
- Loading branch information
Matthew Reid
committed
Nov 5, 2024
1 parent
6f275b0
commit 0d2c777
Showing
10 changed files
with
253 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "ErrorLogModel.h" | ||
|
||
#include <assert.h> | ||
#include <boost/log/core.hpp> | ||
#include <boost/log/expressions.hpp> | ||
#include <boost/log/sinks.hpp> | ||
#include <boost/log/trivial.hpp> | ||
|
||
namespace bl = boost::log; | ||
|
||
class LabelLogSink : public bl::sinks::basic_formatted_sink_backend<char, bl::sinks::synchronized_feeding> | ||
{ | ||
public: | ||
LabelLogSink(std::function<void(bl::trivial::severity_level level, const QString&)> fn) : | ||
mFn(fn) | ||
{ | ||
} | ||
|
||
void consume(const bl::record_view& rec, const std::string& str) { | ||
if (auto severityAttribute = rec["Severity"]; severityAttribute) | ||
{ | ||
if (auto level = severityAttribute.extract<boost::log::trivial::severity_level>(); level) | ||
{ | ||
mFn(*level, QString::fromStdString(str)); | ||
} | ||
} | ||
} | ||
|
||
private: | ||
std::function<void(bl::trivial::severity_level level, const QString&)> mFn; | ||
}; | ||
|
||
ErrorLogModel::ErrorLogModel(QObject* parent) : | ||
QObject(parent) | ||
{ | ||
} | ||
|
||
void ErrorLogModel::append(const Item& item) | ||
{ | ||
mItems.push_back(item); | ||
Q_EMIT itemAppended(item); | ||
} | ||
|
||
void ErrorLogModel::clear() | ||
{ | ||
mItems.clear(); | ||
Q_EMIT cleared(); | ||
} | ||
|
||
static ErrorLogModel::Severity toErrorLogModelSeverity(bl::trivial::severity_level level) | ||
{ | ||
switch (level) | ||
{ | ||
case bl::trivial::severity_level::warning: | ||
return ErrorLogModel::Severity::Warning; | ||
} | ||
return ErrorLogModel::Severity::Error; | ||
} | ||
|
||
void connectToBoostLogger(QPointer<ErrorLogModel> model) | ||
{ | ||
auto sink = boost::make_shared<LabelLogSink>([model = std::move(model)] (bl::trivial::severity_level level, const QString& message) { | ||
if (model) | ||
{ | ||
ErrorLogModel::Item item; | ||
item.dateTime = QDateTime::currentDateTime(); | ||
item.severity = toErrorLogModelSeverity(level); | ||
item.message = message; | ||
model->append(item); | ||
} | ||
}); | ||
|
||
using sink_t = bl::sinks::synchronous_sink<LabelLogSink>; | ||
auto sinkWrapper = boost::make_shared<sink_t>(sink); | ||
sinkWrapper->set_filter(bl::trivial::severity >= bl::trivial::warning); | ||
bl::core::get()->add_sink(sinkWrapper); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <QDateTime> | ||
#include <QObject> | ||
#include <QPointer> | ||
#include <QString> | ||
|
||
class ErrorLogModel : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
ErrorLogModel(QObject* parent = nullptr); | ||
|
||
enum class Severity | ||
{ | ||
Warning, | ||
Error | ||
}; | ||
|
||
struct Item | ||
{ | ||
QDateTime dateTime; | ||
Severity severity; | ||
QString message; | ||
}; | ||
|
||
void append(const Item& item); | ||
void clear(); | ||
|
||
const std::vector<Item>& getItems() const { return mItems; } | ||
|
||
|
||
Q_SIGNAL void itemAppended(const Item& item); | ||
Q_SIGNAL void cleared(); | ||
|
||
private: | ||
std::vector<Item> mItems; | ||
}; | ||
|
||
void connectToBoostLogger(QPointer<ErrorLogModel> model); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "ErrorLogWidget.h" | ||
|
||
#include <QDateTime> | ||
#include <QHeaderView> | ||
#include <QPushButton> | ||
#include <QTableWidget> | ||
#include <QVBoxLayout> | ||
|
||
ErrorLogWidget::ErrorLogWidget(ErrorLogModel* model, QWidget* parent) : | ||
QWidget(parent) | ||
{ | ||
// Create the table widget with 3 columns | ||
mTableWidget = new QTableWidget(this); | ||
mTableWidget->setColumnCount(3); | ||
mTableWidget->setHorizontalHeaderLabels({"Time", "Severity", "Message"}); | ||
mTableWidget->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch); | ||
mTableWidget->setSortingEnabled(true); // Enable sorting by clicking column headers | ||
mTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); // Make the table non-editable | ||
|
||
// Create the "Clear" button | ||
QPushButton* clearButton = new QPushButton("Clear", this); | ||
connect(clearButton, &QPushButton::clicked, model, [model] { | ||
model->clear(); | ||
}); | ||
|
||
// Layout for the dialog | ||
QVBoxLayout* layout = new QVBoxLayout(this); | ||
layout->addWidget(mTableWidget); | ||
layout->addWidget(clearButton); | ||
|
||
setLayout(layout); | ||
|
||
connect(model, &ErrorLogModel::itemAppended, this, [this] (const ErrorLogModel::Item& item) { | ||
addItemToTable(item); | ||
}); | ||
|
||
connect(model, &ErrorLogModel::cleared, this, [this] { | ||
mTableWidget->setRowCount(0); | ||
}); | ||
|
||
// Add initial items | ||
for (const auto& item : model->getItems()) | ||
{ | ||
addItemToTable(item); | ||
} | ||
} | ||
|
||
static QString toQString(ErrorLogModel::Severity severity) | ||
{ | ||
switch (severity) | ||
{ | ||
case ErrorLogModel::Severity::Warning: | ||
return "Warning"; | ||
case ErrorLogModel::Severity::Error: | ||
return "Error"; | ||
} | ||
return ""; | ||
} | ||
|
||
std::unique_ptr<QTableWidgetItem> createItemWithTooltip(const QString& text) | ||
{ | ||
auto item = std::make_unique<QTableWidgetItem>(text); | ||
item->setToolTip(text); | ||
return item; | ||
} | ||
|
||
void ErrorLogWidget::addItemToTable(const ErrorLogModel::Item& item) | ||
{ | ||
int row = mTableWidget->rowCount(); | ||
mTableWidget->setRowCount(row + 1); | ||
|
||
// Insert time, severity, and message into the new row | ||
mTableWidget->setItem(row, 0, createItemWithTooltip(item.dateTime.toString("yyyy-MM-dd HH:mm:ss")).release()); | ||
mTableWidget->setItem(row, 1, new QTableWidgetItem(toQString(item.severity))); | ||
mTableWidget->setItem(row, 2, createItemWithTooltip(item.message).release()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include "ErrorLogModel.h" | ||
#include <QWidget> | ||
|
||
class QTableWidget; | ||
|
||
class ErrorLogWidget : public QWidget | ||
{ | ||
public: | ||
ErrorLogWidget(ErrorLogModel* model, QWidget* parent = nullptr); | ||
|
||
private: | ||
void addItemToTable(const ErrorLogModel::Item& item); | ||
|
||
private: | ||
QTableWidget* mTableWidget; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#pragma once | ||
|
||
class ErrorLogModel; | ||
class QStatusBar; | ||
|
||
void addErrorLogStatusBar(QStatusBar& bar); | ||
void addErrorLogStatusBar(QStatusBar& bar, ErrorLogModel* model); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.