Start using spdlog library

b CAN
This commit is contained in:
2024-08-27 11:10:37 +02:00
parent d24168e5d1
commit 3f1cf62d5c
10 changed files with 72 additions and 18 deletions
+2
View File
@@ -34,3 +34,5 @@
.vscode/
build/
logs/
+1 -1
View File
@@ -29,8 +29,8 @@ target_include_directories(server
)
target_link_libraries(server PRIVATE
CAN
UI
Timer
magic_enum::magic_enum
spdlog
)
+37 -2
View File
@@ -3,25 +3,57 @@
#include <stdlib.h>
#include <unistd.h>
#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<spdlog::sinks::stdout_color_sink_mt >();
auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("logs/sim_us.txt", 1024*1024*10, 3);
std::vector<spdlog::sink_ptr> sinks {stdout_sink, rotating_sink};
auto logger = std::make_shared<spdlog::async_logger>("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
+5 -5
View File
@@ -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;
+1
View File
@@ -2,6 +2,7 @@
#include "UI.hpp"
#include "timer.hpp"
#include "spdlog/spdlog.h"
class Simulator_base {
+16 -5
View File
@@ -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<std::string> 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<std::string> 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;
}
}
+2
View File
@@ -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;
+6 -3
View File
@@ -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<std::string(std::string)> on_message,
std::function<std::string()> 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");
}
}
+1 -1
View File
@@ -35,7 +35,7 @@ struct action {
class Websocket
{
public:
Websocket(uint16_t port, std::function<std::string(std::string)> on_message, std::function<std::string()> on_update);
Websocket(uint16_t port, std::ostream* out, std::function<std::string(std::string)> on_message, std::function<std::string()> on_update);
void run();
void stop();
void process_messages();