From: David Reiss Date: Sun, 13 Jul 2008 22:18:51 +0000 (+0000) Subject: cpp: Eliminate the use of fprintf [THRIFT-77] X-Git-Tag: 0.2.0~473 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=01e55c1c28547eaac74636790bb86617c890c4c2;p=common%2Fthrift.git cpp: Eliminate the use of fprintf [THRIFT-77] Add printf and perror methods to TOutput and use them to replace uses of fprintf in the C++ library. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@676448 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cpp/src/Thrift.cpp b/lib/cpp/src/Thrift.cpp index da21d33d..c7840159 100644 --- a/lib/cpp/src/Thrift.cpp +++ b/lib/cpp/src/Thrift.cpp @@ -8,11 +8,50 @@ #include #include #include +#include +#include namespace facebook { namespace thrift { TOutput GlobalOutput; +void TOutput::printf(const char *message, ...) { + // Try to reduce heap usage, even if printf is called rarely. + static const int STACK_BUF_SIZE = 256; + char stack_buf[STACK_BUF_SIZE]; + va_list ap; + + va_start(ap, message); + int need = vsnprintf(stack_buf, STACK_BUF_SIZE, message, ap); + va_end(ap); + + if (need < STACK_BUF_SIZE) { + f_(stack_buf); + return; + } + + char *heap_buf = (char*)malloc((need+1) * sizeof(char)); + if (heap_buf == NULL) { + // Malloc failed. We might as well print the stack buffer. + f_(stack_buf); + return; + } + + va_start(ap, message); + int rval = vsnprintf(heap_buf, need+1, message, ap); + va_end(ap); + // TODO(shigin): inform user + if (rval != -1) { + f_(heap_buf); + } + free(heap_buf); +} + +void TOutput::perror(const char *message, int errno_copy) { + std::string out = message + strerror_s(errno_copy); + f_(out.c_str()); +} + std::string TOutput::strerror_s(int errno_copy) { #ifndef HAVE_STRERROR_R return "errno = " + boost::lexical_cast(errno_copy); diff --git a/lib/cpp/src/Thrift.h b/lib/cpp/src/Thrift.h index 4d2ff204..bb2b14a5 100644 --- a/lib/cpp/src/Thrift.h +++ b/lib/cpp/src/Thrift.h @@ -38,6 +38,17 @@ class TOutput { f_(message); } + // It is important to have a const char* overload here instead of + // just the string version, otherwise errno could be corrupted + // if there is some problem allocating memory when constructing + // the string. + void perror(const char *message, int errno_copy); + inline void perror(const std::string &message, int errno_copy) { + perror(message.c_str(), errno_copy); + } + + void printf(const char *message, ...); + inline static void errorTimeWrapper(const char* msg) { time_t now; char dbgtime[25]; diff --git a/lib/cpp/src/server/TNonblockingServer.cpp b/lib/cpp/src/server/TNonblockingServer.cpp index 974139b4..bf9a5b17 100644 --- a/lib/cpp/src/server/TNonblockingServer.cpp +++ b/lib/cpp/src/server/TNonblockingServer.cpp @@ -50,12 +50,10 @@ class TConnection::Task: public Runnable { // Signal completion back to the libevent thread via a socketpair int8_t b = 0; if (-1 == send(taskHandle_, &b, sizeof(int8_t), 0)) { - string errStr = "TNonblockingServer::Task: send " + TOutput::strerror_s(errno); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TNonblockingServer::Task: send ", errno); } if (-1 == ::close(taskHandle_)) { - string errStr = "TNonblockingServer::Task: close, possible resource leak " + TOutput::strerror_s(errno); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TNonblockingServer::Task: close, possible resource leak ", errno); } } @@ -141,8 +139,7 @@ void TConnection::workSocket() { } if (errno != ECONNRESET) { - string errStr = "TConnection::workSocket() recv -1 " + TOutput::strerror_s(errno); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TConnection::workSocket() recv -1 ", errno); } } @@ -178,8 +175,7 @@ void TConnection::workSocket() { return; } if (errno != EPIPE) { - string errStr = "TConnection::workSocket() send -1 " + TOutput::strerror_s(errno); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TConnection::workSocket() send -1 ", errno); } close(); return; @@ -198,7 +194,7 @@ void TConnection::workSocket() { return; default: - fprintf(stderr, "Shit Got Ill. Socket State %d\n", socketState_); + GlobalOutput.printf("Shit Got Ill. Socket State %d", socketState_); assert(0); } } @@ -229,8 +225,7 @@ void TConnection::transition() { // We are setting up a Task to do this work and we will wait on it int sv[2]; if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) { - string errStr = "TConnection::socketpair() failed " + TOutput::strerror_s(errno); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TConnection::socketpair() failed ", errno); // Now we will fall through to the APP_WAIT_TASK block with no response } else { // Create task and dispatch to the thread manager @@ -270,15 +265,15 @@ void TConnection::transition() { // Invoke the processor server_->getProcessor()->process(inputProtocol_, outputProtocol_); } catch (TTransportException &ttx) { - fprintf(stderr, "TTransportException: Server::process() %s\n", ttx.what()); + GlobalOutput.printf("TTransportException: Server::process() %s", ttx.what()); close(); return; } catch (TException &x) { - fprintf(stderr, "TException: Server::process() %s\n", x.what()); + GlobalOutput.printf("TException: Server::process() %s", x.what()); close(); return; } catch (...) { - fprintf(stderr, "Server::process() unknown exception\n"); + GlobalOutput.printf("Server::process() unknown exception"); close(); return; } @@ -356,7 +351,7 @@ void TConnection::transition() { sz = (int32_t)ntohl(sz); if (sz <= 0) { - fprintf(stderr, "TConnection:transition() Negative frame size %d, remote side not using TFramedTransport?\n", sz); + GlobalOutput.printf("TConnection:transition() Negative frame size %d, remote side not using TFramedTransport?", sz); close(); return; } @@ -374,7 +369,7 @@ void TConnection::transition() { return; default: - fprintf(stderr, "Totally Fucked. Application State %d\n", appState_); + GlobalOutput.printf("Totally Fucked. Application State %d", appState_); assert(0); } } @@ -508,8 +503,7 @@ void TNonblockingServer::handleEvent(int fd, short which) { int flags; if ((flags = fcntl(clientSocket, F_GETFL, 0)) < 0 || fcntl(clientSocket, F_SETFL, flags | O_NONBLOCK) < 0) { - string errStr = "thriftServerEventHandler: set O_NONBLOCK (fcntl) " + TOutput::strerror_s(errno); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("thriftServerEventHandler: set O_NONBLOCK (fcntl) ", errno); close(clientSocket); return; } @@ -520,7 +514,7 @@ void TNonblockingServer::handleEvent(int fd, short which) { // Fail fast if we could not create a TConnection object if (clientConnection == NULL) { - fprintf(stderr, "thriftServerEventHandler: failed TConnection factory\n"); + GlobalOutput.printf("thriftServerEventHandler: failed TConnection factory"); close(clientSocket); return; } @@ -532,8 +526,7 @@ void TNonblockingServer::handleEvent(int fd, short which) { // Done looping accept, now we have to make sure the error is due to // blocking. Any other error is a problem if (errno != EAGAIN && errno != EWOULDBLOCK) { - string errStr = "thriftServerEventHandler: accept() " + TOutput::strerror_s(errno); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("thriftServerEventHandler: accept() ", errno); } } @@ -646,8 +639,7 @@ void TNonblockingServer::registerEvents(event_base* base) { eventBase_ = base; // Print some libevent stats - fprintf(stderr, - "libevent %s method %s\n", + GlobalOutput.printf("libevent %s method %s", event_get_version(), event_get_method()); diff --git a/lib/cpp/src/server/TNonblockingServer.h b/lib/cpp/src/server/TNonblockingServer.h index 9deaebd0..a2d6533b 100644 --- a/lib/cpp/src/server/TNonblockingServer.h +++ b/lib/cpp/src/server/TNonblockingServer.h @@ -334,8 +334,7 @@ class TConnection { static void taskHandler(int fd, short /* which */, void* v) { assert(fd == ((TConnection*)v)->taskHandle_); if (-1 == ::close(((TConnection*)v)->taskHandle_)) { - std::string errStr = "TConnection::taskHandler close handle failed, resource leak " + TOutput::strerror_s(errno); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TConnection::taskHandler close handle failed, resource leak ", errno); } ((TConnection*)v)->transition(); } diff --git a/lib/cpp/src/transport/TFileTransport.cpp b/lib/cpp/src/transport/TFileTransport.cpp index d3767956..121b4df1 100644 --- a/lib/cpp/src/transport/TFileTransport.cpp +++ b/lib/cpp/src/transport/TFileTransport.cpp @@ -100,11 +100,10 @@ void TFileTransport::resetOutputFile(int fd, string filename, int64_t offset) { if (fd_ > 0) { // flush any events in the queue flush(); - fprintf(stderr, "error, current file (%s) not closed\n", filename_.c_str()); + GlobalOutput.printf("error, current file (%s) not closed", filename_.c_str()); if (-1 == ::close(fd_)) { int errno_copy = errno; - string errStr = "TFileTransport: resetOutputFile() ::close() " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TFileTransport: resetOutputFile() ::close() ", errno_copy); throw TTransportException(TTransportException::UNKNOWN, "TFileTransport: error in file close", errno_copy); } } @@ -162,9 +161,7 @@ TFileTransport::~TFileTransport() { // close logfile if (fd_ > 0) { if(-1 == ::close(fd_)) { - int errno_copy = errno; - string errStr = "TFileTransport: ~TFileTransport() ::close() " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TFileTransport: ~TFileTransport() ::close() ", errno); } } } @@ -321,8 +318,7 @@ void TFileTransport::writerThread() { if (enqueueBuffer_->isEmpty() && dequeueBuffer_->isEmpty()) { if (-1 == ::close(fd_)) { int errno_copy = errno; - string errStr = "TFileTransport: writerThread() ::close() " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TFileTransport: writerThread() ::close() ", errno_copy); throw TTransportException(TTransportException::UNKNOWN, "TFileTransport: error in file close", errno_copy); } // just be safe and sync to disk @@ -370,8 +366,7 @@ void TFileTransport::writerThread() { bzero(zeros, padding); if (-1 == ::write(fd_, zeros, padding)) { int errno_copy = errno; - string errStr = "TFileTransport: writerThread() error while padding zeros " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TFileTransport: writerThread() error while padding zeros ", errno_copy); throw TTransportException(TTransportException::UNKNOWN, "TFileTransport: error while padding zeros", errno_copy); } unflushed += padding; @@ -383,8 +378,7 @@ void TFileTransport::writerThread() { if (outEvent->eventSize_ > 0) { if (-1 == ::write(fd_, outEvent->eventBuff_, outEvent->eventSize_)) { int errno_copy = errno; - string errStr = "TFileTransport: error while writing event " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TFileTransport: error while writing event ", errno_copy); throw TTransportException(TTransportException::UNKNOWN, "TFileTransport: error while writing event", errno_copy); } @@ -767,9 +761,8 @@ void TFileTransport::openLogFile() { // make sure open call was successful if(fd_ == -1) { int errno_copy = errno; - string errStr = "TFileTransport: openLogFile() ::open() file: " + filename_ + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); - throw TTransportException(TTransportException::NOT_OPEN, errStr, errno_copy); + GlobalOutput.perror("TFileTransport: openLogFile() ::open() file: " + filename_, errno_copy); + throw TTransportException(TTransportException::NOT_OPEN, filename_, errno_copy); } } diff --git a/lib/cpp/src/transport/TServerSocket.cpp b/lib/cpp/src/transport/TServerSocket.cpp index 428dc1b8..3493fc09 100644 --- a/lib/cpp/src/transport/TServerSocket.cpp +++ b/lib/cpp/src/transport/TServerSocket.cpp @@ -80,9 +80,7 @@ void TServerSocket::setTcpRecvBuffer(int tcpRecvBuffer) { void TServerSocket::listen() { int sv[2]; if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) { - int errno_copy = errno; - string errStr = "TServerSocket::listen() socketpair() " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::listen() socketpair() ", errno); intSock1_ = -1; intSock2_ = -1; } else { @@ -102,7 +100,7 @@ void TServerSocket::listen() { // Wildcard address error = getaddrinfo(NULL, port, &hints, &res0); if (error) { - fprintf(stderr, "getaddrinfo %d: %s\n", error, gai_strerror(error)); + GlobalOutput.printf("getaddrinfo %d: %s", error, gai_strerror(error)); close(); throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for server socket."); } @@ -117,8 +115,7 @@ void TServerSocket::listen() { serverSocket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (serverSocket_ == -1) { int errno_copy = errno; - string errStr = "TServerSocket::listen() socket() " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::listen() socket() ", errno_copy); close(); throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket.", errno_copy); } @@ -128,8 +125,7 @@ void TServerSocket::listen() { if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) { int errno_copy = errno; - string errStr = "TServerSocket::listen() setsockopt() SO_REUSEADDR " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_REUSEADDR ", errno_copy); close(); throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR", errno_copy); } @@ -139,8 +135,7 @@ void TServerSocket::listen() { if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_SNDBUF, &tcpSendBuffer_, sizeof(tcpSendBuffer_))) { int errno_copy = errno; - string errStr = "TServerSocket::listen() setsockopt() SO_SNDBUF " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_SNDBUF ", errno_copy); close(); throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_SNDBUF", errno_copy); } @@ -150,8 +145,7 @@ void TServerSocket::listen() { if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_RCVBUF, &tcpRecvBuffer_, sizeof(tcpRecvBuffer_))) { int errno_copy = errno; - string errStr = "TServerSocket::listen() setsockopt() SO_RCVBUF " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_RCVBUF ", errno_copy); close(); throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_RCVBUF", errno_copy); } @@ -162,8 +156,7 @@ void TServerSocket::listen() { if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT, &one, sizeof(one))) { int errno_copy = errno; - string errStr = "TServerSocket::listen() setsockopt() TCP_DEFER_ACCEPT " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::listen() setsockopt() TCP_DEFER_ACCEPT ", errno_copy); close(); throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT", errno_copy); } @@ -173,7 +166,7 @@ void TServerSocket::listen() { int zero = 0; if (-1 == setsockopt(serverSocket_, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero))) { - GlobalOutput("TServerSocket::listen() IPV6_V6ONLY"); + GlobalOutput.perror("TServerSocket::listen() IPV6_V6ONLY ", errno); } #endif // #ifdef IPV6_V6ONLY @@ -182,8 +175,7 @@ void TServerSocket::listen() { if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling))) { int errno_copy = errno; - string errStr = "TServerSocket::listen() setsockopt() SO_LINGER " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_LINGER ", errno_copy); close(); throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER", errno_copy); } @@ -192,8 +184,7 @@ void TServerSocket::listen() { if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one))) { int errno_copy = errno; - string errStr = "TServerSocket::listen() setsockopt() TCP_NODELAY " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::listen() setsockopt() TCP_NODELAY ", errno_copy); close(); throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY", errno_copy); } @@ -202,15 +193,13 @@ void TServerSocket::listen() { int flags = fcntl(serverSocket_, F_GETFL, 0); if (flags == -1) { int errno_copy = errno; - string errStr = "TServerSocket::listen() fcntl() F_GETFL " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::listen() fcntl() F_GETFL ", errno_copy); throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); } if (-1 == fcntl(serverSocket_, F_SETFL, flags | O_NONBLOCK)) { int errno_copy = errno; - string errStr = "TServerSocket::listen() fcntl() O_NONBLOCK " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::listen() fcntl() O_NONBLOCK ", errno_copy); throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); } @@ -241,8 +230,7 @@ void TServerSocket::listen() { // Call listen if (-1 == ::listen(serverSocket_, acceptBacklog_)) { int errno_copy = errno; - string errStr = "TServerSocket::listen() listen() " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::listen() listen() ", errno_copy); close(); throw TTransportException(TTransportException::NOT_OPEN, "Could not listen", errno_copy); } @@ -278,17 +266,14 @@ shared_ptr TServerSocket::acceptImpl() { continue; } int errno_copy = errno; - string errStr = "TServerSocket::acceptImpl() poll() " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::acceptImpl() poll() ", errno_copy); throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy); } else if (ret > 0) { // Check for an interrupt signal if (intSock2_ >= 0 && (fds[1].revents & POLLIN)) { int8_t buf; if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) { - int errno_copy = errno; - string errStr = "TServerSocket::acceptImpl() recv() interrupt " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::acceptImpl() recv() interrupt ", errno); } throw TTransportException(TTransportException::INTERRUPTED); } @@ -311,8 +296,7 @@ shared_ptr TServerSocket::acceptImpl() { if (clientSocket < 0) { int errno_copy = errno; - string errStr = "TServerSocket::acceptImpl() ::accept() " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::acceptImpl() ::accept() ", errno_copy); throw TTransportException(TTransportException::UNKNOWN, "accept()", errno_copy); } @@ -320,15 +304,13 @@ shared_ptr TServerSocket::acceptImpl() { int flags = fcntl(clientSocket, F_GETFL, 0); if (flags == -1) { int errno_copy = errno; - string errStr = "TServerSocket::acceptImpl() fcntl() F_GETFL " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::acceptImpl() fcntl() F_GETFL ", errno_copy); throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_GETFL)", errno_copy); } if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) { int errno_copy = errno; - string errStr = "TServerSocket::acceptImpl() fcntl() F_SETFL ~O_NONBLOCK " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::acceptImpl() fcntl() F_SETFL ~O_NONBLOCK ", errno_copy); throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_SETFL)", errno_copy); } @@ -347,9 +329,7 @@ void TServerSocket::interrupt() { if (intSock1_ >= 0) { int8_t byte = 0; if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) { - int errno_copy = errno; - string errStr = "TServerSocket::interrupt() send() " + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TServerSocket::interrupt() send() ", errno); } } } diff --git a/lib/cpp/src/transport/TSocket.cpp b/lib/cpp/src/transport/TSocket.cpp index 34ab1c8d..46cd2b48 100644 --- a/lib/cpp/src/transport/TSocket.cpp +++ b/lib/cpp/src/transport/TSocket.cpp @@ -96,8 +96,7 @@ bool TSocket::peek() { int r = recv(socket_, &buf, 1, MSG_PEEK); if (r == -1) { int errno_copy = errno; - string errStr = "TSocket::peek() recv() " + getSocketInfo() + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TSocket::peek() recv() " + getSocketInfo(), errno_copy); throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy); } return (r > 0); @@ -111,8 +110,7 @@ void TSocket::openConnection(struct addrinfo *res) { socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (socket_ == -1) { int errno_copy = errno; - string errStr = "TSocket::open() socket() " + getSocketInfo() + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy); throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy); } @@ -137,15 +135,13 @@ void TSocket::openConnection(struct addrinfo *res) { if (connTimeout_ > 0) { if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) { int errno_copy = errno; - string errStr = "TSocket::open() fcntl() " + getSocketInfo() + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy); throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); } } else { if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) { int errno_copy = errno; - string errStr = "TSocket::open() fcntl " + getSocketInfo() + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy); throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); } } @@ -160,8 +156,7 @@ void TSocket::openConnection(struct addrinfo *res) { if (errno != EINPROGRESS) { int errno_copy = errno; - string errStr = "TSocket::open() connect() " + getSocketInfo() + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy); throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy); } @@ -180,16 +175,14 @@ void TSocket::openConnection(struct addrinfo *res) { int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon); if (ret2 == -1) { int errno_copy = errno; - string errStr = "TSocket::open() getsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy); throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy); } // no errors on socket, go to town if (val == 0) { goto done; } - string errStr = "TSocket::open() error on socket (after poll) " + getSocketInfo() + TOutput::strerror_s(val); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val); throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val); } else if (ret == 0) { // socket timed out @@ -199,8 +192,7 @@ void TSocket::openConnection(struct addrinfo *res) { } else { // error on poll() int errno_copy = errno; - string errStr = "TSocket::open() poll() " + getSocketInfo() + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy); throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy); } @@ -323,8 +315,8 @@ uint32_t TSocket::read(uint8_t* buf, uint32_t len) { } // Now it's not a try again case, but a real probblez - string errStr = "TSocket::read() recv() " + getSocketInfo() + TOutput::strerror_s(errno); - GlobalOutput(errStr.c_str()); + int errno_copy = errno; // Copy errno because we're allocating memory. + GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy); // If we disconnect with no linger time if (errno == ECONNRESET) { @@ -342,7 +334,8 @@ uint32_t TSocket::read(uint8_t* buf, uint32_t len) { } // Some other error, whatevz - throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno); + errno_copy = errno; + throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy); } // The remote host has closed the socket @@ -377,8 +370,7 @@ void TSocket::write(const uint8_t* buf, uint32_t len) { // Fail on a send error if (b < 0) { int errno_copy = errno; - string errStr = "TSocket::write() send() " + getSocketInfo() + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + GlobalOutput.perror("TSocket::write() send() " + getSocketInfo(), errno_copy); if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) { close(); @@ -422,9 +414,8 @@ void TSocket::setLinger(bool on, int linger) { struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); if (ret == -1) { - int errno_copy = errno; - string errStr = "TSocket::setLinger() setsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + int errno_copy = errno; // Copy errno because we're allocating memory. + GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy); } } @@ -438,9 +429,8 @@ void TSocket::setNoDelay(bool noDelay) { int v = noDelay_ ? 1 : 0; int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); if (ret == -1) { - int errno_copy = errno; - string errStr = "TSocket::setNoDelay() setsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + int errno_copy = errno; // Copy errno because we're allocating memory. + GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy); } } @@ -468,9 +458,8 @@ void TSocket::setRecvTimeout(int ms) { struct timeval r = recvTimeval_; int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r)); if (ret == -1) { - int errno_copy = errno; - string errStr = "TSocket::setRecvTimeout() setsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + int errno_copy = errno; // Copy errno because we're allocating memory. + GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy); } } @@ -491,9 +480,8 @@ void TSocket::setSendTimeout(int ms) { (int)((sendTimeout_%1000)*1000)}; int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s)); if (ret == -1) { - int errno_copy = errno; - string errStr = "TSocket::setSendTimeout() setsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); - GlobalOutput(errStr.c_str()); + int errno_copy = errno; // Copy errno because we're allocating memory. + GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy); } }