THRIFT: generic output handler
Summary: I'm tired of getting output from thrift via perror AND exceptions, so
this class allows the client to set an alternate (or empty) handler for error
output
Reviewed By: mcslee
Test Plan: I ran on the worker with the default, got output via perror, then
overloaded with my own function and got output via syslog and then NULL
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665131 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/transport/TServerSocket.cpp b/lib/cpp/src/transport/TServerSocket.cpp
index f341a2b..0d25bf0 100644
--- a/lib/cpp/src/transport/TServerSocket.cpp
+++ b/lib/cpp/src/transport/TServerSocket.cpp
@@ -64,7 +64,7 @@
void TServerSocket::listen() {
int sv[2];
if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
- perror("TServerSocket::init()");
+ GlobalOutput("TServerSocket::init()");
intSock1_ = -1;
intSock2_ = -1;
} else {
@@ -74,7 +74,7 @@
serverSocket_ = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket_ == -1) {
- perror("TServerSocket::listen() socket");
+ GlobalOutput("TServerSocket::listen() socket");
close();
throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket.");
}
@@ -83,7 +83,7 @@
int one = 1;
if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR,
&one, sizeof(one))) {
- perror("TServerSocket::listen() SO_REUSEADDR");
+ GlobalOutput("TServerSocket::listen() SO_REUSEADDR");
close();
throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR");
}
@@ -92,7 +92,7 @@
#ifdef TCP_DEFER_ACCEPT
if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT,
&one, sizeof(one))) {
- perror("TServerSocket::listen() TCP_DEFER_ACCEPT");
+ GlobalOutput("TServerSocket::listen() TCP_DEFER_ACCEPT");
close();
throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT");
}
@@ -103,7 +103,7 @@
if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER,
&ling, sizeof(ling))) {
close();
- perror("TServerSocket::listen() SO_LINGER");
+ GlobalOutput("TServerSocket::listen() SO_LINGER");
throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER");
}
@@ -111,7 +111,7 @@
if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY,
&one, sizeof(one))) {
close();
- perror("setsockopt TCP_NODELAY");
+ GlobalOutput("setsockopt TCP_NODELAY");
throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY");
}
@@ -147,14 +147,14 @@
if (retries > retryLimit_) {
char errbuf[1024];
sprintf(errbuf, "TServerSocket::listen() BIND %d", port_);
- perror(errbuf);
+ GlobalOutput(errbuf);
close();
throw TTransportException(TTransportException::NOT_OPEN, "Could not bind");
}
// Call listen
if (-1 == ::listen(serverSocket_, acceptBacklog_)) {
- perror("TServerSocket::listen() LISTEN");
+ GlobalOutput("TServerSocket::listen() LISTEN");
close();
throw TTransportException(TTransportException::NOT_OPEN, "Could not listen");
}
@@ -187,14 +187,14 @@
// a certain number
continue;
}
- perror("TServerSocket::acceptImpl() select -1");
+ GlobalOutput("TServerSocket::acceptImpl() select -1");
throw TTransportException(TTransportException::UNKNOWN);
} else if (ret > 0) {
// Check for an interrupt signal
if (intSock2_ >= 0 && FD_ISSET(intSock2_, &fds)) {
int8_t buf;
if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) {
- perror("TServerSocket::acceptImpl() interrupt receive");
+ GlobalOutput("TServerSocket::acceptImpl() interrupt receive");
}
throw TTransportException(TTransportException::INTERRUPTED);
}
@@ -203,7 +203,7 @@
break;
}
} else {
- perror("TServerSocket::acceptImpl() select 0");
+ GlobalOutput("TServerSocket::acceptImpl() select 0");
throw TTransportException(TTransportException::UNKNOWN);
}
}
@@ -215,18 +215,18 @@
(socklen_t *) &size);
if (clientSocket < 0) {
- perror("TServerSocket::accept()");
+ GlobalOutput("TServerSocket::accept()");
throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno);
}
// Make sure client socket is blocking
int flags = fcntl(clientSocket, F_GETFL, 0);
if (flags == -1) {
- perror("TServerSocket::select() fcntl GETFL");
+ GlobalOutput("TServerSocket::select() fcntl GETFL");
throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno);
}
if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) {
- perror("TServerSocket::select() fcntl SETFL");
+ GlobalOutput("TServerSocket::select() fcntl SETFL");
throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno);
}
@@ -245,7 +245,7 @@
if (intSock1_ >= 0) {
int8_t byte = 0;
if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) {
- perror("TServerSocket::interrupt()");
+ GlobalOutput("TServerSocket::interrupt()");
}
}
}