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*
# Clangd
/compile_commands.json
/.cache
# 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 <chrono>
#include <format>
#include <vector>
static std::ofstream logStream;
static std::vector<Logger::LogListener> logListeners;
std::string Logger::currentLogDir = "NULL";
void Logger::init() {
@ -38,7 +40,15 @@ void Logger::log(std::string message, Logger::LogLevel logLevel) {
logStream << formattedLogLine << std::endl;
printf("%s\n", formattedLogLine.c_str());
for (Logger::LogListener listener : logListeners) {
listener(logLevel, message);
}
if (logLevel == Logger::LogLevel::FATAL_ERROR) {
displayErrorMessage(message);
}
}
void Logger::addLogListener(Logger::LogListener listener) {
logListeners.push_back(listener);
}

View file

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

View file

@ -16,6 +16,7 @@
#include "datatypes/cframe.h"
#include "editorcommon.h"
#include "logger.h"
#include "mainwindow.h"
#include "objects/handles.h"
#include "physics/util.h"
@ -280,6 +281,13 @@ void MainGLWidget::keyPressEvent(QKeyEvent* evt) {
}));
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) {

View file

@ -10,14 +10,17 @@
#include <QWidget>
#include <QTreeView>
#include <QAbstractItemView>
#include <functional>
#include <memory>
#include <optional>
#include <qglobal.h>
#include <qnamespace.h>
#include <qwindowdefs.h>
#include <sstream>
#include "common.h"
#include "editorcommon.h"
#include "logger.h"
#include "objects/base/instance.h"
#include "objects/datamodel.h"
#include "objects/handles.h"
@ -41,6 +44,19 @@ MainWindow::MainWindow(QWidget *parent)
timer.start(33, this);
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();
connect(ui->actionToolSelect, &QAction::triggered, this, [&]() { selectedTool = SelectedTool::SELECT; updateToolbars(); });
@ -234,6 +250,17 @@ MainWindow::MainWindow(QWidget *parent)
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();
void MainWindow::timerEvent(QTimerEvent* evt) {
if (evt->timerId() != timer.timerId()) {

View file

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

View file

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>1027</width>
<height>600</height>
<height>750</height>
</rect>
</property>
<property name="windowTitle">
@ -48,11 +48,19 @@
<property name="title">
<string>File</string>
</property>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
<addaction name="actionSave"/>
</widget>
<widget class="QMenu" name="menuEdit">
<property name="title">
<string>Edit</string>
</property>
<addaction name="actionCopy"/>
<addaction name="actionCut"/>
<addaction name="actionPaste"/>
<addaction name="actionPasteInto"/>
<addaction name="actionDelete"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuEdit"/>
@ -127,6 +135,21 @@
<addaction name="separator"/>
<addaction name="actionToggleSimulation"/>
</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">
<property name="icon">
<iconset>