From 9d85cfbf12347d9165a4a99a53bbe30adfc1c694 Mon Sep 17 00:00:00 2001 From: Nodeduino Date: Sun, 19 Nov 2017 21:59:51 +0100 Subject: [PATCH] Refine debug messages in all HttpHandlers --- hueplusplus/include/IHttpHandler.h | 22 +++++++++++----------- hueplusplus/linHttpHandler.cpp | 40 ++++++++++++++++++++-------------------- hueplusplus/winHttpHandler.cpp | 60 ++++++++++++++++++++++++++++++------------------------------ 3 files changed, 61 insertions(+), 61 deletions(-) diff --git a/hueplusplus/include/IHttpHandler.h b/hueplusplus/include/IHttpHandler.h index 6845222..403cae6 100644 --- a/hueplusplus/include/IHttpHandler.h +++ b/hueplusplus/include/IHttpHandler.h @@ -56,8 +56,8 @@ public: size_t start = response.find("\r\n\r\n"); if (start == std::string::npos) { - std::cerr << "Failed to find body in response\n"; - throw(std::runtime_error("Failed to find body in response")); + std::cerr << "IHttpHandler: Failed to find body in response\n"; + throw(std::runtime_error("IHttpHandler: Failed to find body in response")); } response.erase(0, start + 4); return response; @@ -93,7 +93,7 @@ public: request.append(" "); // Separation request.append("HTTP/1.0"); // HTTP-Version request.append("\r\n"); // Ending - // Entities + // Entities request.append("Content-Type:"); // entity-header request.append(" "); // Separation request.append(content_type); // media-type @@ -183,8 +183,8 @@ public: std::unique_ptr reader = std::unique_ptr(builder.newCharReader()); if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) { - std::cout << "Error while parsing JSON in function GETJson() of linHttpHandler: " << error << std::endl; - throw(std::runtime_error("Error while parsing JSON in function GETJson() of linHttpHandler")); + std::cout << "IHttpHandler: Error while parsing JSON in function GETJson(): " << error << std::endl; + throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function GETJson()")); } return result; }; @@ -208,8 +208,8 @@ public: std::unique_ptr reader = std::unique_ptr(builder.newCharReader()); if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) { - std::cout << "Error while parsing JSON in function POSTJson() of linHttpHandler: " << error << std::endl; - throw(std::runtime_error("Error while parsing JSON in function POSTJson() of linHttpHandler")); + std::cout << "IHttpHandler: Error while parsing JSON in function POSTJson(): " << error << std::endl; + throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function POSTJson()")); } return result; } @@ -233,8 +233,8 @@ public: std::unique_ptr reader = std::unique_ptr(builder.newCharReader()); if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) { - std::cout << "Error while parsing JSON in function PUTJson() of linHttpHandler: " << error << std::endl; - throw(std::runtime_error("Error while parsing JSON in function PUTJson() of linHttpHandler")); + std::cout << "IHttpHandler: Error while parsing JSON in function PUTJson(): " << error << std::endl; + throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function PUTJson()")); } return result; }; @@ -258,8 +258,8 @@ public: std::unique_ptr reader = std::unique_ptr(builder.newCharReader()); if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) { - std::cout << "Error while parsing JSON in function PUTJson() of linHttpHandler: " << error << std::endl; - throw(std::runtime_error("Error while parsing JSON in function PUTJson() of linHttpHandler")); + std::cout << "IHttpHandler: Error while parsing JSON in function PUTJson(): " << error << std::endl; + throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function PUTJson()")); } return result; }; diff --git a/hueplusplus/linHttpHandler.cpp b/hueplusplus/linHttpHandler.cpp index 90b6460..47c4c9c 100644 --- a/hueplusplus/linHttpHandler.cpp +++ b/hueplusplus/linHttpHandler.cpp @@ -48,8 +48,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad SocketCloser closeMySocket(socketFD); if (socketFD < 0) { - std::cerr << "Failed to open socket\n"; - throw(std::runtime_error("Failed to open socket")); + std::cerr << "linHttpHandler: Failed to open socket\n"; + throw(std::runtime_error("linHttpHandler: Failed to open socket")); } // lookup ip address @@ -57,8 +57,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad server = gethostbyname(adr.c_str()); if (server == NULL) { - std::cerr << "Failed to find host with address " << adr << "\n"; - throw(std::runtime_error("Failed to find host")); + std::cerr << "linHttpHandler: Failed to find host with address " << adr << "\n"; + throw(std::runtime_error("linHttpHandler: Failed to find host")); } // fill in the structure @@ -71,8 +71,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad // connect the socket if (connect(socketFD, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { - std::cerr << "Failed to connect socket\n"; - throw(std::runtime_error("Failed to connect socket")); + std::cerr << "linHttpHandler: Failed to connect socket\n"; + throw(std::runtime_error("linHttpHandler: Failed to connect socket")); } // send the request @@ -83,8 +83,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad ssize_t bytes = write(socketFD, msg.c_str() + sent, total - sent); if (bytes < 0) { - std::cerr << "Failed to write message to socket\n"; - throw(std::runtime_error("Failed to write message to socket")); + std::cerr << "linHttpHandler: Failed to write message to socket\n"; + throw(std::runtime_error("linHttpHandler: Failed to write message to socket")); } if (bytes == 0) { @@ -106,8 +106,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad ssize_t bytes = read(socketFD, buffer, 127); if (bytes < 0) { - std::cerr << "Failed to read response from socket: " << errno << std::endl; - throw(std::runtime_error("Failed to read response from socket")); + std::cerr << "linHttpHandler: Failed to read response from socket: " << errno << std::endl; + throw(std::runtime_error("linHttpHandler: Failed to read response from socket")); } if (bytes == 0) { @@ -122,8 +122,8 @@ std::string linHttpHandler::send(const std::string & msg, const std::string & ad if (received == total) { - std::cerr << "Failed to store complete response from socket\n"; - throw(std::runtime_error("Failed to store complete response from socket")); + std::cerr << "linHttpHandler: Failed to store complete response from socket\n"; + throw(std::runtime_error("linHttpHandler: Failed to store complete response from socket")); } return response; @@ -143,8 +143,8 @@ std::vector linHttpHandler::sendMulticast(const std::string & msg, server = gethostbyname(adr.c_str()); if (!server) { - std::cerr << "Failed to obtain address of " << msg << "\n"; - throw(std::runtime_error("Failed to obtain address of host")); + std::cerr << "linHttpHandler: sendMulticast: Failed to obtain address of " << msg << "\n"; + throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to obtain address of host")); } // put the host's address into the server address structure @@ -155,15 +155,15 @@ std::vector linHttpHandler::sendMulticast(const std::string & msg, SocketCloser closeMySendSocket(socketFD); if (socketFD < 0) { - std::cerr << "Failed to open socket\n"; - throw(std::runtime_error("Failed to open socket")); + std::cerr << "linHttpHandler: sendMulticast: Failed to open socket\n"; + throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to open socket")); } // send a message to the server if (sendto(socketFD, msg.c_str(), strlen(msg.c_str()), 0, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { - std::cerr << "Failed to send message\n"; - throw(std::runtime_error("Failed to send message")); + std::cerr << "linHttpHandler: sendMulticast: Failed to send message\n"; + throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to send message")); } std::string response; @@ -178,8 +178,8 @@ std::vector linHttpHandler::sendMulticast(const std::string & msg, { if (errno != EAGAIN && errno != EWOULDBLOCK) { - std::cerr << "Failed to read response from socket\n"; - throw(std::runtime_error("Failed to read response from socket")); + std::cerr << "linHttpHandler: sendMulticast: Failed to read response from socket\n"; + throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to read response from socket")); } continue; } diff --git a/hueplusplus/winHttpHandler.cpp b/hueplusplus/winHttpHandler.cpp index cbfd0d7..60a32e7 100644 --- a/hueplusplus/winHttpHandler.cpp +++ b/hueplusplus/winHttpHandler.cpp @@ -34,8 +34,8 @@ winHttpHandler::winHttpHandler() // Initialize Winsock if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { - std::cerr << "Failed to open socket: " << WSAGetLastError() << std::endl; - throw(std::runtime_error("Failed to open socket")); + std::cerr << "winHttpHandler: Failed to open socket: " << WSAGetLastError() << std::endl; + throw(std::runtime_error("winHttpHandler: Failed to open socket")); } } @@ -57,8 +57,8 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad int res = getaddrinfo(adr.c_str(), std::to_string(port).c_str(), &hints, &result); if (res != 0) { - std::cerr << "getaddrinfo failed: " << res << std::endl; - throw(std::runtime_error("getaddrinfo failed")); + std::cerr << "winHttpHandler: getaddrinfo failed: " << res << std::endl; + throw(std::runtime_error("winHttpHandler: getaddrinfo failed")); } SOCKET connect_socket = INVALID_SOCKET; @@ -74,8 +74,8 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad if (connect_socket == INVALID_SOCKET) { freeaddrinfo(result); - std::cerr << "Error at socket(): " << WSAGetLastError() << std::endl; - throw(std::runtime_error("Error at socket()")); + std::cerr << "winHttpHandler: Error at socket(): " << WSAGetLastError() << std::endl; + throw(std::runtime_error("winHttpHandler: Error at socket()")); } // Connect to server. @@ -95,16 +95,16 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad if (connect_socket == INVALID_SOCKET) { - std::cerr << "Unable to connect to server!" << std::endl; - throw(std::runtime_error("Unable to connect to server!")); + std::cerr << "winHttpHandler: Unable to connect to server!" << std::endl; + throw(std::runtime_error("winHttpHandler: Unable to connect to server!")); } // Send an initial buffer res = ::send(connect_socket, msg.c_str(), msg.size(), 0); if (res == SOCKET_ERROR) { - std::cerr << "send failed: " << WSAGetLastError() << std::endl; - throw(std::runtime_error("send failed")); + std::cerr << "winHttpHandler: send failed: " << WSAGetLastError() << std::endl; + throw(std::runtime_error("winHttpHandler: send failed")); } // shutdown the connection for sending since no more data will be sent @@ -113,8 +113,8 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad if (res == SOCKET_ERROR) { closesocket(connect_socket); - std::cerr << "shutdown failed: " << WSAGetLastError() << std::endl; - throw(std::runtime_error("shutdown failed")); + std::cerr << "winHttpHandler: shutdown failed: " << WSAGetLastError() << std::endl; + throw(std::runtime_error("winHttpHandler: shutdown failed")); } const int recvbuflen = 128; @@ -128,17 +128,17 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad res = recv(connect_socket, recvbuf, recvbuflen, 0); if (res > 0) { - std::cout << "Bytes received: " << res << std::endl; + std::cout << "winHttpHandler: Bytes received: " << res << std::endl; response.append(recvbuf, res); } else if (res == 0) { - std::cout << "Connection closed " << std::endl; + std::cout << "winHttpHandler: Connection closed " << std::endl; } else { - std::cerr << "recv failed: " << WSAGetLastError() << std::endl; - throw(std::runtime_error("recv failed")); + std::cerr << "winHttpHandler: recv failed: " << WSAGetLastError() << std::endl; + throw(std::runtime_error("winHttpHandler: recv failed")); } } while (res > 0); @@ -159,8 +159,8 @@ std::vector winHttpHandler::sendMulticast(const std::string & msg, int res = getaddrinfo(adr.c_str(), std::to_string(port).c_str(), &hints, &result); if (res != 0) { - std::cerr << "sendMulticast: getaddrinfo failed: " << res << std::endl; - throw(std::runtime_error("getaddrinfo failed")); + std::cerr << "winHttpHandler: sendMulticast: getaddrinfo failed: " << res << std::endl; + throw(std::runtime_error("winHttpHandler: sendMulticast: getaddrinfo failed")); } SOCKET connect_socket = INVALID_SOCKET; @@ -174,8 +174,8 @@ std::vector winHttpHandler::sendMulticast(const std::string & msg, if ((connect_socket = socket(ptr->ai_family, ptr->ai_socktype, 0)) == INVALID_SOCKET) { freeaddrinfo(result); - std::cerr << "sendMulticast: Error at socket(): " << WSAGetLastError() << std::endl; - throw(std::runtime_error("Error at socket()")); + std::cerr << "winHttpHandler: sendMulticast: Error at socket(): " << WSAGetLastError() << std::endl; + throw(std::runtime_error("winHttpHandler: sendMulticast: Error at socket()")); } // Fill out source socket's address information. @@ -187,8 +187,8 @@ std::vector winHttpHandler::sendMulticast(const std::string & msg, if (bind(connect_socket, (struct sockaddr FAR *) &source_sin, sizeof(source_sin)) == SOCKET_ERROR) { closesocket(connect_socket); - std::cerr << "sendMulticast: Binding socket failed: " << WSAGetLastError() << std::endl; - throw(std::runtime_error("Binding socket failed")); + std::cerr << "winHttpHandler: sendMulticast: Binding socket failed: " << WSAGetLastError() << std::endl; + throw(std::runtime_error("winHttpHandler: sendMulticast: Binding socket failed")); } u_long sock_mode = 1; @@ -202,8 +202,8 @@ std::vector winHttpHandler::sendMulticast(const std::string & msg, if (setsockopt(connect_socket, IPPROTO_IP, IP_MULTICAST_TTL, (char FAR *)&iOptVal, sizeof(int)) == SOCKET_ERROR) { closesocket(connect_socket); - std::cerr << "sendMulticast: setsockopt failed: " << WSAGetLastError() << std::endl; - throw(std::runtime_error("setsockopt failed")); + std::cerr << "winHttpHandler: sendMulticast: setsockopt failed: " << WSAGetLastError() << std::endl; + throw(std::runtime_error("winHttpHandler: sendMulticast: setsockopt failed")); } // Fill out the desination socket's address information. @@ -214,9 +214,9 @@ std::vector winHttpHandler::sendMulticast(const std::string & msg, // Send a message to the multicasting address. if (sendto(connect_socket, msg.c_str(), msg.size(), 0, (struct sockaddr FAR *) &dest_sin, sizeof(dest_sin)) == SOCKET_ERROR) { - std::cerr << "sendMulticast: sendto failed: " << WSAGetLastError() << std::endl; + std::cerr << "winHttpHandler: sendMulticast: sendto failed: " << WSAGetLastError() << std::endl; closesocket(connect_socket); - throw(std::runtime_error("sendto failed")); + throw(std::runtime_error("winHttpHandler: sendMulticast: sendto failed")); } // shutdown the connection for sending since no more data will be sent @@ -225,8 +225,8 @@ std::vector winHttpHandler::sendMulticast(const std::string & msg, if (res == SOCKET_ERROR) { closesocket(connect_socket); - std::cerr << "sendMulticast: shutdown failed: " << WSAGetLastError() << std::endl; - throw(std::runtime_error("shutdown failed")); + std::cerr << "winHttpHandler: sendMulticast: shutdown failed: " << WSAGetLastError() << std::endl; + throw(std::runtime_error("winHttpHandler: sendMulticast: shutdown failed")); } std::string response; @@ -238,12 +238,12 @@ std::vector winHttpHandler::sendMulticast(const std::string & msg, res = recv(connect_socket, recvbuf, recvbuflen, 0); if (res > 0) { - std::cout << "sendMulticast: Bytes received: " << res << std::endl; + std::cout << "winHttpHandler: sendMulticast: Bytes received: " << res << std::endl; response.append(recvbuf, res); } else if (res == 0) { - std::cout << "sendMulticast: Connection closed " << std::endl; + std::cout << "winHttpHandler: sendMulticast: Connection closed " << std::endl; } else { -- libgit2 0.21.4