diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 3b3a8c2..5906461 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -99,7 +99,7 @@ target_include_directories(Simulator-CVS_I10 ${PROJECT_BINARY_DIR}/ ) -target_link_libraries(Simulator-CVS_I10 PRIVATE qwt Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::WebSockets magic_enum::magic_enum nlohmann_json::nlohmann_json) +target_link_libraries(client PRIVATE qwt spdlog Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::WebSockets magic_enum::magic_enum nlohmann_json::nlohmann_json) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. # If you are developing for iOS or macOS you should consider setting an diff --git a/client/main.cpp b/client/main.cpp index fd2b13c..823c43e 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -1,6 +1,22 @@ #include "mainwindow.h" - #include +#include "spdlog/async.h" +#include "spdlog/sinks/stdout_color_sinks.h" +#include "spdlog/spdlog.h" + +void init_logger() +{ + spdlog::init_thread_pool(8192, 1); + auto stdout_sink = std::make_shared(); + //auto rotating_sink = std::make_shared("log-sim_us.txt", 1024 * 1024 * 10, 3); + std::vector sinks{stdout_sink/*, rotating_sink*/}; + auto logger = std::make_shared("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(), + spdlog::async_overflow_policy::block); + spdlog::set_default_logger(logger); + spdlog::set_pattern("[%H:%M:%S %z] [%^---%L---%$] %@: %v"); + spdlog::flush_every(std::chrono::seconds(3)); + spdlog::set_level(spdlog::level::trace); +} int main(int argc, char *argv[]) { @@ -22,6 +38,9 @@ int main(int argc, char *argv[]) parser.addOption(portOption); parser.process(a); + // Initialize the logger + init_logger(); + SPDLOG_INFO("Starting GUI"); QString host = parser.value(hostOption); uint16_t port = parser.value(portOption).toUShort(); diff --git a/client/mainwindow.cpp b/client/mainwindow.cpp index df06780..fa41caa 100644 --- a/client/mainwindow.cpp +++ b/client/mainwindow.cpp @@ -85,7 +85,7 @@ void MainWindow::open_sim(QString sim_name) } catch (const std::out_of_range& ex) { - QD << "out_of_range::what(): " << ex.what(); + SPDLOG_CRITICAL("out_of_range: {}", ex.what()); return; } @@ -102,21 +102,21 @@ void MainWindow::check_active_sim(QString name) { if (name == "") { - QD << "Name not set"; + SPDLOG_DEBUG("Name not set"); m_web_socket->send_command(Web_socket_wrapper::Command::switch_simulator, m_selected_sim_name); return; } - QD << "Current sim:" << name; + SPDLOG_DEBUG("Current sim:", name); if (name == m_selected_sim_name) { - QD << "Name set and the same"; + SPDLOG_DEBUG("Name set and the same"); open_sim_window(); return; } - QD << "Name set and wrong"; + SPDLOG_DEBUG("Name set and wrong"); m_web_socket->send_command(Web_socket_wrapper::Command::switch_simulator, m_selected_sim_name); } diff --git a/client/simulator_base.cpp b/client/simulator_base.cpp index 40e91dd..869b2e7 100644 --- a/client/simulator_base.cpp +++ b/client/simulator_base.cpp @@ -19,8 +19,12 @@ QWidget* SimulatorBase::id_to_ui(size_t id) void SimulatorBase::showEvent( QShowEvent* event ) { QWidget::showEvent( event ); - QD << "Connecting callbacks"; - setup_cb(); + SPDLOG_DEBUG("Connecting callbacks"); + + connect(m_web_socket, &Web_socket_wrapper::on_command_cb, this, [=, this](json& j){on_cmd_cb(j);}); + connect(m_web_socket, &Web_socket_wrapper::on_event_cb, this, [=, this](json& j){on_event_cb(j);}); + connect(m_web_socket, &Web_socket_wrapper::on_closed, this, [=, this]{m_error_dialog->open();}); + m_web_socket->send_command(Web_socket_wrapper::Command::get_UI_elements); this->m_timer_update->start(m_refresh_rate); m_open = true; @@ -30,7 +34,7 @@ void SimulatorBase::closeEvent(QCloseEvent* event) { if (m_open == true) { - QD << "Disconnecting callbacks"; + SPDLOG_DEBUG("Disconnecting callbacks"); QWidget::closeEvent(event); m_timer_update->stop(); disconnect(m_web_socket, nullptr, nullptr, nullptr); @@ -39,13 +43,6 @@ void SimulatorBase::closeEvent(QCloseEvent* event) } } -void SimulatorBase::setup_cb(void) -{ - connect(m_web_socket, &Web_socket_wrapper::on_command_cb, this, [=, this](json& j){on_cmd_cb(j);}); - connect(m_web_socket, &Web_socket_wrapper::on_event_cb, this, [=, this](json& j){on_event_cb(j);}); - connect(m_web_socket, &Web_socket_wrapper::on_closed, this, [=, this]{m_error_dialog->open();}); -} - void SimulatorBase::on_cmd_cb(json& j) { auto response = magic_enum::enum_cast(std::string(j.at("type"))).value_or(Web_socket_wrapper::Command::end); @@ -141,14 +138,14 @@ void SimulatorBase::process_ui_label(json& uiItem) QWidget* widget = id_to_ui(uiItem["id"]); if (widget == nullptr) { - QD << "id_to_ui returned null on " << QString::number((size_t)uiItem["id"]); + SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString()); return; } auto label = qobject_cast(widget); if (label == nullptr) { - QD << "widget is not of type QLabel"; + SPDLOG_WARN("widget is not of type QLabel"); return; } @@ -166,14 +163,14 @@ void SimulatorBase::process_ui_slider(json& uiItem) QWidget* widget = id_to_ui(uiItem["id"]); if (widget == nullptr) { - QD << "id_to_ui returned null on " << QString::number((size_t)uiItem["id"]); + SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString()); return; } auto slider = qobject_cast(widget); if (slider == nullptr) { - QD << "widget is not of type QwtSlider"; + SPDLOG_WARN("widget is not of type QwtSlider"); return; } @@ -210,14 +207,14 @@ void SimulatorBase::process_ui_dial(json& uiItem) QWidget* widget = id_to_ui(uiItem["id"]); if (widget == nullptr) { - QD << "id_to_ui returned null on " << QString::number((size_t)uiItem["id"]); + SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString()); return; } auto dial = qobject_cast(widget); if (dial == nullptr) { - QD << "widget is not of type QwtDial"; + SPDLOG_WARN("widget is not of type QwtDial"); return; } @@ -248,14 +245,14 @@ void SimulatorBase::process_ui_thermo(json& uiItem) QWidget* widget = id_to_ui(uiItem["id"]); if (widget == nullptr) { - QD << "id_to_ui returned null on " << QString::number((size_t)uiItem["id"]); + SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString()); return; } auto thermo = qobject_cast(widget); if (thermo == nullptr) { - QD << "widget is not of type QwtThermo"; + SPDLOG_WARN("widget is not of type QwtThermo"); return; } @@ -282,8 +279,7 @@ void SimulatorBase::process_ui_thermo(json& uiItem) void SimulatorBase::process_ui_textbox(json& uiItem) { - QD << "Not implemented"; - emit quit(); + SPDLOG_CRITICAL("Not implemented"); } void SimulatorBase::process_ui_combobox(json& uiItem) @@ -291,14 +287,14 @@ void SimulatorBase::process_ui_combobox(json& uiItem) QWidget* widget = id_to_ui(uiItem["id"]); if (widget == nullptr) { - QD << "id_to_ui returned null on " << QString::number((size_t)uiItem["id"]); + SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString()); return; } auto combobox = qobject_cast(widget); if (combobox == nullptr) { - QD << "widget is not of type QComboBox"; + SPDLOG_WARN("widget is not of type QComboBox"); return; } @@ -318,8 +314,7 @@ void SimulatorBase::process_ui_combobox(json& uiItem) void SimulatorBase::process_ui_radiobutton(json& uiItem) { - QD << "Not implemented"; - emit quit(); + SPDLOG_CRITICAL("Not implemented"); } void SimulatorBase::process_ui_button(json& uiItem) @@ -328,14 +323,14 @@ void SimulatorBase::process_ui_button(json& uiItem) QWidget* widget = id_to_ui(uiItem["id"]); if (widget == nullptr) { - QD << "id_to_ui returned null on " << QString::number((size_t)uiItem["id"]); + SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString()); return; } auto button = qobject_cast(widget); if (button == nullptr) { - QD << "widget is not of type QPushButton"; + SPDLOG_WARN("widget is not of type QPushButton"); return; } @@ -389,14 +384,14 @@ void SimulatorBase::process_ui_led(json& uiItem) QWidget* widget = id_to_ui(uiItem["id"]); if (widget == nullptr) { - QD << "id_to_ui returned null on " << QString::number((size_t)uiItem["id"]); + SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString()); return; } auto led = qobject_cast(widget); if (led == nullptr) { - QD << "widget is not of type led"; + SPDLOG_WARN("widget is not of type led"); return; } @@ -444,14 +439,14 @@ void SimulatorBase::process_ui_can(json& uiItem) QWidget* widget = id_to_ui(uiItem["id"]); if (widget == nullptr) { - QD << "id_to_ui returned null on " << QString::number((size_t)uiItem["id"]); + SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString()); return; } auto can_ui = qobject_cast(widget); if (can_ui == nullptr) { - QD << "widget is not of type led"; + SPDLOG_WARN("widget is not of type led"); return; } diff --git a/client/simulator_base.h b/client/simulator_base.h index 4ac5702..8ea23bc 100644 --- a/client/simulator_base.h +++ b/client/simulator_base.h @@ -9,17 +9,14 @@ #include "led.h" #include #include "IterableEnum.hpp" - +#include "spdlog/spdlog.h" #include using json = nlohmann::json; -#define QD qDebug() << "Debug:" << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << ":" - class SimulatorBase : public QMainWindow { Q_OBJECT public: explicit SimulatorBase(Web_socket_wrapper* api, QWidget* parent = nullptr); - void setup_cb(void); void sim_update(void); QWidget* id_to_ui(size_t id); virtual QLabel* id_to_label(size_t name) {return nullptr;} diff --git a/client/web_socket_wrapper.cpp b/client/web_socket_wrapper.cpp index a97e532..6292ce3 100644 --- a/client/web_socket_wrapper.cpp +++ b/client/web_socket_wrapper.cpp @@ -1,4 +1,5 @@ #include "web_socket_wrapper.h" +#include "spdlog/spdlog.h" #include "magic_enum.hpp" Web_socket_wrapper::Web_socket_wrapper(const QUrl &url, QObject *parent) : @@ -185,7 +186,7 @@ void Web_socket_wrapper::m_on_pong(quint64 elapsedTime) void Web_socket_wrapper::m_on_pong_timeout() { - qDebug() << "Pong timeout"; + SPDLOG_CRITICAL("Pong timeout"); m_web_socket->close(); emit on_closed(); }