server logger

move over to qt signals that are processed in simulators
This commit is contained in:
2026-02-14 14:32:49 +01:00
parent 043c19c537
commit 16e6100465
4 changed files with 30 additions and 4 deletions
+1 -1
View File
@@ -69,7 +69,7 @@ foreach(sim ${sims})
# Create a library for each .cpp file # 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) 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_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 add_custom_command(TARGET sim_${LIB_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:sim_${LIB_NAME}> $<TARGET_FILE:sim_${LIB_NAME}>
+21 -1
View File
@@ -1,5 +1,4 @@
#pragma once #pragma once
#include "spdlog/spdlog.h"
#include <QSerialPort> #include <QSerialPort>
#include <QThread> #include <QThread>
#include <QTimer> #include <QTimer>
@@ -22,6 +21,14 @@ Q_FORWARD_DECLARE_OBJC_CLASS(UI_item);
return new sim(com, parent); \ 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 * @brief Struct holding all communication interfaces
* *
@@ -49,6 +56,17 @@ class Simulator_base : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
enum class Log_level_enum : int
{
trace,
debug,
info,
warn,
err,
critical,
off,
n_levels
};
/** /**
* @brief Construct a new Simulator_base object * @brief Construct a new Simulator_base object
* *
@@ -171,6 +189,8 @@ class Simulator_base : public QObject
*/ */
void sim_changed(); void sim_changed();
void log_signal(const char *filename_in, int line_in, const char *funcname_in, int level, QString msg);
protected: protected:
/** /**
* @brief The name of the simulator * @brief The name of the simulator
+6
View File
@@ -1,6 +1,7 @@
#include "simulators.hpp" #include "simulators.hpp"
#define INTERNAL #define INTERNAL
#include "simulator_base.hpp" #include "simulator_base.hpp"
#include "spdlog/spdlog.h"
#include "websocket.hpp" #include "websocket.hpp"
#include <QCoreApplication> #include <QCoreApplication>
#include <QDirIterator> #include <QDirIterator>
@@ -153,6 +154,11 @@ void Simulators::switch_simulator(std::string name)
} }
}, },
Qt::QueuedConnection); 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(); this->start();
} }
+2 -2
View File
@@ -65,7 +65,7 @@ void Test_Sim::timer()
void Test_Sim::run_at_startup() void Test_Sim::run_at_startup()
{ {
// Run at startup // Run at startup
SPDLOG_INFO("Test_Sim started"); LOG_INFO("Test_Sim started");
} }
void Test_Sim::button_clicked(UI_button *id) 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) 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_label.set_text(std::to_string(value));
m_dial.set_value(value); m_dial.set_value(value);
m_thermo.set_value(value); m_thermo.set_value(value);