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/transport/TSocket.cpp b/lib/cpp/src/transport/TSocket.cpp
index 34ab1c8..46cd2b4 100644
--- a/lib/cpp/src/transport/TSocket.cpp
+++ b/lib/cpp/src/transport/TSocket.cpp
@@ -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);
}
}