-- adding hostinfo and time to GlobalOutput
Summary:
- makes thrift errors a lot more useful
Reviewed By: jwang, mcslee
Test Plan: tested with search
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665249 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/Thrift.h b/lib/cpp/src/Thrift.h
index 4a3df65..0b13bae 100644
--- a/lib/cpp/src/Thrift.h
+++ b/lib/cpp/src/Thrift.h
@@ -28,7 +28,7 @@
class TOutput{
public:
- TOutput() : f_(perror) {}
+ TOutput() : f_(&perrorTimeWrapper) {}
inline void setOutputFunction(void (*function)(const char *)){
f_ = function;
@@ -38,6 +38,15 @@
f_(message);
}
+ inline static void perrorTimeWrapper(const char* msg) {
+ time_t now;
+ char dbgtime[25];
+ time(&now);
+ ctime_r(&now, dbgtime);
+ dbgtime[24] = 0;
+ fprintf(stderr, "%s ", dbgtime);
+ perror(msg);
+ }
private:
void (*f_)(const char *);
};
diff --git a/lib/cpp/src/transport/TSocket.cpp b/lib/cpp/src/transport/TSocket.cpp
index 90f07f8..540bb6e 100644
--- a/lib/cpp/src/transport/TSocket.cpp
+++ b/lib/cpp/src/transport/TSocket.cpp
@@ -86,7 +86,7 @@
close();
}
-bool TSocket::isOpen() {
+bool TSocket::isOpen() {
return (socket_ >= 0);
}
@@ -98,7 +98,8 @@
int r = recv(socket_, &buf, 1, MSG_PEEK);
if (r == -1) {
int errno_copy = errno;
- GlobalOutput("TSocket::peek()");
+ string errStr = "TSocket::peek() " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy);
}
return (r > 0);
@@ -108,11 +109,12 @@
if (isOpen()) {
throw TTransportException(TTransportException::ALREADY_OPEN);
}
-
+
socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (socket_ == -1) {
int errno_copy = errno;
- GlobalOutput("TSocket::open() socket");
+ string errStr = "TSocket::open() socket " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy);
}
@@ -157,10 +159,8 @@
if (errno != EINPROGRESS) {
int errno_copy = errno;
- char buff[1024];
- sprintf(buff, "TSocket::open() connect %s %d", host_.c_str(), port_);
- GlobalOutput(buff);
-
+ string errStr = "TSocket::open() connect " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
throw TTransportException(TTransportException::NOT_OPEN, "connect()", errno_copy);
}
@@ -177,22 +177,26 @@
int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon);
if (ret2 == -1) {
int errno_copy = errno;
- GlobalOutput("TSocket::open() getsockopt SO_ERROR");
+ string errStr = "TSocket::open() getsockopt SO_ERROR " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
}
if (val == 0) {
goto done;
}
int errno_copy = errno;
- GlobalOutput("TSocket::open() SO_ERROR was set");
+ string errStr = "TSocket::open() SO_ERROR was set " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
} else if (ret == 0) {
int errno_copy = errno;
- GlobalOutput("TSocket::open() timed out");
+ string errStr = "TSocket::open() timed out " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
} else {
int errno_copy = errno;
- GlobalOutput("TSocket::open() select error");
+ string errStr = "TSocket::open() select error " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
throw TTransportException(TTransportException::NOT_OPEN, "open()", errno_copy);
}
@@ -315,7 +319,8 @@
}
// Now it's not a try again case, but a real probblez
- GlobalOutput("TSocket::read()");
+ string errStr = "TSocket::read() " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
// If we disconnect with no linger time
if (errno == ECONNRESET) {
@@ -376,7 +381,8 @@
}
int errno_copy = errno;
- GlobalOutput("TSocket::write() send < 0");
+ string errStr = "TSocket::write() send < 0 " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
throw TTransportException(TTransportException::UNKNOWN, "send", errno_copy);
}
@@ -406,7 +412,8 @@
struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
if (ret == -1) {
- GlobalOutput("TSocket::setLinger()");
+ string errStr = "TSocket::setLinger() " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
}
}
@@ -420,7 +427,8 @@
int v = noDelay_ ? 1 : 0;
int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
if (ret == -1) {
- GlobalOutput("TSocket::setNoDelay()");
+ string errStr = "TSocket::setNoDelay() " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
}
}
@@ -440,7 +448,8 @@
struct timeval r = recvTimeval_;
int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
if (ret == -1) {
- GlobalOutput("TSocket::setRecvTimeout()");
+ string errStr = "TSocket::setRecvTimeout() " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
}
}
@@ -454,7 +463,8 @@
(int)((sendTimeout_%1000)*1000)};
int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
if (ret == -1) {
- GlobalOutput("TSocket::setSendTimeout()");
+ string errStr = "TSocket::setSendTimeout() " + getSocketInfo();
+ GlobalOutput(errStr.c_str());
}
}
@@ -462,4 +472,10 @@
maxRecvRetries_ = maxRecvRetries;
}
+string TSocket::getSocketInfo() {
+ std::ostringstream oss;
+ oss << "<Host: " << host_ << " Port: " << port_ << ">";
+ return oss.str();
+}
+
}}} // facebook::thrift::transport
diff --git a/lib/cpp/src/transport/TSocket.h b/lib/cpp/src/transport/TSocket.h
index 9d8f082..511e4a9 100644
--- a/lib/cpp/src/transport/TSocket.h
+++ b/lib/cpp/src/transport/TSocket.h
@@ -137,6 +137,9 @@
*/
void setMaxRecvRetries(int maxRecvRetries);
+ /** get socket information */
+ std::string getSocketInfo();
+
protected:
/**
* Constructor to create socket from raw UNIX handle. Never called directly
@@ -179,6 +182,7 @@
/** Recv timeout timeval */
struct timeval recvTimeval_;
+
};
}}} // facebook::thrift::transport