From 3f1cf62d5cceeb9e270b97ded5b9a49637720d9a Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sat, 24 Aug 2024 15:23:36 +0200 Subject: [PATCH] Start using spdlog library b CAN --- .gitignore | 2 ++ external/websocketpp | 2 +- tools/CMakeLists.txt | 2 +- tools/main.cpp | 39 +++++++++++++++++++++++++++++++++-- tools/message_parser.cpp | 10 ++++----- tools/simulator_base.hpp | 1 + tools/simulators/test_sim.cpp | 21 ++++++++++++++----- tools/simulators/test_sim.hpp | 2 ++ tools/websocket.cpp | 9 +++++--- tools/websocket.hpp | 2 +- 10 files changed, 72 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index b3663e3..1285f9e 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ .vscode/ build/ + +logs/ diff --git a/external/websocketpp b/external/websocketpp index e1b5337..cbe02ed 160000 --- a/external/websocketpp +++ b/external/websocketpp @@ -1 +1 @@ -Subproject commit e1b533756f4b509d456c82fea324df61d49f9e2b +Subproject commit cbe02ed984a8512cf94f3dcd2b44bc98cbd05845 diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 032242d..cee8267 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -29,8 +29,8 @@ target_include_directories(server ) target_link_libraries(server PRIVATE - CAN UI Timer magic_enum::magic_enum + spdlog ) \ No newline at end of file diff --git a/tools/main.cpp b/tools/main.cpp index 5315b5d..76603de 100644 --- a/tools/main.cpp +++ b/tools/main.cpp @@ -3,25 +3,57 @@ #include #include +#include "spdlog/spdlog.h" +#include "spdlog/async.h" +#include "spdlog/sinks/stdout_color_sinks.h" +#include "spdlog/sinks/rotating_file_sink.h" + #include "simulators.hpp" #include "websocket.hpp" #include "message_parser.hpp" +class Spdlog_buffer : public std::stringbuf +{ +public: + int sync() { + std::string str = this->str(); + str.pop_back(); + spdlog::info(str); + this->str(""); + return 0; + } +}; + Simulators g_simulators; Websocket* g_web_socket; +Spdlog_buffer g_buffer; +std::ostream g_out(&g_buffer); void int_handler(int s) { UNUSED(s); - std::cout << "\nCaught Ctrl + C\nShutting down." << std::endl; + g_out << "Caught Ctrl + C" << std::endl; if (g_web_socket != nullptr) g_web_socket->stop(); g_simulators.stop(); + g_out << "Shutting down." << std::endl; // delay to allow the server to stop gracefully std::this_thread::sleep_for(std::chrono::milliseconds(1000)); exit(1); } +void init_logger() +{ + spdlog::init_thread_pool(8192, 1); + auto stdout_sink = std::make_shared(); + auto rotating_sink = std::make_shared("logs/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] [%n] [%^---%L---%$] [thread %t] %v"); + spdlog::flush_every(std::chrono::seconds(3)); +} + int main(void) { // Setup the signal handler @@ -33,9 +65,12 @@ int main(void) sigaction(SIGINT, &sigIntHandler, NULL); + // Initialize the logger + init_logger(); // The server starts in this thread - Websocket web_socket(9002, [](std::string message) { return message_parser(message); }, []() { return g_simulators.changed_UI_items().dump(); }); + spdlog::info("Starting server"); + Websocket web_socket(9002, &g_out, [](std::string message) { return message_parser(message); }, []() { return g_simulators.changed_UI_items().dump(); }); g_web_socket = &web_socket; // Start the message processor and broadcast processor in separate threads diff --git a/tools/message_parser.cpp b/tools/message_parser.cpp index 8a9afe6..2c764dc 100644 --- a/tools/message_parser.cpp +++ b/tools/message_parser.cpp @@ -30,27 +30,27 @@ json command_parser(json command) switch (cmd) { case Command::get_UI_element: - std::cout << type << ": " << command["id"] << std::endl; + spdlog::info("{}: {}", type, std::string(command["id"])); j["response"]["type"] = type; j["response"]["UI_item"] = g_simulators.invoke_active_simulator()->get_UI_item(command["id"])->to_json(); break; case Command::get_UI_elements: - std::cout << type << std::endl; + spdlog::info("{}", type); j["response"]["type"] = type; j["response"]["UI_items"] = g_simulators.invoke_active_simulator()->get_UI_items()["UI_items"]; break; case Command::switch_simulator: - std::cout << type << " to " << command["name"] << std::endl; + spdlog::info("{} to {}", type, std::string(command["name"])); g_simulators.switch_simulator(command["name"]); j["response"]["type"] = type; break; case Command::get_active_simulator_name: - std::cout << type << std::endl; + spdlog::info("{}", type); j["response"]["type"] = type; j["response"]["name"] = g_simulators.active_simulator_name(); break; case Command::get_simulators: - std::cout << type << std::endl; + spdlog::info("{}", type); j["response"]["type"] = type; j["response"]["simulators"] = g_simulators.list_simulators(); break; diff --git a/tools/simulator_base.hpp b/tools/simulator_base.hpp index 996f97c..7a01169 100644 --- a/tools/simulator_base.hpp +++ b/tools/simulator_base.hpp @@ -2,6 +2,7 @@ #include "UI.hpp" #include "timer.hpp" +#include "spdlog/spdlog.h" class Simulator_base { diff --git a/tools/simulators/test_sim.cpp b/tools/simulators/test_sim.cpp index 8518971..8986135 100644 --- a/tools/simulators/test_sim.cpp +++ b/tools/simulators/test_sim.cpp @@ -3,11 +3,11 @@ Test_Sim::Test_Sim() : Simulator_base("Test_Sim"), -m_button("button", "Button", Color(Color::White), Color(Color::Black), [](std::string id) { std::cout << "Button " << id << " clicked" << std::endl; }), -m_combobox("combobox", "Combobox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}, 0, [](std::string id, std::string selected) { std::cout << "Combobox " << id << " selected " << selected << std::endl; }), +m_button("button", "Off", Color(Color::White), Color(Color::Red), [this](std::string id) { button_clicked(id); }), +m_combobox("combobox", "Combobox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}, 0, [](std::string id, std::string selected) { spdlog::info("Combobox {} selected {}", id, selected); }), m_label("label", "Label", Color(Color::White), Color(Color::Black)), -m_checkbox("checkbox", "Checkbox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}, [](std::string id, std::vector selected) { std::cout << "Checkbox " << id << " selected " << (selected.size() > 0 ? selected[0] : "") << std::endl; }), -m_slider("slider", "Slider", Color(Color::White), Color(Color::Black), 0, 100, 50, [](std::string id, double value) { std::cout << "Slider " << id << " changed to " << value << std::endl; }) +m_checkbox("checkbox", "Checkbox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}, [](std::string id, std::vector selected) { spdlog::info("Checkbox {} selected {}", id, (selected.size() > 0 ? selected[0] : "")); }), +m_slider("slider", "Slider", Color(Color::White), Color(Color::Black), 0, 100, 50, [](std::string id, double value) { spdlog::info("Slider {} changed to {}", id, value); }) { this->add_UI_item(&this->m_button); this->add_UI_item(&this->m_combobox); @@ -24,5 +24,16 @@ void Test_Sim::timer() void Test_Sim::run_at_startup() { // Run at startup - std::cout << "Test_Sim started" << std::endl; + spdlog::info("Test_Sim started"); +} + +void Test_Sim::button_clicked(std::string id) +{ + if (id == "button") + { + bool state = this->m_button.bg_color() == Color(Color::Red) ? true : false; + this->m_button.set_bg_color(state ? Color(Color::Green) : Color(Color::Red)); + this->m_button.set_text(state ? "On" : "Off"); + return; + } } diff --git a/tools/simulators/test_sim.hpp b/tools/simulators/test_sim.hpp index 97dd002..b6703f8 100644 --- a/tools/simulators/test_sim.hpp +++ b/tools/simulators/test_sim.hpp @@ -6,6 +6,8 @@ class Test_Sim : public Simulator_base { Test_Sim(); void timer() override; void run_at_startup() override; + private: + void button_clicked(std::string id); private: UI_button m_button; UI_combobox m_combobox; diff --git a/tools/websocket.cpp b/tools/websocket.cpp index 54cb31d..7902b04 100644 --- a/tools/websocket.cpp +++ b/tools/websocket.cpp @@ -1,11 +1,14 @@ #include "websocket.hpp" +#include "spdlog/spdlog.h" -Websocket::Websocket(uint16_t port, +Websocket::Websocket(uint16_t port, std::ostream* out, std::function on_message, std::function on_update) : m_on_message(on_message), m_on_update(on_update), m_port(port) { // Set logging settings + m_server.get_alog().set_custom(out == &std::cout ? false : true); + m_server.get_alog().set_ostream(out); m_server.set_access_channels(websocketpp::log::alevel::connect + websocketpp::log::alevel::disconnect); m_server.clear_access_channels(websocketpp::log::alevel::frame_payload + @@ -31,9 +34,9 @@ void Websocket::run() // Start the ASIO io_service run loop m_server.run(); } catch (websocketpp::exception const& e) { - std::cout << e.what() << std::endl; + spdlog::critical("{}", e.what()); } catch (...) { - std::cout << "other exception" << std::endl; + spdlog::critical("other exception"); } } diff --git a/tools/websocket.hpp b/tools/websocket.hpp index c27a9b8..94dc5c4 100644 --- a/tools/websocket.hpp +++ b/tools/websocket.hpp @@ -35,7 +35,7 @@ struct action { class Websocket { public: - Websocket(uint16_t port, std::function on_message, std::function on_update); + Websocket(uint16_t port, std::ostream* out, std::function on_message, std::function on_update); void run(); void stop(); void process_messages();