From 0566a4045eaeccfcabec315fa2f8f8d468d173dc Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sat, 24 Aug 2024 15:53:05 +0200 Subject: [PATCH] spdlog in websocket class better logging for std::cerr --- tools/main.cpp | 13 +++++++++---- tools/websocket.cpp | 14 +++++++++----- tools/websocket.hpp | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/tools/main.cpp b/tools/main.cpp index 76603de..5c23021 100644 --- a/tools/main.cpp +++ b/tools/main.cpp @@ -15,19 +15,24 @@ class Spdlog_buffer : public std::stringbuf { public: + Spdlog_buffer(std::function log_function) : log_function(log_function) {} int sync() { std::string str = this->str(); str.pop_back(); - spdlog::info(str); + log_function(str); this->str(""); return 0; } +private: + std::function log_function; }; Simulators g_simulators; Websocket* g_web_socket; -Spdlog_buffer g_buffer; -std::ostream g_out(&g_buffer); +Spdlog_buffer g_info_buffer([](std::string message) { spdlog::info(message); }); +Spdlog_buffer g_error_buffer([](std::string message) { spdlog::error(message); }); +std::ostream g_out(&g_info_buffer); +std::ostream g_err(&g_error_buffer); void int_handler(int s) { @@ -70,7 +75,7 @@ int main(void) // The server starts in this thread spdlog::info("Starting server"); - Websocket web_socket(9002, &g_out, [](std::string message) { return message_parser(message); }, []() { return g_simulators.changed_UI_items().dump(); }); + Websocket web_socket(9002, &g_out, &g_err, [](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/websocket.cpp b/tools/websocket.cpp index 7902b04..e2f1930 100644 --- a/tools/websocket.cpp +++ b/tools/websocket.cpp @@ -1,14 +1,16 @@ #include "websocket.hpp" #include "spdlog/spdlog.h" -Websocket::Websocket(uint16_t port, std::ostream* out, +Websocket::Websocket(uint16_t port, std::ostream* out, std::ostream* err, 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_elog().set_custom(err == &std::cerr ? false : true); m_server.get_alog().set_ostream(out); + m_server.get_elog().set_ostream(err); m_server.set_access_channels(websocketpp::log::alevel::connect + websocketpp::log::alevel::disconnect); m_server.clear_access_channels(websocketpp::log::alevel::frame_payload + @@ -25,18 +27,20 @@ Websocket::Websocket(uint16_t port, std::ostream* out, void Websocket::run() { - // listen on specified port - m_server.listen(m_port); - // Start the server accept loop - m_server.start_accept(); try { + // listen on specified port + m_server.listen(m_port); + // Start the server accept loop + m_server.start_accept(); // Start the ASIO io_service run loop m_server.run(); } catch (websocketpp::exception const& e) { spdlog::critical("{}", e.what()); + exit(1); } catch (...) { spdlog::critical("other exception"); + exit(1); } } diff --git a/tools/websocket.hpp b/tools/websocket.hpp index 94dc5c4..dbcd2e5 100644 --- a/tools/websocket.hpp +++ b/tools/websocket.hpp @@ -35,7 +35,7 @@ struct action { class Websocket { public: - Websocket(uint16_t port, std::ostream* out, std::function on_message, std::function on_update); + Websocket(uint16_t port, std::ostream* out, std::ostream* err, std::function on_message, std::function on_update); void run(); void stop(); void process_messages();