Error messages: Add logging to Socket class.

Refs #6070
This commit is contained in:
Michael Friedrich 2014-06-05 15:45:37 +02:00
parent 17668af2c2
commit a416987031
2 changed files with 72 additions and 4 deletions

View File

@ -21,7 +21,9 @@
#include "base/objectlock.hpp" #include "base/objectlock.hpp"
#include "base/utility.hpp" #include "base/utility.hpp"
#include "base/exception.hpp" #include "base/exception.hpp"
#include "base/logger_fwd.hpp"
#include <sstream> #include <sstream>
#include <iostream>
#include <boost/exception/errinfo_api_function.hpp> #include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_errno.hpp> #include <boost/exception/errinfo_errno.hpp>
@ -129,10 +131,18 @@ String Socket::GetAddressFromSockaddr(sockaddr *address, socklen_t len)
if (getnameinfo(address, len, host, sizeof(host), service, if (getnameinfo(address, len, host, sizeof(host), service,
sizeof(service), NI_NUMERICHOST | NI_NUMERICSERV) < 0) { sizeof(service), NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
#ifndef _WIN32 #ifndef _WIN32
std::ostringstream msgbuf;
msgbuf << "getnameinfo() failed with return code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("getnameinfo") << boost::errinfo_api_function("getnameinfo")
<< boost::errinfo_errno(errno)); << boost::errinfo_errno(errno));
#else /* _WIN32 */ #else /* _WIN32 */
std::ostringstream msgbuf;
msgbuf << "getnameinfo() failed with return code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("getnameinfo") << boost::errinfo_api_function("getnameinfo")
<< errinfo_win32_error(WSAGetLastError())); << errinfo_win32_error(WSAGetLastError()));
@ -158,10 +168,18 @@ String Socket::GetClientAddress(void)
if (getsockname(GetFD(), (sockaddr *)&sin, &len) < 0) { if (getsockname(GetFD(), (sockaddr *)&sin, &len) < 0) {
#ifndef _WIN32 #ifndef _WIN32
std::ostringstream msgbuf;
msgbuf << "getsockname() failed with return code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("getsockname") << boost::errinfo_api_function("getsockname")
<< boost::errinfo_errno(errno)); << boost::errinfo_errno(errno));
#else /* _WIN32 */ #else /* _WIN32 */
std::ostringstream msgbuf;
msgbuf << "getsockname() failed with return code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("getsockname") << boost::errinfo_api_function("getsockname")
<< errinfo_win32_error(WSAGetLastError())); << errinfo_win32_error(WSAGetLastError()));
@ -185,10 +203,18 @@ String Socket::GetPeerAddress(void)
if (getpeername(GetFD(), (sockaddr *)&sin, &len) < 0) { if (getpeername(GetFD(), (sockaddr *)&sin, &len) < 0) {
#ifndef _WIN32 #ifndef _WIN32
std::ostringstream msgbuf;
msgbuf << "getpeername() failed with return code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("getpeername") << boost::errinfo_api_function("getpeername")
<< boost::errinfo_errno(errno)); << boost::errinfo_errno(errno));
#else /* _WIN32 */ #else /* _WIN32 */
std::ostringstream msgbuf;
msgbuf << "getpeername() failed with return code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("getpeername") << boost::errinfo_api_function("getpeername")
<< errinfo_win32_error(WSAGetLastError())); << errinfo_win32_error(WSAGetLastError()));
@ -205,10 +231,18 @@ void Socket::Listen(void)
{ {
if (listen(GetFD(), SOMAXCONN) < 0) { if (listen(GetFD(), SOMAXCONN) < 0) {
#ifndef _WIN32 #ifndef _WIN32
std::ostringstream msgbuf;
msgbuf << "listen() failed with return code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("listen") << boost::errinfo_api_function("listen")
<< boost::errinfo_errno(errno)); << boost::errinfo_errno(errno));
#else /* _WIN32 */ #else /* _WIN32 */
std::ostringstream msgbuf;
msgbuf << "listen() failed with return code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("listen") << boost::errinfo_api_function("listen")
<< errinfo_win32_error(WSAGetLastError())); << errinfo_win32_error(WSAGetLastError()));
@ -225,10 +259,18 @@ size_t Socket::Write(const void *buffer, size_t count)
if (rc < 0) { if (rc < 0) {
#ifndef _WIN32 #ifndef _WIN32
std::ostringstream msgbuf;
msgbuf << "send() failed with return code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("send") << boost::errinfo_api_function("send")
<< boost::errinfo_errno(errno)); << boost::errinfo_errno(errno));
#else /* _WIN32 */ #else /* _WIN32 */
std::ostringstream msgbuf;
msgbuf << "send() failed with return code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("send") << boost::errinfo_api_function("send")
<< errinfo_win32_error(WSAGetLastError())); << errinfo_win32_error(WSAGetLastError()));
@ -247,10 +289,18 @@ size_t Socket::Read(void *buffer, size_t count)
if (rc < 0) { if (rc < 0) {
#ifndef _WIN32 #ifndef _WIN32
std::ostringstream msgbuf;
msgbuf << "recv() failed with return code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("recv") << boost::errinfo_api_function("recv")
<< boost::errinfo_errno(errno)); << boost::errinfo_errno(errno));
#else /* _WIN32 */ #else /* _WIN32 */
std::ostringstream msgbuf;
msgbuf << "recv() failed with return code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("recv") << boost::errinfo_api_function("recv")
<< errinfo_win32_error(WSAGetLastError())); << errinfo_win32_error(WSAGetLastError()));
@ -273,10 +323,18 @@ Socket::Ptr Socket::Accept(void)
if (fd < 0) { if (fd < 0) {
#ifndef _WIN32 #ifndef _WIN32
std::ostringstream msgbuf;
msgbuf << "accept() failed with return code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("accept") << boost::errinfo_api_function("accept")
<< boost::errinfo_errno(errno)); << boost::errinfo_errno(errno));
#else /* _WIN32 */ #else /* _WIN32 */
std::ostringstream msgbuf;
msgbuf << "accept() failed with return code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("accept") << boost::errinfo_api_function("accept")
<< errinfo_win32_error(WSAGetLastError())); << errinfo_win32_error(WSAGetLastError()));
@ -302,20 +360,30 @@ void Socket::Poll(bool read, bool write)
FD_ZERO(&exceptfds); FD_ZERO(&exceptfds);
FD_SET(GetFD(), &exceptfds); FD_SET(GetFD(), &exceptfds);
if (select(GetFD() + 1, &readfds, &writefds, &exceptfds, NULL) < 0) if (select(GetFD() + 1, &readfds, &writefds, &exceptfds, NULL) < 0) {
std::ostringstream msgbuf;
msgbuf << "select() failed with return code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("select") << boost::errinfo_api_function("select")
<< errinfo_win32_error(WSAGetLastError())); << errinfo_win32_error(WSAGetLastError()));
}
#else /* _WIN32 */ #else /* _WIN32 */
pollfd pfd; pollfd pfd;
pfd.fd = GetFD(); pfd.fd = GetFD();
pfd.events = (read ? POLLIN : 0) | (write ? POLLOUT : 0); pfd.events = (read ? POLLIN : 0) | (write ? POLLOUT : 0);
pfd.revents = 0; pfd.revents = 0;
if (poll(&pfd, 1, -1) < 0) if (poll(&pfd, 1, -1) < 0) {
std::ostringstream msgbuf;
msgbuf << "poll() failed with return code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
Log(LogCritical, "Socket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("poll") << boost::errinfo_api_function("poll")
<< boost::errinfo_errno(errno)); << boost::errinfo_errno(errno));
}
#endif /* _WIN32 */ #endif /* _WIN32 */
} }

