From 06f1d08fdb993b63ca2a2440aad8333e50bda7b5 Mon Sep 17 00:00:00 2001 From: Johnathon Slightham Date: Sun, 5 Oct 2025 22:00:19 -0400 Subject: [PATCH] Cleanup logging --- components/rpc/CommunicationRouter.cpp | 27 +++++++------- components/rpc/OrientationDetection.cpp | 16 ++++----- components/rpc/TCPServer.cpp | 47 ++++++++++++------------- components/rpc/WifiManager.cpp | 24 +++++++------ 4 files changed, 57 insertions(+), 57 deletions(-) diff --git a/components/rpc/CommunicationRouter.cpp b/components/rpc/CommunicationRouter.cpp index 3717aa2..7729747 100644 --- a/components/rpc/CommunicationRouter.cpp +++ b/components/rpc/CommunicationRouter.cpp @@ -1,16 +1,17 @@ -#include "CommunicationRouter.h" - -#include #include + +#include "CommunicationRouter.h" +#include "AngleControlMessageBuilder.h" #include "mDNSDiscoveryService.h" #include "MPIMessageBuilder.h" #include "WifiManager.h" #include "freertos/FreeRTOS.h" -#include "freertos/queue.h" #include "Tables.h" #include "PtrQueue.h" #include "OrientationDetection.h" +#define TAG "CommunicationRouter" + CommunicationRouter::~CommunicationRouter() { vTaskDelete(m_router_thread); } @@ -24,7 +25,7 @@ CommunicationRouter::~CommunicationRouter() { while (true) { const auto buffer = that->m_tcp_rx_queue->dequeue(); - std::cout << "routing from tcp" << std::endl; + ESP_LOGD(TAG, "Got message from TCP"); that->route(buffer->data(), buffer->size()); } } @@ -35,10 +36,10 @@ CommunicationRouter::~CommunicationRouter() { const auto channel = params->channel; delete params; - char buffer[512]; size_t bytes_received = 0; that->m_data_link_manager->start_receive_frames(channel); while (true) { + char buffer[512]; // todo: very c style function calls const auto err = that->m_data_link_manager->receive(reinterpret_cast(buffer), 512, &bytes_received, channel); that->m_data_link_manager->start_receive_frames(channel); @@ -46,7 +47,7 @@ CommunicationRouter::~CommunicationRouter() { // todo: do we only want one thread ever doing this? if (std::chrono::system_clock::now() - that->m_last_leader_updated > std::chrono::seconds(15)) { that->m_last_leader_updated = std::chrono::system_clock::now(); - std::cout << "Updating leader" << std::endl; + ESP_LOGI(TAG, "Updating leader"); that->update_leader(); } @@ -54,12 +55,13 @@ CommunicationRouter::~CommunicationRouter() { continue; } - std::cout << "routing message from rmt" << std::endl; + ESP_LOGD(TAG, "Got message from RMT"); that->route(reinterpret_cast(buffer), bytes_received); } } int CommunicationRouter::send_msg(char* buffer, const size_t length) const { + ESP_LOGD(TAG, "Got message from application"); route(reinterpret_cast(buffer), length); return 0; } @@ -101,17 +103,16 @@ void CommunicationRouter::route(uint8_t* buffer, const size_t length) const { const auto& mpi_message = Flatbuffers::MPIMessageBuilder::parse_mpi_message(buffer); if (mpi_message->destination() == m_module_id) { - std::cout << "Routing to this module [dest:" << static_cast(mpi_message->destination()) << ", length: " << length << "]" << std::endl; - + ESP_LOGD(TAG, "Routing to this module [dest: %d, length: %d]", static_cast(mpi_message->destination()), length); this->m_rx_callback(reinterpret_cast(buffer), 512); } else if (mpi_message->destination() == PC_ADDR && this->m_leader == m_module_id) { - std::cout << "Routing to wifi [dest:" << static_cast(mpi_message->destination()) << ", length: " << length << "]" << std::endl; + ESP_LOGD(TAG, "Routing to wifi [dest: %d, length: %d]", static_cast(mpi_message->destination()), length); this->m_tcp_server->send_msg(reinterpret_cast(buffer), 512); } else if (mpi_message->destination() == PC_ADDR) { - std::cout << "Routing to wireline for wifi [dest:" << static_cast(mpi_message->destination()) << ", length: " << length << "]" << std::endl; + ESP_LOGD(TAG, "Routing to wireline for wifi [dest: %d, length: %d]", static_cast(mpi_message->destination()), length); this->m_data_link_manager->send(this->m_leader, buffer, length, FrameType::MOTOR_TYPE, 0); }else { - std::cout << "Routing to wireline [dest:" << static_cast(mpi_message->destination()) << ", length: " << length << "]" << std::endl; + ESP_LOGD(TAG, "Routing to wireline [dest: %d, length: %d]", static_cast(mpi_message->destination()), length); this->m_data_link_manager->send(mpi_message->destination(), buffer, length, FrameType::MOTOR_TYPE, 0); } } diff --git a/components/rpc/OrientationDetection.cpp b/components/rpc/OrientationDetection.cpp index 3f5f498..bf65bcb 100644 --- a/components/rpc/OrientationDetection.cpp +++ b/components/rpc/OrientationDetection.cpp @@ -2,12 +2,12 @@ // Created by Johnathon Slightham on 2025-07-26. // -#include +#include "constants/module.h" #include "OrientationDetection.h" +#include "ConfigManager.h" +#include "iostream" -#include -#include -#include +#define TAG "OrientationDetection" void OrientationDetection::init() { const auto& config_manager = ConfigManager::get_instance(); @@ -21,16 +21,16 @@ void OrientationDetection::init() { Orientation OrientationDetection::get_orientation(const uint8_t channel) { if (gpio_get_level(static_cast(CHANNEL_TO_90_DEG_MAP[channel]))) { - std::cout << "90deg" << std::endl; + ESP_LOGD(TAG, "90deg"); return Orientation_Deg90; } else if (gpio_get_level(static_cast(CHANNEL_TO_180_DEG_MAP[channel]))) { - std::cout << "180deg" << std::endl; + ESP_LOGD(TAG, "180deg"); return Orientation_Deg180; } else if (gpio_get_level(static_cast(CHANNEL_TO_270_DEG_MAP[channel]))) { - std::cout << "270deg" << std::endl; + ESP_LOGD(TAG, "270deg"); return Orientation_Deg270; } else { - std::cout << "No orientation detected" << std::endl; + ESP_LOGD(TAG, "No orientation detected"); return Orientation_Deg0; } } diff --git a/components/rpc/TCPServer.cpp b/components/rpc/TCPServer.cpp index b20ad9d..23eaffc 100644 --- a/components/rpc/TCPServer.cpp +++ b/components/rpc/TCPServer.cpp @@ -1,24 +1,23 @@ -#include +#include +#include +#include #include + #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_event.h" #include "esp_netif.h" - #include "lwip/err.h" #include "lwip/sockets.h" #include "lwip/sys.h" -#include -#include - +#include "lwip/netdb.h" #include "TCPServer.h" - -#include -#include -#include - +#include "bits/shared_ptr_base.h" +#include "constants/app_comms.h" #include "constants/tcp.h" +#define TAG "TCPServer" + // todo: - add message routing to correct client // - authenticate (don't just return true from the auth function) // - tx from board @@ -51,18 +50,18 @@ TCPServer::~TCPServer() { const auto that = static_cast(args); while (true) { - printf("Attempting to start TCP Server on port %d", that->m_port); + ESP_LOGI(TAG, "Attempting to start TCP Server on port %d", that->m_port); that->m_server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (that->m_server_sock < 0) { - printf("Unable to create TCP socket: errno %d\n", errno); + ESP_LOGE(TAG, "Unable to create TCP socket: errno %d\n", errno); vTaskDelay(SLEEP_AFTER_FAIL_MS / portTICK_PERIOD_MS); continue; } constexpr int opt = 1; setsockopt(that->m_server_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); - printf("Socket created\n"); + ESP_LOGI(TAG, "Socket created\n"); sockaddr_in server_addr = { .sin_family = AF_INET, @@ -74,18 +73,18 @@ TCPServer::~TCPServer() { int err = bind(that->m_server_sock, reinterpret_cast(&server_addr), sizeof(server_addr)); if (0 != err) { - printf("Socket unable to bind: errno %d\n", errno); + ESP_LOGE(TAG, "Socket unable to bind: errno %d\n", errno); close(that->m_server_sock); that->m_server_sock = -1; vTaskDelay(SLEEP_AFTER_FAIL_MS / portTICK_PERIOD_MS); continue; } - printf("Socket bound to port %d\n", that->m_port); + ESP_LOGI(TAG, "Socket bound to port %d\n", that->m_port); err = listen(that->m_server_sock, TCP_DEFAULT_LISTEN_BACKLOG); if (0 != err) { - printf("Error occurred during TCP listen: errno %d\n", errno); + ESP_LOGE(TAG, "Error occurred during TCP listen: errno %d\n", errno); close(that->m_server_sock); that->m_server_sock = -1; vTaskDelay(SLEEP_AFTER_FAIL_MS / portTICK_PERIOD_MS); @@ -97,13 +96,13 @@ TCPServer::~TCPServer() { socklen_t addr_len = sizeof(client_addr); int client_sock = accept(that->m_server_sock, reinterpret_cast(&client_addr), &addr_len); if (client_sock < 0) { - printf("Unable to accept TCP connection: errno %d\n", errno); + ESP_LOGE(TAG, "Unable to accept TCP connection: errno %d\n", errno); continue; } err = that->authenticate_client(client_sock); if (0 != err) { - printf("Client failed authentication\n"); + ESP_LOGE(TAG, "Client failed authentication\n"); } setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(int)); @@ -154,18 +153,17 @@ TCPServer::~TCPServer() { continue; } - printf("Message size: %ld\n", msg_size); + ESP_LOGD(TAG, "Message size: %ld\n", msg_size); - int len = recv(sock, buffer->data(), msg_size, MSG_WAITALL); - if (len < 0) { - printf("Error occurred during receiving: errno %d\n", errno); + if (int len = recv(sock, buffer->data(), msg_size, MSG_WAITALL); len < 0) { + ESP_LOGE(TAG, "Error occurred during receiving: errno %d\n", errno); to_remove.emplace_back(sock); } else if (0 == len) { - printf("Connection closed\n"); + ESP_LOGW(TAG, "Connection closed\n"); close(sock); to_remove.emplace_back(sock); } else { - printf("TCP Server Received %d bytes\n", len); + ESP_LOGD(TAG, "TCP Server Received %d bytes\n", len); buffer->resize(len); that->m_rx_queue->enqueue(std::move(buffer)); } @@ -219,7 +217,6 @@ int TCPServer::send_msg(char *buffer, uint32_t length) const { } for (const auto client_sock : m_clients) { - std::cout << "sending tcp" << std::endl; send(client_sock, &length, 4, 0); send(client_sock, buffer, length, 0); } diff --git a/components/rpc/WifiManager.cpp b/components/rpc/WifiManager.cpp index 69a97fd..9427441 100644 --- a/components/rpc/WifiManager.cpp +++ b/components/rpc/WifiManager.cpp @@ -5,6 +5,8 @@ #include "constants/wifi.h" #include "mDNSDiscoveryService.h" +#define TAG "WifiManager" + WifiManager::~WifiManager() { this->handle_disconnect(); vTaskDelete(this->m_task); @@ -32,21 +34,21 @@ int WifiManager::disconnect() { switch (state) { case wifi_state::connect: - printf("Attempting to connect to wifi in station mode\n"); + ESP_LOGI(TAG, "Attempting to connect to wifi in station mode\n"); init_connection(); update_state(wifi_state::connecting); break; case wifi_state::connecting: - printf("connecting...\n"); + ESP_LOGI(TAG, "connecting...\n"); handle_connecting(); break; case wifi_state::broadcast: - printf("Attempting to broadcast in softap mode\n"); + ESP_LOGI(TAG, "Attempting to broadcast in softap mode\n"); init_softap(); update_state(wifi_state::broadcasting); break; case wifi_state::disconnect: - printf("Shutting down wifi\n"); + ESP_LOGI(TAG, "Shutting down wifi\n"); handle_disconnect(); update_state(wifi_state::disconnected); break; @@ -169,13 +171,13 @@ void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t e const auto that = static_cast(event_handler_arg); if (WIFI_EVENT_STA_START == event_id) { - printf("Station mode started\n"); + ESP_LOGI(TAG, "Station mode started\n"); } else if (WIFI_EVENT_STA_CONNECTED == event_id) { - printf("Connected to wifi in station mode\n"); + ESP_LOGI(TAG, "Connected to wifi in station mode\n"); that->update_state(wifi_state::connected); mDNSDiscoveryService::setup(); } else if (WIFI_EVENT_STA_DISCONNECTED == event_id) { - printf("Station mode shutdown\n"); + ESP_LOGI(TAG, "Station mode shutdown\n"); xSemaphoreTake(that->m_mutex, portMAX_DELAY); if (that->m_state == wifi_state::connected) { xSemaphoreGive(that->m_mutex); @@ -185,13 +187,13 @@ void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t e xSemaphoreGive(that->m_mutex); } } else if (IP_EVENT_STA_GOT_IP == event_id) { - printf("Got IP as station\n"); + ESP_LOGI(TAG, "Got IP as station\n"); } else if (WIFI_EVENT_AP_STACONNECTED == event_id) { - printf("User connected to AP\n"); + ESP_LOGI(TAG, "User connected to AP\n"); } else if (WIFI_EVENT_AP_STADISCONNECTED == event_id) { - printf("User disconnected from AP\n"); + ESP_LOGI(TAG, "User disconnected from AP\n"); } else if (WIFI_EVENT_AP_START == event_id) { mDNSDiscoveryService::setup(); - printf("AP started\n"); + ESP_LOGI(TAG, "AP started\n"); } }