From f24b51666a33afb3228cc077c2de58f36fe2ac53 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Fri, 13 Dec 2013 17:02:21 +0100 Subject: [PATCH] sstream.cpp: better error reporting when things go wrong (specifically, say which host is responsible) --- include/mitsuba/core/sstream.h | 5 ++++- src/libcore/sstream.cpp | 6 +++--- src/mitsuba/mtssrv.cpp | 10 +++++----- src/mtsgui/server.cpp | 10 +++++----- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/include/mitsuba/core/sstream.h b/include/mitsuba/core/sstream.h index a45fd94d..7d2687c2 100644 --- a/include/mitsuba/core/sstream.h +++ b/include/mitsuba/core/sstream.h @@ -78,7 +78,10 @@ public: std::string toString() const; /// Handle the last socket-specific error (looks up the appropriate OS description) - static bool handleError(const std::string &cmd, ELogLevel level = EError); + inline bool handleError(const std::string &cmd, ELogLevel level = EError) { return SocketStream::handleError(m_peer, cmd, level); } + + /// Handle the last socket-specific error (looks up the appropriate OS description) + static bool handleError(const std::string &peer, const std::string &cmd, ELogLevel level = EError); //! @} // ============================================================= diff --git a/src/libcore/sstream.cpp b/src/libcore/sstream.cpp index 9200ab3a..85c40ec7 100644 --- a/src/libcore/sstream.cpp +++ b/src/libcore/sstream.cpp @@ -246,15 +246,15 @@ void SocketStream::flush() { /* Ignore */ } -bool SocketStream::handleError(const std::string &cmd, ELogLevel level) { +bool SocketStream::handleError(const std::string &peer, const std::string &cmd, ELogLevel level) { #if !defined(__WINDOWS__) if (level == EWarn && errno == EINTR) /* This is not really a warning -- just retry the operation. */ return false; if (cmd.find("(") == std::string::npos) - Log(level, "Error in %s(): %s!", cmd.c_str(), strerror(errno)); + Log(level, "[%s] Error in %s(): %s!", peer.c_str(), cmd.c_str(), strerror(errno)); else - Log(level, "Error in %s: %s!", cmd.c_str(), strerror(errno)); + Log(level, "[%s] Error in %s: %s!", peer.c_str(), cmd.c_str(), strerror(errno)); #else std::string err; int error = WSAGetLastError(); diff --git a/src/mitsuba/mtssrv.cpp b/src/mitsuba/mtssrv.cpp index 6101eec9..de978e51 100644 --- a/src/mitsuba/mtssrv.cpp +++ b/src/mitsuba/mtssrv.cpp @@ -293,15 +293,15 @@ int mtssrv(int argc, char **argv) { /* Allocate a socket */ sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol); if (sock == -1) - SocketStream::handleError("socket"); + SocketStream::handleError("none", "socket"); /* Avoid "bind: socket already in use" */ if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(int)) < 0) - SocketStream::handleError("setsockopt"); + SocketStream::handleError("none", "setsockopt"); /* Bind the socket to the port number */ if (bind(sock, p->ai_addr, (socklen_t) p->ai_addrlen) == -1) { - SocketStream::handleError(formatString("bind(%s:%i)", hostName.c_str(), listenPort), EError); + SocketStream::handleError("none", formatString("bind(%s:%i)", hostName.c_str(), listenPort), EError); #if defined(__WINDOWS__) closesocket(sock); #else @@ -317,7 +317,7 @@ int mtssrv(int argc, char **argv) { freeaddrinfo(servinfo); if (listen(sock, CONN_BACKLOG) == -1) - SocketStream::handleError("bind"); + SocketStream::handleError("none", "bind"); SLog(EInfo, "Enter mtssrv -h for more options"); #if defined(__WINDOWS__) @@ -358,7 +358,7 @@ int mtssrv(int argc, char **argv) { if (errno == EINTR) continue; #endif - SocketStream::handleError("accept", EWarn); + SocketStream::handleError("none", "accept", EWarn); continue; } diff --git a/src/mtsgui/server.cpp b/src/mtsgui/server.cpp index 5f9c3c41..4b30441a 100644 --- a/src/mtsgui/server.cpp +++ b/src/mtsgui/server.cpp @@ -57,15 +57,15 @@ void ServerThread::run() { /* Allocate a socket */ m_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol); if (m_socket == -1) - SocketStream::handleError("socket"); + SocketStream::handleError("none", "socket"); /* Avoid "bind: socket already in use" */ if (setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(int)) < 0) - SocketStream::handleError("setsockopt"); + SocketStream::handleError("none", "setsockopt"); /* Bind the socket to the port number */ if (bind(m_socket, p->ai_addr, (socklen_t) p->ai_addrlen) == -1) { - SocketStream::handleError(formatString("bind(%s:%i)", m_nodeName.c_str(), m_listenPort), EError); + SocketStream::handleError("none", formatString("bind(%s:%i)", m_nodeName.c_str(), m_listenPort), EError); #if defined(__WINDOWS__) closesocket(m_socket); #else @@ -81,7 +81,7 @@ void ServerThread::run() { freeaddrinfo(servinfo); if (listen(m_socket, CONN_BACKLOG) == -1) - SocketStream::handleError("bind"); + SocketStream::handleError("none", "bind"); SLog(EInfo, "%s: Listening on port %i.. Close the window to stop.", m_nodeName.c_str(), m_listenPort); @@ -114,7 +114,7 @@ void ServerThread::run() { if (errno == EINTR) continue; #endif - SocketStream::handleError("accept", EWarn); + SocketStream::handleError("none", "accept", EWarn); continue; }