From 16e610046580c88e0c11e14b9a34181fa3f6f16d Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Fri, 13 Feb 2026 22:16:25 +0100 Subject: [PATCH] server logger move over to qt signals that are processed in simulators --- server/CMakeLists.txt | 2 +- server/simulator_base.hpp | 22 +++++++++++++++++++++- server/simulators.cpp | 6 ++++++ server/simulators/test.cpp | 4 ++-- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index ecf1f6f..2efc5fd 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -69,7 +69,7 @@ foreach(sim ${sims}) # Create a library for each .cpp file add_library(sim_${LIB_NAME} SHARED ${CMAKE_CURRENT_SOURCE_DIR}/simulators/${LIB_NAME}.hpp ${CMAKE_CURRENT_SOURCE_DIR}/simulators/${LIB_NAME}.cpp) target_link_libraries(sim_${LIB_NAME} PRIVATE Qt::SerialBus magic_enum::magic_enum nlohmann_json::nlohmann_json) - target_include_directories(sim_${LIB_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/extensions ${CMAKE_SOURCE_DIR}/UI ${CMAKE_SOURCE_DIR}/external/color ${CMAKE_SOURCE_DIR}/external/spdlog/include ${CMAKE_SOURCE_DIR}/external/cpp-templates) + target_include_directories(sim_${LIB_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/extensions ${CMAKE_SOURCE_DIR}/UI ${CMAKE_SOURCE_DIR}/external/color ${CMAKE_SOURCE_DIR}/external/cpp-templates) add_custom_command(TARGET sim_${LIB_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $ diff --git a/server/simulator_base.hpp b/server/simulator_base.hpp index 4684dcf..da08774 100644 --- a/server/simulator_base.hpp +++ b/server/simulator_base.hpp @@ -1,5 +1,4 @@ #pragma once -#include "spdlog/spdlog.h" #include #include #include @@ -22,6 +21,14 @@ Q_FORWARD_DECLARE_OBJC_CLASS(UI_item); return new sim(com, 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 Struct holding all communication interfaces * @@ -49,6 +56,17 @@ class Simulator_base : public QObject { Q_OBJECT public: + enum class Log_level_enum : int + { + trace, + debug, + info, + warn, + err, + critical, + off, + n_levels + }; /** * @brief Construct a new Simulator_base object * @@ -171,6 +189,8 @@ class Simulator_base : public QObject */ void sim_changed(); + void log_signal(const char *filename_in, int line_in, const char *funcname_in, int level, QString msg); + protected: /** * @brief The name of the simulator diff --git a/server/simulators.cpp b/server/simulators.cpp index 8600a25..96b8f50 100644 --- a/server/simulators.cpp +++ b/server/simulators.cpp @@ -1,6 +1,7 @@ #include "simulators.hpp" #define INTERNAL #include "simulator_base.hpp" +#include "spdlog/spdlog.h" #include "websocket.hpp" #include #include @@ -153,6 +154,11 @@ void Simulators::switch_simulator(std::string name) } }, Qt::QueuedConnection); + connect(m_simulators.at(m_current_simulator).get(), &Simulator_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()); + }); this->start(); } diff --git a/server/simulators/test.cpp b/server/simulators/test.cpp index 779aa57..199b076 100644 --- a/server/simulators/test.cpp +++ b/server/simulators/test.cpp @@ -65,7 +65,7 @@ void Test_Sim::timer() void Test_Sim::run_at_startup() { // Run at startup - SPDLOG_INFO("Test_Sim started"); + LOG_INFO("Test_Sim started"); } void Test_Sim::button_clicked(UI_button *id) @@ -86,7 +86,7 @@ void Test_Sim::slider_changed(UI_slider *id, double value) { if (id == &m_slider) { - SPDLOG_INFO("Test slider changed to {}", value); + LOG_INFO(QString("Test slider changed to %0").arg(value)); m_label.set_text(std::to_string(value)); m_dial.set_value(value); m_thermo.set_value(value);