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/TFileTransport.cpp b/lib/cpp/src/transport/TFileTransport.cpp
index d376795..121b4df 100644
--- a/lib/cpp/src/transport/TFileTransport.cpp
+++ b/lib/cpp/src/transport/TFileTransport.cpp
@@ -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);
}
}
diff --git a/lib/cpp/src/transport/TServerSocket.cpp b/lib/cpp/src/transport/TServerSocket.cpp
index 428dc1b..3493fc0 100644
--- a/lib/cpp/src/transport/TServerSocket.cpp
+++ b/lib/cpp/src/transport/TServerSocket.cpp
@@ -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);
}
}
}
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);
}
}