View File

@ -63,7 +63,7 @@ void TcpSocket::Bind(const String& node, const String& service, int family)
if (rc != 0) { if (rc != 0) {
std::ostringstream msgbuf; std::ostringstream msgbuf;
msgbuf << "getaddrinfo() failed with return code " << rc << "('" << Utility::FormatErrorNumber(rc) << "')"; msgbuf << "getaddrinfo() failed with return code " << rc << ", \"" << Utility::FormatErrorNumber(rc) << "\"";
Log(LogCritical, "TcpSocket", msgbuf.str()); Log(LogCritical, "TcpSocket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()
@ -156,7 +156,7 @@ void TcpSocket::Connect(const String& node, const String& service)
if (rc != 0) { if (rc != 0) {
std::ostringstream msgbuf; std::ostringstream msgbuf;
msgbuf << "getaddrinfo() failed with return code " << rc << "('" << Utility::FormatErrorNumber(rc) << "')"; msgbuf << "getaddrinfo() failed with return code " << rc << ", \"" << Utility::FormatErrorNumber(rc) << "\"";
Log(LogCritical, "TcpSocket", msgbuf.str()); Log(LogCritical, "TcpSocket", msgbuf.str());
BOOST_THROW_EXCEPTION(socket_error() BOOST_THROW_EXCEPTION(socket_error()