You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dr...@apache.org on 2008/07/14 00:18:53 UTC

svn commit: r676448 - in /incubator/thrift/trunk/lib/cpp/src: Thrift.cpp Thrift.h server/TNonblockingServer.cpp server/TNonblockingServer.h transport/TFileTransport.cpp transport/TServerSocket.cpp transport/TSocket.cpp

Author: dreiss
Date: Sun Jul 13 15:18:51 2008
New Revision: 676448

URL: http://svn.apache.org/viewvc?rev=676448&view=rev
Log:
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.

Modified:
    incubator/thrift/trunk/lib/cpp/src/Thrift.cpp
    incubator/thrift/trunk/lib/cpp/src/Thrift.h
    incubator/thrift/trunk/lib/cpp/src/server/TNonblockingServer.cpp
    incubator/thrift/trunk/lib/cpp/src/server/TNonblockingServer.h
    incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp
    incubator/thrift/trunk/lib/cpp/src/transport/TServerSocket.cpp
    incubator/thrift/trunk/lib/cpp/src/transport/TSocket.cpp

Modified: incubator/thrift/trunk/lib/cpp/src/Thrift.cpp
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/Thrift.cpp?rev=676448&r1=676447&r2=676448&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/Thrift.cpp (original)
+++ incubator/thrift/trunk/lib/cpp/src/Thrift.cpp Sun Jul 13 15:18:51 2008
@@ -8,11 +8,50 @@
 #include <cstring>
 #include <boost/lexical_cast.hpp>
 #include <protocol/TProtocol.h>
+#include <stdarg.h>
+#include <stdio.h>
 
 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<string>(errno_copy);

Modified: incubator/thrift/trunk/lib/cpp/src/Thrift.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/Thrift.h?rev=676448&r1=676447&r2=676448&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/Thrift.h (original)
+++ incubator/thrift/trunk/lib/cpp/src/Thrift.h Sun Jul 13 15:18:51 2008
@@ -38,6 +38,17 @@
     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];

Modified: incubator/thrift/trunk/lib/cpp/src/server/TNonblockingServer.cpp
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/server/TNonblockingServer.cpp?rev=676448&r1=676447&r2=676448&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/server/TNonblockingServer.cpp (original)
+++ incubator/thrift/trunk/lib/cpp/src/server/TNonblockingServer.cpp Sun Jul 13 15:18:51 2008
@@ -50,12 +50,10 @@
     // 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 @@
       }
 
       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 @@
         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 @@
     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 @@
       // 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 @@
         // 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 @@
     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 @@
     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 @@
     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 @@
 
     // 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 @@
   // 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 @@
   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());
 

Modified: incubator/thrift/trunk/lib/cpp/src/server/TNonblockingServer.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/server/TNonblockingServer.h?rev=676448&r1=676447&r2=676448&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/server/TNonblockingServer.h (original)
+++ incubator/thrift/trunk/lib/cpp/src/server/TNonblockingServer.h Sun Jul 13 15:18:51 2008
@@ -334,8 +334,7 @@
   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();
   }

Modified: incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp?rev=676448&r1=676447&r2=676448&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp (original)
+++ incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp Sun Jul 13 15:18:51 2008
@@ -100,11 +100,10 @@
   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 @@
   // 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 @@
       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 @@
             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 @@
         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 @@
   // 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);
   }
 
 }

Modified: incubator/thrift/trunk/lib/cpp/src/transport/TServerSocket.cpp
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/transport/TServerSocket.cpp?rev=676448&r1=676447&r2=676448&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/transport/TServerSocket.cpp (original)
+++ incubator/thrift/trunk/lib/cpp/src/transport/TServerSocket.cpp Sun Jul 13 15:18:51 2008
@@ -80,9 +80,7 @@
 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 @@
   // 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 @@
   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 @@
   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 @@
     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 @@
     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 @@
   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 @@
   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 @@
   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 @@
   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 @@
   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 @@
   // 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 @@
         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 @@
 
   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 @@
   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 @@
   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);
     }
   }
 }

Modified: incubator/thrift/trunk/lib/cpp/src/transport/TSocket.cpp
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/transport/TSocket.cpp?rev=676448&r1=676447&r2=676448&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/transport/TSocket.cpp (original)
+++ incubator/thrift/trunk/lib/cpp/src/transport/TSocket.cpp Sun Jul 13 15:18:51 2008
@@ -96,8 +96,7 @@
   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 @@
   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 @@
   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 @@
 
   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 @@
     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 @@
   } 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 @@
     }
 
     // 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 @@
     }
 
     // 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 @@
     // 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 @@
   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 @@
   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 @@
   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 @@
                       (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);
   }
 }