diff --git a/NetworkClient.cpp b/NetworkClient.cpp index 4415edf50..52f24f967 100644 --- a/NetworkClient.cpp +++ b/NetworkClient.cpp @@ -10,6 +10,7 @@ \*---------------------------------------------------------*/ #include +#include "LogManager.h" #include "NetworkClient.h" #include "RGBController_Network.h" #include "StringUtils.h" @@ -37,6 +38,11 @@ const char yes = 1; using namespace std::chrono_literals; +/*---------------------------------------------------------*\ +| NetworkClient name for log entries | +\*---------------------------------------------------------*/ +const char* NETWORKCLIENT = "NetworkClient"; + NetworkClient::NetworkClient() { port_ip = "127.0.0.1"; @@ -692,7 +698,7 @@ void NetworkClient::ConnectionThreadFunction() if(port.tcp_client_connect() == true) { client_sock = port.sock; - printf( "Connected to server\n" ); + LOG_INFO("[%s] Connected to server", NETWORKCLIENT); /*---------------------------------------------------------*\ | Server is now connected | @@ -716,7 +722,7 @@ void NetworkClient::ConnectionThreadFunction() } else { - printf( "Connection attempt failed\n" ); + LOG_INFO("[%s] Connection attempt failed", NETWORKCLIENT); } } @@ -838,7 +844,7 @@ void NetworkClient::ConnectionThreadFunction() if(rgb_controller == NULL) { - printf("Client: Requesting controller ID %d\r\n", server_controller_ids[requested_controller_index]); + LOG_INFO("[%s] Requesting controller ID %d", NETWORKCLIENT, server_controller_ids[requested_controller_index]); controller_data_received = false; SendRequest_ControllerData(id); @@ -881,7 +887,7 @@ void NetworkClient::ConnectionThreadFunction() void NetworkClient::ListenThreadFunction() { - printf("Network client listener started\n"); + LOG_INFO("[%s] Listener thread started", NETWORKCLIENT); /*---------------------------------------------------------*\ | This thread handles messages received from the server | @@ -1029,7 +1035,7 @@ void NetworkClient::ListenThreadFunction() } listen_done: - printf( "Client socket has been closed"); + LOG_INFO("[%s] Client socket has been closed", NETWORKCLIENT); client_string_sent = false; controller_data_requested = false; controller_data_received = false; @@ -1158,7 +1164,7 @@ void NetworkClient::ProcessReply_ControllerIDs(unsigned int data_size, char * da else { server_controller_ids.clear(); - printf("Client: Received incorrect packet size for controller list, list cleared"); + LOG_INFO("[%s] Received incorrect packet size for controller list, list cleared", NETWORKCLIENT); } } /*-------------------------------------------------*\ diff --git a/NetworkServer.cpp b/NetworkServer.cpp index c21d75a5c..a4a6d700a 100644 --- a/NetworkServer.cpp +++ b/NetworkServer.cpp @@ -43,6 +43,11 @@ const char yes = 1; using namespace std::chrono_literals; +/*---------------------------------------------------------*\ +| NetworkServer name for log entries | +\*---------------------------------------------------------*/ +const char* NETWORKSERVER = "NetworkServer"; + NetworkClientInfo::NetworkClientInfo() { client_string = "Client"; @@ -56,7 +61,7 @@ NetworkClientInfo::~NetworkClientInfo() { if(client_sock != INVALID_SOCKET) { - LOG_INFO("[NetworkServer] Closing server connection: %s", client_ip.c_str()); + LOG_INFO("[%s] Closing server connection: %s", NETWORKSERVER, client_ip.c_str()); delete client_listen_thread; shutdown(client_sock, SD_RECEIVE); closesocket(client_sock); @@ -304,7 +309,7 @@ void NetworkServer::StartServer() if(err) { - LOG_ERROR("[NetworkServer] Unable to get address."); + LOG_ERROR("[%s] Unable to get address.", NETWORKSERVER); WSACleanup(); return; } @@ -318,7 +323,7 @@ void NetworkServer::StartServer() if(server_sock[socket_count] == INVALID_SOCKET) { - LOG_ERROR("[NetworkServer] Network socket could not be created."); + LOG_ERROR("[%s] Network socket could not be created.", NETWORKSERVER); WSACleanup(); return; } @@ -335,23 +340,23 @@ void NetworkServer::StartServer() { if(errno == EADDRINUSE) { - LOG_ERROR("[NetworkServer] Could not bind network socket. Is port %hu already being used?", GetPort()); + LOG_ERROR("[%s] Could not bind network socket. Is port %hu already being used?", NETWORKSERVER, GetPort()); } else if(errno == EACCES) { - LOG_ERROR("[NetworkServer] Could not bind network socket. Access to socket was denied."); + LOG_ERROR("[%s] Could not bind network socket. Access to socket was denied.", NETWORKSERVER); } else if(errno == EBADF) { - LOG_ERROR("[NetworkServer] Could not bind network socket. sockfd is not a valid file descriptor."); + LOG_ERROR("[%s] Could not bind network socket. sockfd is not a valid file descriptor.", NETWORKSERVER); } else if(errno == EINVAL) { - LOG_ERROR("[NetworkServer] Could not bind network socket. The socket is already bound to an address, or addrlen is wrong, or addr is not a valid address for this socket's domain."); + LOG_ERROR("[%s] Could not bind network socket. The socket is already bound to an address, or addrlen is wrong, or addr is not a valid address for this socket's domain.", NETWORKSERVER); } else if(errno == ENOTSOCK) { - LOG_ERROR("[NetworkServer] Could not bind network socket. The file descriptor sockfd does not refer to a socket."); + LOG_ERROR("[%s] Could not bind network socket. The file descriptor sockfd does not refer to a socket.", NETWORKSERVER); } else { @@ -359,7 +364,7 @@ void NetworkServer::StartServer() | errno could be a Linux specific error, see: | | https://man7.org/linux/man-pages/man2/bind.2.html | \*---------------------------------------------------------*/ - LOG_ERROR("[NetworkServer] Could not bind network socket. Error code: %d.", errno); + LOG_ERROR("[%s] Could not bind network socket. Error code: %d.", NETWORKSERVER, errno); } WSACleanup(); @@ -705,7 +710,7 @@ void NetworkServer::ConnectionThreadFunction(int socket_idx) /*-----------------------------------------------------*\ | This thread handles client connections | \*-----------------------------------------------------*/ - LOG_INFO("[NetworkServer] Network connection thread started on port %hu", GetPort()); + LOG_INFO("[%s] Network connection thread started on port %hu", NETWORKSERVER, GetPort()); while(server_online == true) { @@ -721,7 +726,7 @@ void NetworkServer::ConnectionThreadFunction(int socket_idx) \*---------------------------------------------------------*/ if(listen(server_sock[socket_idx], 10) < 0) { - LOG_INFO("[NetworkServer] Connection thread closed"); + LOG_INFO("[%s] Connection thread closed", NETWORKSERVER); server_online = false; return; @@ -737,7 +742,7 @@ void NetworkServer::ConnectionThreadFunction(int socket_idx) if(client_info->client_sock < 0) { - LOG_INFO("[NetworkServer] Connection thread closed"); + LOG_INFO("[%s] Connection thread closed", NETWORKSERVER); server_online = false; server_listening = false; @@ -796,7 +801,7 @@ void NetworkServer::ConnectionThreadFunction(int socket_idx) SignalClientInfoChanged(); } - LOG_INFO("[NetworkServer] Connection thread closed"); + LOG_INFO("[%s] Connection thread closed", NETWORKSERVER); server_online = false; server_listening = false; SignalServerListeningChanged(); @@ -865,7 +870,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info) { SOCKET client_sock = client_info->client_sock; - LOG_INFO("[NetworkServer] Network server started"); + LOG_INFO("[%s] Network server started", NETWORKSERVER); /*-----------------------------------------------------*\ | This thread handles messages received from clients | @@ -886,7 +891,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info) if(bytes_read <= 0) { - LOG_ERROR("[NetworkServer] recv_select failed receiving magic, closing listener"); + LOG_ERROR("[%s] recv_select failed receiving magic, closing listener", NETWORKSERVER); goto listen_done; } @@ -895,7 +900,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info) \*---------------------------------------------*/ if(header.pkt_magic[i] != openrgb_sdk_magic[i]) { - LOG_ERROR("[NetworkServer] Invalid magic received"); + LOG_ERROR("[%s] Invalid magic received", NETWORKSERVER); continue; } } @@ -915,7 +920,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info) if(tmp_bytes_read <= 0) { - LOG_ERROR("[NetworkServer] recv_select failed receiving header, closing listener"); + LOG_ERROR("[%s] recv_select failed receiving header, closing listener", NETWORKSERVER); goto listen_done; } @@ -937,7 +942,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info) if(tmp_bytes_read <= 0) { - LOG_ERROR("[NetworkServer] recv_select failed receiving data, closing listener"); + LOG_ERROR("[%s] recv_select failed receiving data, closing listener", NETWORKSERVER); goto listen_done; } bytes_read += tmp_bytes_read; @@ -1201,7 +1206,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info) } else { - LOG_ERROR("[NetworkServer] ResizeZone packet has invalid size. Packet size: %d", header.pkt_size); + LOG_ERROR("[%s] ResizeZone packet has invalid size. Packet size: %d", header.pkt_size, NETWORKSERVER); goto listen_done; } break; @@ -1282,7 +1287,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info) } else { - LOG_ERROR("[NetworkServer] RGBController packet with ID %d has invalid size. Packet size: %d,", header.pkt_id, header.pkt_size); + LOG_ERROR("[%s] RGBController packet with ID %d has invalid size. Packet size: %d,", header.pkt_id, header.pkt_size, NETWORKSERVER); goto listen_done; } break; @@ -1300,7 +1305,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info) } else { - LOG_ERROR("[NetworkServer] UpdateSingleLED packet has invalid size. Packet size: %d", header.pkt_size); + LOG_ERROR("[%s] UpdateSingleLED packet has invalid size. Packet size: %d", header.pkt_size, NETWORKSERVER); goto listen_done; } break; @@ -1320,7 +1325,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info) } else { - LOG_ERROR("[NetworkServer] ClearSegments packet has invalid size. Packet size: %d", header.pkt_size); + LOG_ERROR("[%s] ClearSegments packet has invalid size. Packet size: %d", header.pkt_size, NETWORKSERVER); goto listen_done; } break; @@ -1339,7 +1344,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info) } else { - LOG_ERROR("[NetworkServer] AddSegment packet has invalid size. Packet size: %d", header.pkt_size); + LOG_ERROR("[%s] AddSegment packet has invalid size. Packet size: %d", header.pkt_size, NETWORKSERVER); goto listen_done; } break;