From 597c3c54879db436f7bb9f4c5473f9b6b15e6437 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Wed, 5 Feb 2025 19:01:04 +0100 Subject: [PATCH] Use SPDLOG_* to use file:line --- server/CAN/CAN_interface.cpp | 6 +++--- server/main.cpp | 4 +--- server/message_parser.cpp | 28 ++++++++++++++-------------- server/simulators.cpp | 16 ++++++++-------- server/simulators/test_sim.cpp | 4 ++-- server/websocket.cpp | 12 ++++++------ 6 files changed, 34 insertions(+), 36 deletions(-) diff --git a/server/CAN/CAN_interface.cpp b/server/CAN/CAN_interface.cpp index 7cd6916..9b06d87 100644 --- a/server/CAN/CAN_interface.cpp +++ b/server/CAN/CAN_interface.cpp @@ -6,7 +6,7 @@ CAN_Interface::CAN_Interface(QString dev, QObject *parent) : QObject(parent) { if (!QCanBus::instance()->plugins().contains(QStringLiteral("socketcan"))) { - spdlog::critical("Runtime error: CAN Interface: socketcan plugin not found"); + SPDLOG_CRITICAL("Runtime error: CAN Interface: socketcan plugin not found"); throw std::runtime_error("CAN Interface: socketcan plugin not found"); } QString errorString; @@ -14,7 +14,7 @@ CAN_Interface::CAN_Interface(QString dev, QObject *parent) if (!m_canDevice) { // Error handling goes here - spdlog::critical("Runtime error: CAN Interface: {}", errorString.toStdString()); + SPDLOG_CRITICAL("Runtime error: CAN Interface: {}", errorString.toStdString()); throw std::runtime_error("CAN Interface: " + errorString.toStdString()); } @@ -50,7 +50,7 @@ void CAN_Interface::processErrors(QCanBusDevice::CanBusError error) const case QCanBusDevice::ConnectionError: case QCanBusDevice::ConfigurationError: case QCanBusDevice::UnknownError: - spdlog::critical("CAN Interface: {} {}", magic_enum::enum_name(error), m_canDevice->errorString().toStdString()); + SPDLOG_CRITICAL("CAN Interface: {} {}", magic_enum::enum_name(error), m_canDevice->errorString().toStdString()); break; default: break; diff --git a/server/main.cpp b/server/main.cpp index 1b53874..30865e1 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -3,7 +3,6 @@ #include #include "spdlog/async.h" -#include "spdlog/sinks/rotating_file_sink.h" #include "spdlog/sinks/stdout_color_sinks.h" #include "spdlog/spdlog.h" @@ -19,7 +18,7 @@ void init_logger() auto logger = std::make_shared("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] [%^---%L---%$] %v"); + spdlog::set_pattern("[%H:%M:%S %z] [%^---%L---%$] %@: %v"); spdlog::flush_every(std::chrono::seconds(3)); spdlog::set_level(spdlog::level::trace); } @@ -45,7 +44,6 @@ int main(int argc, char *argv[]) parser.process(a); uint16_t port = parser.value(port_option).toUShort(); QString can_port = parser.value(can_port_option); - spdlog::info(can_port.toStdString()); // Initialize the logger init_logger(); diff --git a/server/message_parser.cpp b/server/message_parser.cpp index b0dde51..500c22a 100644 --- a/server/message_parser.cpp +++ b/server/message_parser.cpp @@ -29,27 +29,27 @@ json Simulators::command_parser(json command) switch (cmd) { case Command::get_UI_element: - spdlog::info("Command {}: {}", type, (size_t)command.at("id")); + SPDLOG_INFO("Command {}: {}", type, (size_t)command.at("id")); j["response"]["type"] = type; j["response"]["UI_item"] = invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(command.at("id")); break; case Command::get_UI_elements: - spdlog::info("Command {}", type); + SPDLOG_INFO("Command {}", type); j["response"]["type"] = type; j["response"]["UI_items"] = invoke_active_simulator()->get_UI_items()["UI_items"]; break; case Command::switch_simulator: - spdlog::info("Command {} to {}", type, std::string(command.at("name"))); + SPDLOG_INFO("Command {} to {}", type, std::string(command.at("name"))); switch_simulator(command.at("name")); j["response"]["type"] = type; break; case Command::get_active_simulator_name: - spdlog::info("Command {}", type); + SPDLOG_INFO("Command {}", type); j["response"]["type"] = type; j["response"]["name"] = active_simulator_name(); break; case Command::get_simulators: - spdlog::info("Command {}", type); + SPDLOG_INFO("Command {}", type); j["response"]["type"] = type; j["response"]["simulators"] = list_simulators(); break; @@ -59,7 +59,7 @@ json Simulators::command_parser(json command) } catch (const json::exception &e) { - spdlog::error("{} {}", __FUNCTION__, e.what()); + SPDLOG_ERROR("{} {}", __FUNCTION__, e.what()); } return j; @@ -73,45 +73,45 @@ void Simulators::event_handler(json event) Event e = magic_enum::enum_cast(type).value_or(Event::end); if (invoke_active_simulator() == nullptr) { - spdlog::error("Event got called but there is no active simulator"); + SPDLOG_ERROR("Event got called but there is no active simulator"); return; } switch (e) { case Event::clicked: { - spdlog::debug("Event {}: {}", type, (size_t)event.at("id")); + SPDLOG_DEBUG("Event {}: {}", type, (size_t)event.at("id")); invoke_active_simulator()->get_UI_item(event.at("id"))->click(); break; } case Event::value_changed: { - spdlog::debug("Event {}: {} {}", type, (size_t)event.at("id"), (double)(event["value"])); + SPDLOG_DEBUG("Event {}: {} {}", type, (size_t)event.at("id"), (double)(event["value"])); invoke_active_simulator()->get_UI_item(event.at("id"))->set_value((double)event.at("value")); break; } case Event::text_changed: { - spdlog::debug("Event {}: {} {}", type, (size_t)event.at("id"), std::string(event.at("text"))); + SPDLOG_DEBUG("Event {}: {} {}", type, (size_t)event.at("id"), std::string(event.at("text"))); invoke_active_simulator() ->get_UI_item(event.at("id")) ->set_text(std::string(event.at("text"))); break; } case Event::selected: { - spdlog::debug("Event {}: {} {}", type, (size_t)event.at("id"), std::string(event.at("selected"))); + SPDLOG_DEBUG("Event {}: {} {}", type, (size_t)event.at("id"), std::string(event.at("selected"))); invoke_active_simulator() ->get_UI_item(event.at("id")) ->set_selected(std::string(event.at("selected"))); break; } case Event::can_send: { - spdlog::debug("Event {}: {}", type, (size_t)event.at("id")); + SPDLOG_DEBUG("Event {}: {}", type, (size_t)event.at("id")); invoke_active_simulator() ->get_UI_item(event.at("id")) ->can_send((uint32_t)event.at("sid"), (uint8_t)event.at("dlc"), (std::array)event.at("payload")); break; } case Event::can_clear: { - spdlog::debug("Event {}: {}", type, (size_t)event.at("id")); + SPDLOG_DEBUG("Event {}: {}", type, (size_t)event.at("id")); invoke_active_simulator()->get_UI_item(event.at("id"))->can_clear(); break; } @@ -122,6 +122,6 @@ void Simulators::event_handler(json event) } catch (const json::exception &e) { - spdlog::error("{} {}", __FUNCTION__, e.what()); + SPDLOG_ERROR("{} {}", __FUNCTION__, e.what()); } } diff --git a/server/simulators.cpp b/server/simulators.cpp index 9b7fcd8..e2c0339 100644 --- a/server/simulators.cpp +++ b/server/simulators.cpp @@ -30,12 +30,12 @@ void Simulators::run() { if (this->m_simulators.empty()) { - spdlog::warn("no simulators found"); + SPDLOG_WARN("no simulators found"); return; } if (this->m_current_simulator.empty()) { - spdlog::warn("no active simulator"); + SPDLOG_WARN("no active simulator"); return; } @@ -47,12 +47,12 @@ void Simulators::stop() { if (this->m_simulators.empty()) { - spdlog::warn("no simulators found"); + SPDLOG_WARN("no simulators found"); return; } if (this->m_current_simulator.empty()) { - spdlog::warn("no active simulator"); + SPDLOG_WARN("no active simulator"); return; } @@ -63,18 +63,18 @@ void Simulators::switch_simulator(std::string name) { if (this->m_simulators.empty()) { - spdlog::warn("no simulators found"); + SPDLOG_WARN("no simulators found"); return; } if (!this->m_current_simulator.empty()) { - spdlog::info("stopping current simulator {} and switching to {}", m_current_simulator, name); + SPDLOG_INFO("stopping current simulator {} and switching to {}", m_current_simulator, name); this->m_simulators.at(m_current_simulator)->stop(); } if (this->m_simulators.find(name) == this->m_simulators.end()) { - spdlog::warn("simulator {} not found", name); + SPDLOG_WARN("simulator {} not found", name); return; } @@ -160,7 +160,7 @@ json Simulators::changed_UI_items() } catch (const std::exception &e) { - spdlog::error(e.what()); + SPDLOG_ERROR(e.what()); } m_before = after; diff --git a/server/simulators/test_sim.cpp b/server/simulators/test_sim.cpp index 2a2b382..a5e462d 100644 --- a/server/simulators/test_sim.cpp +++ b/server/simulators/test_sim.cpp @@ -44,7 +44,7 @@ void Test_Sim::timer() void Test_Sim::run_at_startup() { // Run at startup - spdlog::info("Test_Sim started"); + SPDLOG_INFO("Test_Sim started"); } void Test_Sim::button_clicked(UI_button* id) @@ -64,7 +64,7 @@ void Test_Sim::slider_changed(UI_slider* id, double value) { if (id == &this->m_slider) { - spdlog::info("Test slider changed to {}", value); + SPDLOG_INFO("Test slider changed to {}", value); this->m_label.set_text(std::to_string(value)); this->m_dial.set_value(value); this->m_thermo.set_value(value); diff --git a/server/websocket.cpp b/server/websocket.cpp index 20c2eef..971d341 100644 --- a/server/websocket.cpp +++ b/server/websocket.cpp @@ -9,7 +9,7 @@ Websocket::Websocket(uint16_t port, QObject *parent) { if (m_pWebSocketServer->listen(QHostAddress::Any, port)) { - spdlog::debug("Server listening on port {}", port); + SPDLOG_DEBUG("Server listening on port {}", port); connect(m_pWebSocketServer, &QWebSocketServer::newConnection, this, &Websocket::onNewConnection); connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &Websocket::closed); } @@ -32,7 +32,7 @@ void Websocket::broadcast(QString message) void Websocket::send(QWebSocket *conn, QString message) { conn->sendTextMessage(message); - spdlog::trace("sending message: {}", message.toStdString()); + SPDLOG_TRACE("sending message: {}", message.toStdString()); } void Websocket::onNewConnection() @@ -44,20 +44,20 @@ void Websocket::onNewConnection() connect(pSocket, &QWebSocket::disconnected, this, &Websocket::socketDisconnected); m_clients << pSocket; - spdlog::debug("socket connected"); + SPDLOG_DEBUG("socket connected"); } void Websocket::processTextMessage(QString message) { QWebSocket *pClient = qobject_cast(sender()); - spdlog::trace("Message received: {}", message.toStdString()); + SPDLOG_TRACE("Message received: {}", message.toStdString()); emit on_message(pClient, message); } void Websocket::processBinaryMessage(QByteArray message) { QWebSocket *pClient = qobject_cast(sender()); - spdlog::trace("Binary Message received: {}", message.data()); + SPDLOG_TRACE("Binary Message received: {}", message.data()); Q_UNUSED(message); Q_UNUSED(pClient); } @@ -65,7 +65,7 @@ void Websocket::processBinaryMessage(QByteArray message) void Websocket::socketDisconnected() { QWebSocket *pClient = qobject_cast(sender()); - spdlog::debug("socket Disconnected"); + SPDLOG_DEBUG("socket Disconnected"); if (pClient) { m_clients.removeAll(pClient);