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:
+37
-2
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user