client logger

move over to qt signals that are processed in mainwindow
This commit is contained in:
2026-02-14 14:32:49 +01:00
parent 16e6100465
commit 8449b6091e
4 changed files with 53 additions and 26 deletions
+1 -1
View File
@@ -84,7 +84,7 @@ if (NOT EMSCRIPTEN)
# Create a library for each .cpp file
add_library(ui_${LIB_NAME} SHARED ${CMAKE_CURRENT_SOURCE_DIR}/simulators/${LIB_NAME}.hpp ${CMAKE_CURRENT_SOURCE_DIR}/simulators/${LIB_NAME}.cpp ${CMAKE_CURRENT_SOURCE_DIR}/simulators/${LIB_NAME}.ui)
target_link_libraries(ui_${LIB_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets magic_enum::magic_enum nlohmann_json::nlohmann_json)
target_include_directories(ui_${LIB_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/ui ${CMAKE_SOURCE_DIR}/external/qwt/src ${CMAKE_SOURCE_DIR}/external/spdlog/include ${PROJECT_BINARY_DIR}/)
target_include_directories(ui_${LIB_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/ui ${CMAKE_SOURCE_DIR}/external/qwt/src ${PROJECT_BINARY_DIR}/)
set_target_properties(
ui_${LIB_NAME}
+6
View File
@@ -7,6 +7,7 @@
#include <QMessageBox>
#include <QThread>
#include <magic_enum/magic_enum.hpp>
#include <spdlog/spdlog.h>
typedef UI_base *(*Get_UI)(Web_socket_wrapper *, QWidget *);
@@ -31,6 +32,11 @@ MainWindow::MainWindow(QUrl ws_url, QString sim, QWidget *parent)
}
auto widget = loaded_ui(m_web_socket.get(), this);
m_sims.insert(std::make_pair(widget->name(), widget));
connect(widget, &UI_base::log_signal, this,
[](const char *filename_in, int line_in, const char *funcname_in, int level, QString msg) {
spdlog::default_logger_raw()->log(spdlog::source_loc{filename_in, line_in, funcname_in},
(spdlog::level::level_enum)level, msg.toStdString());
});
lib.unload();
}
+24 -24
View File
@@ -36,7 +36,7 @@ void UI_base::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);
setWindowTitle(m_name);
SPDLOG_DEBUG("Connecting callbacks");
LOG_DEBUG("Connecting callbacks");
m_first_load = true;
connect(m_web_socket, &Web_socket_wrapper::on_command_cb, this, [=, this](json &j) { on_cmd_cb(j); });
@@ -54,7 +54,7 @@ void UI_base::closeEvent(QCloseEvent *event)
QWidget::closeEvent(event);
if (m_open == true)
{
SPDLOG_DEBUG("Disconnecting callbacks");
LOG_DEBUG("Disconnecting callbacks");
m_timer_update->stop();
disconnect(m_web_socket, nullptr, nullptr, nullptr);
m_open = false;
@@ -109,7 +109,7 @@ void UI_base::UI_item_parser(json &input)
auto widget = id_to_ui(ui_item.at("id"));
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)ui_item.at("id")).toStdString());
LOG_WARN(QString("id_to_ui returned null on %0").arg((size_t)ui_item.at("id")));
return;
}
@@ -216,7 +216,7 @@ void UI_base::process_ui_label(json &ui_item, QWidget *widget)
auto label = qobject_cast<QLabel *>(widget);
if (label == nullptr)
{
SPDLOG_WARN("widget is not of type QLabel");
LOG_WARN("widget is not of type QLabel");
return;
}
@@ -247,7 +247,7 @@ void UI_base::process_ui_slider(json &ui_item, QWidget *widget)
auto slider = qobject_cast<QwtSlider *>(widget);
if (slider == nullptr)
{
SPDLOG_WARN("widget is not of type QwtSlider");
LOG_WARN("widget is not of type QwtSlider");
return;
}
@@ -286,7 +286,7 @@ void UI_base::process_ui_dial(json &ui_item, QWidget *widget)
auto dial = qobject_cast<QwtDial *>(widget);
if (dial == nullptr)
{
SPDLOG_WARN("widget is not of type QwtDial");
LOG_WARN("widget is not of type QwtDial");
return;
}
@@ -318,7 +318,7 @@ void UI_base::process_ui_thermo(json &ui_item, QWidget *widget)
auto thermo = qobject_cast<QwtThermo *>(widget);
if (thermo == nullptr)
{
SPDLOG_WARN("widget is not of type QwtThermo");
LOG_WARN("widget is not of type QwtThermo");
return;
}
@@ -348,7 +348,7 @@ void UI_base::process_ui_textbox(json &ui_item, QWidget *widget)
auto line_edit = qobject_cast<QLineEdit *>(widget);
if (line_edit == nullptr)
{
SPDLOG_WARN("widget is not of type QLineEdit");
LOG_WARN("widget is not of type QLineEdit");
return;
}
@@ -381,7 +381,7 @@ void UI_base::process_ui_combobox(json &ui_item, QWidget *widget)
auto combobox = qobject_cast<QComboBox *>(widget);
if (combobox == nullptr)
{
SPDLOG_WARN("widget is not of type QComboBox");
LOG_WARN("widget is not of type QComboBox");
return;
}
@@ -413,7 +413,7 @@ void UI_base::process_ui_combobox(json &ui_item, QWidget *widget)
void UI_base::process_ui_radiobutton(json &ui_item, QWidget *widget)
{
SPDLOG_CRITICAL("Not implemented");
LOG_CRITICAL("Not implemented");
}
void UI_base::process_ui_checkbox(json &ui_item, QWidget *widget)
@@ -421,7 +421,7 @@ void UI_base::process_ui_checkbox(json &ui_item, QWidget *widget)
auto checkbox = qobject_cast<QCheckBox *>(widget);
if (checkbox == nullptr)
{
SPDLOG_WARN("widget is not of type QCheckbox");
LOG_WARN("widget is not of type QCheckbox");
return;
}
@@ -487,7 +487,7 @@ void UI_base::process_ui_button(json &ui_item, QWidget *widget)
auto button = qobject_cast<QPushButton *>(widget);
if (button == nullptr)
{
SPDLOG_WARN("widget is not of type QPushButton");
LOG_WARN("widget is not of type QPushButton");
return;
}
@@ -547,7 +547,7 @@ void UI_base::process_ui_led(json &ui_item, QWidget *widget)
auto led = qobject_cast<Led *>(widget);
if (led == nullptr)
{
SPDLOG_WARN("widget is not of type led");
LOG_WARN("widget is not of type led");
return;
}
@@ -601,7 +601,7 @@ void UI_base::process_ui_spinbox(json &ui_item, QWidget *widget)
auto spinbox = qobject_cast<QSpinBox *>(widget);
if (spinbox == nullptr)
{
SPDLOG_WARN("widget is not of type QSpinBox");
LOG_WARN("widget is not of type QSpinBox");
return;
}
@@ -640,7 +640,7 @@ void UI_base::process_ui_double_spinbox(json &ui_item, QWidget *widget)
auto spinbox = qobject_cast<QDoubleSpinBox *>(widget);
if (spinbox == nullptr)
{
SPDLOG_WARN("widget is not of type QDoubleSpinBox");
LOG_WARN("widget is not of type QDoubleSpinBox");
return;
}
@@ -679,7 +679,7 @@ void UI_base::process_ui_hex_spinbox(json &ui_item, QWidget *widget)
auto spinbox = qobject_cast<HexSpinBox *>(widget);
if (spinbox == nullptr)
{
SPDLOG_WARN("widget is not of type HexSpinBox");
LOG_WARN("widget is not of type HexSpinBox");
return;
}
@@ -718,13 +718,13 @@ void UI_base::process_ui_tab_widget(json &ui_item, QWidget *widget)
auto tab_widget = qobject_cast<QTabWidget *>(widget);
if (tab_widget == nullptr)
{
SPDLOG_WARN("widget is not of type QTabWidget");
LOG_WARN("widget is not of type QTabWidget");
return;
}
if (tab_widget->count() != ui_item.at("tab_names").size())
{
SPDLOG_ERROR("QTabWidget has not the same amount of tabs as the server");
LOG_ERROR("QTabWidget has not the same amount of tabs as the server");
return;
}
@@ -765,13 +765,13 @@ void UI_base::process_ui_stacked_widget(json &ui_item, QWidget *widget)
auto stacked_widget = qobject_cast<QStackedWidget *>(widget);
if (stacked_widget == nullptr)
{
SPDLOG_WARN("widget is not of type QStackedWidget");
LOG_WARN("widget is not of type QStackedWidget");
return;
}
if (stacked_widget->count() != ui_item.at("tab_count"))
{
SPDLOG_ERROR("QStackedWidget has not the same amount of tabs as the server");
LOG_ERROR("QStackedWidget has not the same amount of tabs as the server");
return;
}
@@ -799,7 +799,7 @@ void UI_base::process_ui_status_bar(json &ui_item, QWidget *widget)
auto status_bar = qobject_cast<QStatusBar *>(widget);
if (status_bar == nullptr)
{
SPDLOG_WARN("widget is not of type QStatusBar");
LOG_WARN("widget is not of type QStatusBar");
return;
}
@@ -814,7 +814,7 @@ void UI_base::process_ui_plot(json &ui_item, QWidget *widget)
auto plot = qobject_cast<Plot_wrapper *>(widget);
if (plot == nullptr)
{
SPDLOG_WARN("widget is not of type QwtPlot");
LOG_WARN("widget is not of type QwtPlot");
return;
}
@@ -876,7 +876,7 @@ void UI_base::process_ui_table(json &ui_item, QWidget *widget)
auto table = qobject_cast<QTableWidget *>(widget);
if (table == nullptr)
{
SPDLOG_WARN("widget is not of type QTableWidget");
LOG_WARN("widget is not of type QTableWidget");
return;
}
@@ -947,7 +947,7 @@ void UI_base::process_ui_can(json &ui_item, QWidget *widget)
auto can_ui = qobject_cast<Can_Transceive *>(widget);
if (can_ui == nullptr)
{
SPDLOG_WARN("widget is not of type led");
LOG_WARN("widget is not of type led");
return;
}
+22 -1
View File
@@ -1,5 +1,4 @@
#pragma once
#include "spdlog/spdlog.h"
#include <QMainWindow>
#include <QWidget>
#include <nlohmann/json.hpp>
@@ -18,6 +17,14 @@ using json = nlohmann::json;
return new client(api, parent); \
}
#define LOG_SRC_LOC(level, msg) emit log_signal(__FILE__, __LINE__, __FUNCTION__, (int)level, msg)
#define LOG_TRACE(msg) LOG_SRC_LOC(Log_level_enum::trace, msg)
#define LOG_DEBUG(msg) LOG_SRC_LOC(Log_level_enum::debug, msg)
#define LOG_INFO(msg) LOG_SRC_LOC(Log_level_enum::info, msg)
#define LOG_WARN(msg) LOG_SRC_LOC(Log_level_enum::warn, msg)
#define LOG_ERROR(msg) LOG_SRC_LOC(Log_level_enum::err, msg)
#define LOG_CRITICAL(msg) LOG_SRC_LOC(Log_level_enum::critical, msg)
/**
* @brief Base class for client UIs
*/
@@ -25,6 +32,17 @@ class UI_base : public QMainWindow
{
Q_OBJECT
public:
enum class Log_level_enum : int
{
trace,
debug,
info,
warn,
err,
critical,
off,
n_levels
};
/**
* @brief Construct a new ui base object
*
@@ -320,6 +338,9 @@ class UI_base : public QMainWindow
*/
void closeEvent(QCloseEvent *event) override;
signals:
void log_signal(const char *filename_in, int line_in, const char *funcname_in, int level, QString msg);
private:
Web_socket_wrapper *m_web_socket;
QString m_name;