diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 078f2ab..6d6c4f0 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -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} diff --git a/client/mainwindow.cpp b/client/mainwindow.cpp index 3b08a23..21bf820 100644 --- a/client/mainwindow.cpp +++ b/client/mainwindow.cpp @@ -7,6 +7,7 @@ #include #include #include +#include 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(); } diff --git a/client/ui_base.cpp b/client/ui_base.cpp index 1c3e976..c965a40 100644 --- a/client/ui_base.cpp +++ b/client/ui_base.cpp @@ -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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(widget); if (can_ui == nullptr) { - SPDLOG_WARN("widget is not of type led"); + LOG_WARN("widget is not of type led"); return; } diff --git a/client/ui_base.hpp b/client/ui_base.hpp index 569ecac..b51dc32 100644 --- a/client/ui_base.hpp +++ b/client/ui_base.hpp @@ -1,5 +1,4 @@ #pragma once -#include "spdlog/spdlog.h" #include #include #include @@ -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;