From: Aditya Agarwal Date: Wed, 5 Sep 2007 01:01:15 +0000 (+0000) Subject: -- adding hostinfo and time to GlobalOutput X-Git-Tag: 0.2.0~1223 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=4529c4b394d39900e1b50f6be08ba31553550c0d;p=common%2Fthrift.git -- 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 4a3df658..0b13bae2 100644 --- a/lib/cpp/src/Thrift.h +++ b/lib/cpp/src/Thrift.h @@ -28,7 +28,7 @@ namespace facebook { namespace thrift { class TOutput{ public: - TOutput() : f_(perror) {} + TOutput() : f_(&perrorTimeWrapper) {} inline void setOutputFunction(void (*function)(const char *)){ f_ = function; @@ -38,6 +38,15 @@ public: 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 90f07f84..540bb6e9 100644 --- a/lib/cpp/src/transport/TSocket.cpp +++ b/lib/cpp/src/transport/TSocket.cpp @@ -86,7 +86,7 @@ TSocket::~TSocket() { close(); } -bool TSocket::isOpen() { +bool TSocket::isOpen() { return (socket_ >= 0); } @@ -98,7 +98,8 @@ bool TSocket::peek() { 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 @@ void TSocket::openConnection(struct addrinfo *res) { 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 @@ void TSocket::openConnection(struct addrinfo *res) { 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 @@ void TSocket::openConnection(struct addrinfo *res) { 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 @@ uint32_t TSocket::read(uint8_t* buf, uint32_t len) { } // 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 @@ void TSocket::write(const uint8_t* buf, uint32_t len) { } 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 @@ void TSocket::setLinger(bool on, int linger) { 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 @@ void TSocket::setNoDelay(bool noDelay) { 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 @@ void TSocket::setRecvTimeout(int ms) { 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 @@ void TSocket::setSendTimeout(int ms) { (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 @@ void TSocket::setMaxRecvRetries(int maxRecvRetries) { maxRecvRetries_ = maxRecvRetries; } +string TSocket::getSocketInfo() { + std::ostringstream oss; + oss << ""; + return oss.str(); +} + }}} // facebook::thrift::transport diff --git a/lib/cpp/src/transport/TSocket.h b/lib/cpp/src/transport/TSocket.h index 9d8f082f..511e4a9a 100644 --- a/lib/cpp/src/transport/TSocket.h +++ b/lib/cpp/src/transport/TSocket.h @@ -137,6 +137,9 @@ class TSocket : public TTransport { */ 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 @@ class TSocket : public TTransport { /** Recv timeout timeval */ struct timeval recvTimeval_; + }; }}} // facebook::thrift::transport