Use SPDLOG_* to use file:line

This commit is contained in:
2026-02-02 14:29:45 +01:00
parent f9a7cee71b
commit 597c3c5487
6 changed files with 34 additions and 36 deletions
+3 -3
View File
@@ -6,7 +6,7 @@ CAN_Interface::CAN_Interface(QString dev, QObject *parent)
: QObject(parent) : QObject(parent)
{ {
if (!QCanBus::instance()->plugins().contains(QStringLiteral("socketcan"))) { 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"); throw std::runtime_error("CAN Interface: socketcan plugin not found");
} }
QString errorString; QString errorString;
@@ -14,7 +14,7 @@ CAN_Interface::CAN_Interface(QString dev, QObject *parent)
if (!m_canDevice) if (!m_canDevice)
{ {
// Error handling goes here // 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()); 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::ConnectionError:
case QCanBusDevice::ConfigurationError: case QCanBusDevice::ConfigurationError:
case QCanBusDevice::UnknownError: case QCanBusDevice::UnknownError:
spdlog::critical("CAN Interface: {} {}", magic_enum::enum_name<QCanBusDevice::CanBusError>(error), m_canDevice->errorString().toStdString()); SPDLOG_CRITICAL("CAN Interface: {} {}", magic_enum::enum_name<QCanBusDevice::CanBusError>(error), m_canDevice->errorString().toStdString());
break; break;
default: default:
break; break;
+1 -3
View File
@@ -3,7 +3,6 @@
#include <QtCore/QCoreApplication> #include <QtCore/QCoreApplication>
#include "spdlog/async.h" #include "spdlog/async.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h" #include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
@@ -19,7 +18,7 @@ void init_logger()
auto logger = std::make_shared<spdlog::async_logger>("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(), auto logger = std::make_shared<spdlog::async_logger>("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(),
spdlog::async_overflow_policy::block); spdlog::async_overflow_policy::block);
spdlog::set_default_logger(logger); 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::flush_every(std::chrono::seconds(3));
spdlog::set_level(spdlog::level::trace); spdlog::set_level(spdlog::level::trace);
} }
@@ -45,7 +44,6 @@ int main(int argc, char *argv[])
parser.process(a); parser.process(a);
uint16_t port = parser.value(port_option).toUShort(); uint16_t port = parser.value(port_option).toUShort();
QString can_port = parser.value(can_port_option); QString can_port = parser.value(can_port_option);
spdlog::info(can_port.toStdString());
// Initialize the logger // Initialize the logger
init_logger(); init_logger();
+14 -14
View File
@@ -29,27 +29,27 @@ json Simulators::command_parser(json command)
switch (cmd) switch (cmd)
{ {
case Command::get_UI_element: 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"]["type"] = type;
j["response"]["UI_item"] = invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(command.at("id")); j["response"]["UI_item"] = invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(command.at("id"));
break; break;
case Command::get_UI_elements: case Command::get_UI_elements:
spdlog::info("Command {}", type); SPDLOG_INFO("Command {}", type);
j["response"]["type"] = type; j["response"]["type"] = type;
j["response"]["UI_items"] = invoke_active_simulator()->get_UI_items()["UI_items"]; j["response"]["UI_items"] = invoke_active_simulator()->get_UI_items()["UI_items"];
break; break;
case Command::switch_simulator: 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")); switch_simulator(command.at("name"));
j["response"]["type"] = type; j["response"]["type"] = type;
break; break;
case Command::get_active_simulator_name: case Command::get_active_simulator_name:
spdlog::info("Command {}", type); SPDLOG_INFO("Command {}", type);
j["response"]["type"] = type; j["response"]["type"] = type;
j["response"]["name"] = active_simulator_name(); j["response"]["name"] = active_simulator_name();
break; break;
case Command::get_simulators: case Command::get_simulators:
spdlog::info("Command {}", type); SPDLOG_INFO("Command {}", type);
j["response"]["type"] = type; j["response"]["type"] = type;
j["response"]["simulators"] = list_simulators(); j["response"]["simulators"] = list_simulators();
break; break;
@@ -59,7 +59,7 @@ json Simulators::command_parser(json command)
} }
catch (const json::exception &e) catch (const json::exception &e)
{ {
spdlog::error("{} {}", __FUNCTION__, e.what()); SPDLOG_ERROR("{} {}", __FUNCTION__, e.what());
} }
return j; return j;
@@ -73,45 +73,45 @@ void Simulators::event_handler(json event)
Event e = magic_enum::enum_cast<Event>(type).value_or(Event::end); Event e = magic_enum::enum_cast<Event>(type).value_or(Event::end);
if (invoke_active_simulator() == nullptr) 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; return;
} }
switch (e) switch (e)
{ {
case Event::clicked: { 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(); invoke_active_simulator()->get_UI_item(event.at("id"))->click();
break; break;
} }
case Event::value_changed: { 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")); invoke_active_simulator()->get_UI_item(event.at("id"))->set_value((double)event.at("value"));
break; break;
} }
case Event::text_changed: { 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() invoke_active_simulator()
->get_UI_item(event.at("id")) ->get_UI_item(event.at("id"))
->set_text(std::string(event.at("text"))); ->set_text(std::string(event.at("text")));
break; break;
} }
case Event::selected: { 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() invoke_active_simulator()
->get_UI_item(event.at("id")) ->get_UI_item(event.at("id"))
->set_selected(std::string(event.at("selected"))); ->set_selected(std::string(event.at("selected")));
break; break;
} }
case Event::can_send: { 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() invoke_active_simulator()
->get_UI_item(event.at("id")) ->get_UI_item(event.at("id"))
->can_send((uint32_t)event.at("sid"), (uint8_t)event.at("dlc"), (std::array<uint8_t, 8>)event.at("payload")); ->can_send((uint32_t)event.at("sid"), (uint8_t)event.at("dlc"), (std::array<uint8_t, 8>)event.at("payload"));
break; break;
} }
case Event::can_clear: { 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(); invoke_active_simulator()->get_UI_item(event.at("id"))->can_clear();
break; break;
} }
@@ -122,6 +122,6 @@ void Simulators::event_handler(json event)
} }
catch (const json::exception &e) catch (const json::exception &e)
{ {
spdlog::error("{} {}", __FUNCTION__, e.what()); SPDLOG_ERROR("{} {}", __FUNCTION__, e.what());
} }
} }
+8 -8
View File
@@ -30,12 +30,12 @@ void Simulators::run()
{ {
if (this->m_simulators.empty()) if (this->m_simulators.empty())
{ {
spdlog::warn("no simulators found"); SPDLOG_WARN("no simulators found");
return; return;
} }
if (this->m_current_simulator.empty()) if (this->m_current_simulator.empty())
{ {
spdlog::warn("no active simulator"); SPDLOG_WARN("no active simulator");
return; return;
} }
@@ -47,12 +47,12 @@ void Simulators::stop()
{ {
if (this->m_simulators.empty()) if (this->m_simulators.empty())
{ {
spdlog::warn("no simulators found"); SPDLOG_WARN("no simulators found");
return; return;
} }
if (this->m_current_simulator.empty()) if (this->m_current_simulator.empty())
{ {
spdlog::warn("no active simulator"); SPDLOG_WARN("no active simulator");
return; return;
} }
@@ -63,18 +63,18 @@ void Simulators::switch_simulator(std::string name)
{ {
if (this->m_simulators.empty()) if (this->m_simulators.empty())
{ {
spdlog::warn("no simulators found"); SPDLOG_WARN("no simulators found");
return; return;
} }
if (!this->m_current_simulator.empty()) 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(); this->m_simulators.at(m_current_simulator)->stop();
} }
if (this->m_simulators.find(name) == this->m_simulators.end()) if (this->m_simulators.find(name) == this->m_simulators.end())
{ {
spdlog::warn("simulator {} not found", name); SPDLOG_WARN("simulator {} not found", name);
return; return;
} }
@@ -160,7 +160,7 @@ json Simulators::changed_UI_items()
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
spdlog::error(e.what()); SPDLOG_ERROR(e.what());
} }
m_before = after; m_before = after;
+2 -2
View File
@@ -44,7 +44,7 @@ void Test_Sim::timer()
void Test_Sim::run_at_startup() void Test_Sim::run_at_startup()
{ {
// Run at startup // Run at startup
spdlog::info("Test_Sim started"); SPDLOG_INFO("Test_Sim started");
} }
void Test_Sim::button_clicked(UI_button* id) 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) 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_label.set_text(std::to_string(value));
this->m_dial.set_value(value); this->m_dial.set_value(value);
this->m_thermo.set_value(value); this->m_thermo.set_value(value);
+6 -6
View File
@@ -9,7 +9,7 @@ Websocket::Websocket(uint16_t port, QObject *parent)
{ {
if (m_pWebSocketServer->listen(QHostAddress::Any, port)) 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::newConnection, this, &Websocket::onNewConnection);
connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &Websocket::closed); connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &Websocket::closed);
} }
@@ -32,7 +32,7 @@ void Websocket::broadcast(QString message)
void Websocket::send(QWebSocket *conn, QString message) void Websocket::send(QWebSocket *conn, QString message)
{ {
conn->sendTextMessage(message); conn->sendTextMessage(message);
spdlog::trace("sending message: {}", message.toStdString()); SPDLOG_TRACE("sending message: {}", message.toStdString());
} }
void Websocket::onNewConnection() void Websocket::onNewConnection()
@@ -44,20 +44,20 @@ void Websocket::onNewConnection()
connect(pSocket, &QWebSocket::disconnected, this, &Websocket::socketDisconnected); connect(pSocket, &QWebSocket::disconnected, this, &Websocket::socketDisconnected);
m_clients << pSocket; m_clients << pSocket;
spdlog::debug("socket connected"); SPDLOG_DEBUG("socket connected");
} }
void Websocket::processTextMessage(QString message) void Websocket::processTextMessage(QString message)
{ {
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
spdlog::trace("Message received: {}", message.toStdString()); SPDLOG_TRACE("Message received: {}", message.toStdString());
emit on_message(pClient, message); emit on_message(pClient, message);
} }
void Websocket::processBinaryMessage(QByteArray message) void Websocket::processBinaryMessage(QByteArray message)
{ {
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
spdlog::trace("Binary Message received: {}", message.data()); SPDLOG_TRACE("Binary Message received: {}", message.data());
Q_UNUSED(message); Q_UNUSED(message);
Q_UNUSED(pClient); Q_UNUSED(pClient);
} }
@@ -65,7 +65,7 @@ void Websocket::processBinaryMessage(QByteArray message)
void Websocket::socketDisconnected() void Websocket::socketDisconnected()
{ {
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
spdlog::debug("socket Disconnected"); SPDLOG_DEBUG("socket Disconnected");
if (pClient) if (pClient)
{ {
m_clients.removeAll(pClient); m_clients.removeAll(pClient);