mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +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
|
class Spdlog_buffer : public std::stringbuf
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Spdlog_buffer(std::function <void(std::string)> log_function) : log_function(log_function) {}
|
||||||
int sync() {
|
int sync() {
|
||||||
std::string str = this->str();
|
std::string str = this->str();
|
||||||
str.pop_back();
|
str.pop_back();
|
||||||
spdlog::info(str);
|
log_function(str);
|
||||||
this->str("");
|
this->str("");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
private:
|
||||||
|
std::function <void(std::string)> log_function;
|
||||||
};
|
};
|
||||||
|
|
||||||
Simulators g_simulators;
|
Simulators g_simulators;
|
||||||
Websocket* g_web_socket;
|
Websocket* g_web_socket;
|
||||||
Spdlog_buffer g_buffer;
|
Spdlog_buffer g_info_buffer([](std::string message) { spdlog::info(message); });
|
||||||
std::ostream g_out(&g_buffer);
|
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)
|
void int_handler(int s)
|
||||||
{
|
{
|
||||||
@@ -70,7 +75,7 @@ int main(void)
|
|||||||
|
|
||||||
// The server starts in this thread
|
// The server starts in this thread
|
||||||
spdlog::info("Starting server");
|
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;
|
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
|
||||||
|
|||||||
+7
-3
@@ -1,14 +1,16 @@
|
|||||||
#include "websocket.hpp"
|
#include "websocket.hpp"
|
||||||
#include "spdlog/spdlog.h"
|
#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(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_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_alog().set_ostream(out);
|
||||||
|
m_server.get_elog().set_ostream(err);
|
||||||
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 +
|
||||||
@@ -25,18 +27,20 @@ Websocket::Websocket(uint16_t port, std::ostream* out,
|
|||||||
|
|
||||||
void Websocket::run()
|
void Websocket::run()
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
// listen on specified port
|
// listen on specified port
|
||||||
m_server.listen(m_port);
|
m_server.listen(m_port);
|
||||||
// Start the server accept loop
|
// Start the server accept loop
|
||||||
m_server.start_accept();
|
m_server.start_accept();
|
||||||
try
|
|
||||||
{
|
|
||||||
// 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) {
|
||||||
spdlog::critical("{}", e.what());
|
spdlog::critical("{}", e.what());
|
||||||
|
exit(1);
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
spdlog::critical("other exception");
|
spdlog::critical("other exception");
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -35,7 +35,7 @@ struct action {
|
|||||||
class Websocket
|
class Websocket
|
||||||
{
|
{
|
||||||
public:
|
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 run();
|
||||||
void stop();
|
void stop();
|
||||||
void process_messages();
|
void process_messages();
|
||||||
|
|||||||
Reference in New Issue
Block a user