mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-26 21:51:13 +01:00
Start using spdlog library
b CAN
This commit is contained in:
@@ -34,3 +34,5 @@
|
|||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
build/
|
build/
|
||||||
|
|
||||||
|
logs/
|
||||||
|
|||||||
Vendored
+1
-1
Submodule external/websocketpp updated: e1b533756f...cbe02ed984
@@ -29,8 +29,8 @@ target_include_directories(server
|
|||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(server PRIVATE
|
target_link_libraries(server PRIVATE
|
||||||
CAN
|
|
||||||
UI
|
UI
|
||||||
Timer
|
Timer
|
||||||
magic_enum::magic_enum
|
magic_enum::magic_enum
|
||||||
|
spdlog
|
||||||
)
|
)
|
||||||
+37
-2
@@ -3,25 +3,57 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.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 "simulators.hpp"
|
||||||
#include "websocket.hpp"
|
#include "websocket.hpp"
|
||||||
#include "message_parser.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;
|
Simulators g_simulators;
|
||||||
Websocket* g_web_socket;
|
Websocket* g_web_socket;
|
||||||
|
Spdlog_buffer g_buffer;
|
||||||
|
std::ostream g_out(&g_buffer);
|
||||||
|
|
||||||
void int_handler(int s)
|
void int_handler(int s)
|
||||||
{
|
{
|
||||||
UNUSED(s);
|
UNUSED(s);
|
||||||
std::cout << "\nCaught Ctrl + C\nShutting down." << std::endl;
|
g_out << "Caught Ctrl + C" << std::endl;
|
||||||
if (g_web_socket != nullptr)
|
if (g_web_socket != nullptr)
|
||||||
g_web_socket->stop();
|
g_web_socket->stop();
|
||||||
g_simulators.stop();
|
g_simulators.stop();
|
||||||
|
g_out << "Shutting down." << std::endl;
|
||||||
// delay to allow the server to stop gracefully
|
// delay to allow the server to stop gracefully
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||||
exit(1);
|
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)
|
int main(void)
|
||||||
{
|
{
|
||||||
// Setup the signal handler
|
// Setup the signal handler
|
||||||
@@ -33,9 +65,12 @@ int main(void)
|
|||||||
|
|
||||||
sigaction(SIGINT, &sigIntHandler, NULL);
|
sigaction(SIGINT, &sigIntHandler, NULL);
|
||||||
|
|
||||||
|
// Initialize the logger
|
||||||
|
init_logger();
|
||||||
|
|
||||||
// The server starts in this thread
|
// 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;
|
g_web_socket = &web_socket;
|
||||||
|
|
||||||
// Start the message processor and broadcast processor in separate threads
|
// Start the message processor and broadcast processor in separate threads
|
||||||
|
|||||||
@@ -30,27 +30,27 @@ json command_parser(json command)
|
|||||||
switch (cmd)
|
switch (cmd)
|
||||||
{
|
{
|
||||||
case Command::get_UI_element:
|
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"]["type"] = type;
|
||||||
j["response"]["UI_item"] = g_simulators.invoke_active_simulator()->get_UI_item(command["id"])->to_json();
|
j["response"]["UI_item"] = g_simulators.invoke_active_simulator()->get_UI_item(command["id"])->to_json();
|
||||||
break;
|
break;
|
||||||
case Command::get_UI_elements:
|
case Command::get_UI_elements:
|
||||||
std::cout << type << std::endl;
|
spdlog::info("{}", type);
|
||||||
j["response"]["type"] = type;
|
j["response"]["type"] = type;
|
||||||
j["response"]["UI_items"] = g_simulators.invoke_active_simulator()->get_UI_items()["UI_items"];
|
j["response"]["UI_items"] = g_simulators.invoke_active_simulator()->get_UI_items()["UI_items"];
|
||||||
break;
|
break;
|
||||||
case Command::switch_simulator:
|
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"]);
|
g_simulators.switch_simulator(command["name"]);
|
||||||
j["response"]["type"] = type;
|
j["response"]["type"] = type;
|
||||||
break;
|
break;
|
||||||
case Command::get_active_simulator_name:
|
case Command::get_active_simulator_name:
|
||||||
std::cout << type << std::endl;
|
spdlog::info("{}", type);
|
||||||
j["response"]["type"] = type;
|
j["response"]["type"] = type;
|
||||||
j["response"]["name"] = g_simulators.active_simulator_name();
|
j["response"]["name"] = g_simulators.active_simulator_name();
|
||||||
break;
|
break;
|
||||||
case Command::get_simulators:
|
case Command::get_simulators:
|
||||||
std::cout << type << std::endl;
|
spdlog::info("{}", type);
|
||||||
j["response"]["type"] = type;
|
j["response"]["type"] = type;
|
||||||
j["response"]["simulators"] = g_simulators.list_simulators();
|
j["response"]["simulators"] = g_simulators.list_simulators();
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "UI.hpp"
|
#include "UI.hpp"
|
||||||
#include "timer.hpp"
|
#include "timer.hpp"
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
class Simulator_base {
|
class Simulator_base {
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
|
|
||||||
Test_Sim::Test_Sim() :
|
Test_Sim::Test_Sim() :
|
||||||
Simulator_base("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_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) { std::cout << "Combobox " << id << " selected " << selected << 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) { spdlog::info("Combobox {} selected {}", id, selected); }),
|
||||||
m_label("label", "Label", Color(Color::White), Color(Color::Black)),
|
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_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) { std::cout << "Slider " << id << " changed to " << value << std::endl; })
|
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_button);
|
||||||
this->add_UI_item(&this->m_combobox);
|
this->add_UI_item(&this->m_combobox);
|
||||||
@@ -24,5 +24,16 @@ void Test_Sim::timer()
|
|||||||
void Test_Sim::run_at_startup()
|
void Test_Sim::run_at_startup()
|
||||||
{
|
{
|
||||||
// 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ class Test_Sim : public Simulator_base {
|
|||||||
Test_Sim();
|
Test_Sim();
|
||||||
void timer() override;
|
void timer() override;
|
||||||
void run_at_startup() override;
|
void run_at_startup() override;
|
||||||
|
private:
|
||||||
|
void button_clicked(std::string id);
|
||||||
private:
|
private:
|
||||||
UI_button m_button;
|
UI_button m_button;
|
||||||
UI_combobox m_combobox;
|
UI_combobox m_combobox;
|
||||||
|
|||||||
+6
-3
@@ -1,11 +1,14 @@
|
|||||||
#include "websocket.hpp"
|
#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(std::string)> on_message,
|
||||||
std::function<std::string()> on_update)
|
std::function<std::string()> on_update)
|
||||||
: m_on_message(on_message), m_on_update(on_update), m_port(port)
|
: m_on_message(on_message), m_on_update(on_update), m_port(port)
|
||||||
{
|
{
|
||||||
// Set logging settings
|
// 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 +
|
m_server.set_access_channels(websocketpp::log::alevel::connect +
|
||||||
websocketpp::log::alevel::disconnect);
|
websocketpp::log::alevel::disconnect);
|
||||||
m_server.clear_access_channels(websocketpp::log::alevel::frame_payload +
|
m_server.clear_access_channels(websocketpp::log::alevel::frame_payload +
|
||||||
@@ -31,9 +34,9 @@ void Websocket::run()
|
|||||||
// Start the ASIO io_service run loop
|
// Start the ASIO io_service run loop
|
||||||
m_server.run();
|
m_server.run();
|
||||||
} catch (websocketpp::exception const& e) {
|
} catch (websocketpp::exception const& e) {
|
||||||
std::cout << e.what() << std::endl;
|
spdlog::critical("{}", e.what());
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
std::cout << "other exception" << std::endl;
|
spdlog::critical("other exception");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -35,7 +35,7 @@ struct action {
|
|||||||
class Websocket
|
class Websocket
|
||||||
{
|
{
|
||||||
public:
|
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 run();
|
||||||
void stop();
|
void stop();
|
||||||
void process_messages();
|
void process_messages();
|
||||||
|
|||||||
Reference in New Issue
Block a user