feat(logging): added output widget

This commit is contained in:
maelstrom 2025-03-21 00:16:27 +01:00
parent b9e35520ad
commit 12116c7c49
8 changed files with 76 additions and 2 deletions

1
.gitignore vendored
View file

@ -7,7 +7,6 @@
/CMakeLists.txt.user* /CMakeLists.txt.user*
# Clangd # Clangd
/compile_commands.json
/.cache /.cache
# Gdb # Gdb

1
compile_commands.json Symbolic link
View file

@ -0,0 +1 @@
./build/compile_commands.json

View file

@ -4,8 +4,10 @@
#include <fstream> #include <fstream>
#include <chrono> #include <chrono>
#include <format> #include <format>
#include <vector>
static std::ofstream logStream; static std::ofstream logStream;
static std::vector<Logger::LogListener> logListeners;
std::string Logger::currentLogDir = "NULL"; std::string Logger::currentLogDir = "NULL";
void Logger::init() { void Logger::init() {
@ -38,7 +40,15 @@ void Logger::log(std::string message, Logger::LogLevel logLevel) {
logStream << formattedLogLine << std::endl; logStream << formattedLogLine << std::endl;
printf("%s\n", formattedLogLine.c_str()); printf("%s\n", formattedLogLine.c_str());
for (Logger::LogListener listener : logListeners) {
listener(logLevel, message);
}
if (logLevel == Logger::LogLevel::FATAL_ERROR) { if (logLevel == Logger::LogLevel::FATAL_ERROR) {
displayErrorMessage(message); displayErrorMessage(message);
} }
} }
void Logger::addLogListener(Logger::LogListener listener) {
logListeners.push_back(listener);
}

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <format> #include <format>
#include <functional>
#include <string> #include <string>
namespace Logger { namespace Logger {
@ -12,10 +13,13 @@ namespace Logger {
FATAL_ERROR, FATAL_ERROR,
}; };
typedef std::function<void(LogLevel logLeve, std::string message)> LogListener;
extern std::string currentLogDir; extern std::string currentLogDir;
void init(); void init();
void finish(); void finish();
void addLogListener(LogListener);
void log(std::string message, LogLevel logLevel); void log(std::string message, LogLevel logLevel);
inline void info(std::string message) { log(message, LogLevel::INFO); } inline void info(std::string message) { log(message, LogLevel::INFO); }

View file

@ -16,6 +16,7 @@
#include "datatypes/cframe.h" #include "datatypes/cframe.h"
#include "editorcommon.h" #include "editorcommon.h"
#include "logger.h"
#include "mainwindow.h" #include "mainwindow.h"
#include "objects/handles.h" #include "objects/handles.h"
#include "physics/util.h" #include "physics/util.h"
@ -280,6 +281,13 @@ void MainGLWidget::keyPressEvent(QKeyEvent* evt) {
})); }));
syncPartPhysics(lastPart); syncPartPhysics(lastPart);
} }
if (evt->key() == Qt::Key_U)
Logger::info("info message");
if (evt->key() == Qt::Key_I)
Logger::warning("warning message");
if (evt->key() == Qt::Key_O)
Logger::error("error message");
} }
void MainGLWidget::keyReleaseEvent(QKeyEvent* evt) { void MainGLWidget::keyReleaseEvent(QKeyEvent* evt) {

View file

@ -10,14 +10,17 @@
#include <QWidget> #include <QWidget>
#include <QTreeView> #include <QTreeView>
#include <QAbstractItemView> #include <QAbstractItemView>
#include <functional>
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <qglobal.h> #include <qglobal.h>
#include <qnamespace.h>
#include <qwindowdefs.h> #include <qwindowdefs.h>
#include <sstream> #include <sstream>
#include "common.h" #include "common.h"
#include "editorcommon.h" #include "editorcommon.h"
#include "logger.h"
#include "objects/base/instance.h" #include "objects/base/instance.h"
#include "objects/datamodel.h" #include "objects/datamodel.h"
#include "objects/handles.h" #include "objects/handles.h"
@ -41,6 +44,19 @@ MainWindow::MainWindow(QWidget *parent)
timer.start(33, this); timer.start(33, this);
setMouseTracking(true); setMouseTracking(true);
setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
// Logger
Logger::addLogListener(std::bind(&MainWindow::handleLog, this, std::placeholders::_1, std::placeholders::_2));
QFont font("");
font.setStyleHint(QFont::Monospace);
ui->outputTextView->setFont(font);
// Explorer View
ui->explorerView->buildContextMenu(); ui->explorerView->buildContextMenu();
connect(ui->actionToolSelect, &QAction::triggered, this, [&]() { selectedTool = SelectedTool::SELECT; updateToolbars(); }); connect(ui->actionToolSelect, &QAction::triggered, this, [&]() { selectedTool = SelectedTool::SELECT; updateToolbars(); });
@ -234,6 +250,17 @@ MainWindow::MainWindow(QWidget *parent)
syncPartPhysics(ui->mainWidget->lastPart); syncPartPhysics(ui->mainWidget->lastPart);
} }
void MainWindow::handleLog(Logger::LogLevel logLevel, std::string message) {
if (logLevel == Logger::LogLevel::DEBUG) return;
if (logLevel == Logger::LogLevel::INFO)
ui->outputTextView->appendHtml(QString("<p>%1</p>").arg(QString::fromStdString(message)));
if (logLevel == Logger::LogLevel::WARNING)
ui->outputTextView->appendHtml(QString("<p style=\"color:rgb(255, 127, 0); font-weight: bold;\">%1</p>").arg(QString::fromStdString(message)));
if (logLevel == Logger::LogLevel::ERROR || logLevel == Logger::LogLevel::FATAL_ERROR)
ui->outputTextView->appendHtml(QString("<p style=\"color:rgb(255, 0, 0); font-weight: bold;\">%1</p>").arg(QString::fromStdString(message)));
}
static std::chrono::time_point lastTime = std::chrono::steady_clock::now(); static std::chrono::time_point lastTime = std::chrono::steady_clock::now();
void MainWindow::timerEvent(QTimerEvent* evt) { void MainWindow::timerEvent(QTimerEvent* evt) {
if (evt->timerId() != timer.timerId()) { if (evt->timerId() != timer.timerId()) {

View file

@ -1,6 +1,7 @@
#ifndef MAINWINDOW_H #ifndef MAINWINDOW_H
#define MAINWINDOW_H #define MAINWINDOW_H
#include "logger.h"
#include "panes/explorerview.h" #include "panes/explorerview.h"
#include "qbasictimer.h" #include "qbasictimer.h"
#include "qcoreevent.h" #include "qcoreevent.h"
@ -45,6 +46,7 @@ private:
void updateToolbars(); void updateToolbars();
void timerEvent(QTimerEvent*) override; void timerEvent(QTimerEvent*) override;
void handleLog(Logger::LogLevel, std::string);
std::optional<std::string> openFileDialog(QString filter, QString defaultExtension, QFileDialog::AcceptMode acceptMode, QString title = ""); std::optional<std::string> openFileDialog(QString filter, QString defaultExtension, QFileDialog::AcceptMode acceptMode, QString title = "");
}; };

View file

@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1027</width> <width>1027</width>
<height>600</height> <height>750</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -48,11 +48,19 @@
<property name="title"> <property name="title">
<string>File</string> <string>File</string>
</property> </property>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
<addaction name="actionSave"/>
</widget> </widget>
<widget class="QMenu" name="menuEdit"> <widget class="QMenu" name="menuEdit">
<property name="title"> <property name="title">
<string>Edit</string> <string>Edit</string>
</property> </property>
<addaction name="actionCopy"/>
<addaction name="actionCut"/>
<addaction name="actionPaste"/>
<addaction name="actionPasteInto"/>
<addaction name="actionDelete"/>
</widget> </widget>
<addaction name="menuFile"/> <addaction name="menuFile"/>
<addaction name="menuEdit"/> <addaction name="menuEdit"/>
@ -127,6 +135,21 @@
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionToggleSimulation"/> <addaction name="actionToggleSimulation"/>
</widget> </widget>
<widget class="QDockWidget" name="outputWidget">
<property name="windowTitle">
<string>Output</string>
</property>
<attribute name="dockWidgetArea">
<number>8</number>
</attribute>
<widget class="QWidget" name="dockWidgetContents_3">
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QPlainTextEdit" name="outputTextView"/>
</item>
</layout>
</widget>
</widget>
<action name="actionAddPart"> <action name="actionAddPart">
<property name="icon"> <property name="icon">
<iconset> <iconset>