mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-26 21:51:13 +01:00
spdlog in websocket class
better logging for std::cerr
This commit is contained in:
+9
-4
@@ -15,19 +15,24 @@
|
||||
class Spdlog_buffer : public std::stringbuf
|
||||
{
|
||||
public:
|
||||
Spdlog_buffer(std::function <void(std::string)> 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 <void(std::string)> 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
|
||||
|
||||
+7
-3
@@ -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<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_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()
|
||||
{
|
||||
try
|
||||
{
|
||||
// listen on specified port
|
||||
m_server.listen(m_port);
|
||||
// Start the server accept loop
|
||||
m_server.start_accept();
|
||||
try
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ struct action {
|
||||
class Websocket
|
||||
{
|
||||
public:
|
||||
Websocket(uint16_t port, std::ostream* out, std::function<std::string(std::string)> on_message, std::function<std::string()> on_update);
|
||||
Websocket(uint16_t port, std::ostream* out, std::ostream* err, std::function<std::string(std::string)> on_message, std::function<std::string()> on_update);
|
||||
void run();
|
||||
void stop();
|
||||
void process_messages();
|
||||
|
||||
Reference in New Issue
Block a user