diff --git a/rpc/rpcconn.cpp b/rpc/rpcconn.cpp index 6f47359..5d9ac4d 100644 --- a/rpc/rpcconn.cpp +++ b/rpc/rpcconn.cpp @@ -29,9 +29,17 @@ along with this program. If not, see #include #include +#include #include +#include +#include #include +#include +#include + +static QThreadStorage connId; + RpcConnection::RpcConnection(int socketDescriptor, ::google::protobuf::Service *service) : socketDescriptor(socketDescriptor), @@ -64,6 +72,7 @@ RpcConnection::~RpcConnection() void RpcConnection::start() { + QString id = QString("[%1:%2] "); clientSock = new QTcpSocket; if (!clientSock->setSocketDescriptor(socketDescriptor)) { qWarning("Unable to initialize TCP socket for incoming connection"); @@ -71,6 +80,9 @@ void RpcConnection::start() } qDebug("clientSock Thread = %p", clientSock->thread()); + connId.setLocalData(new QString(id.arg(clientSock->peerAddress().toString()) + .arg(clientSock->peerPort()))); + qDebug("accepting new connection from %s: %d", clientSock->peerAddress().toString().toAscii().constData(), clientSock->peerPort()); @@ -150,7 +162,7 @@ void RpcConnection::sendRpcReply(PbRpcController *controller) { qDebug("Server(%s): sending %d bytes to client <----", __FUNCTION__, len + PB_HDR_SIZE); - BUFDUMP(msg, len + 8); + BUFDUMP(msg, 8); qDebug("method = %d\nreq = \n%s---->", pendingMethodId, response->DebugString().c_str()); } @@ -289,3 +301,15 @@ _error_exit2: return; } +void RpcConnection::connIdMsgHandler(QtMsgType type, const char* msg) +{ + if (connId.hasLocalData()) { + QString newMsg(*connId.localData()); + newMsg.append(msg); + newMsg.replace(QChar('\n'), QString("\n").append(*connId.localData())); + msg = qPrintable(newMsg); + } + + fprintf(stderr, "%s\n", msg); + fflush(stderr); +} diff --git a/rpc/rpcconn.h b/rpc/rpcconn.h index ce5e5bd..fb23bb8 100644 --- a/rpc/rpcconn.h +++ b/rpc/rpcconn.h @@ -42,6 +42,7 @@ class RpcConnection : public QObject public: RpcConnection(int socketDescriptor, ::google::protobuf::Service *service); virtual ~RpcConnection(); + static void connIdMsgHandler(QtMsgType type, const char* msg); private: void sendRpcReply(PbRpcController *controller); diff --git a/rpc/rpcserver.cpp b/rpc/rpcserver.cpp index 6aa8f88..dad4b76 100644 --- a/rpc/rpcserver.cpp +++ b/rpc/rpcserver.cpp @@ -34,6 +34,8 @@ protected: RpcServer::RpcServer() { service = NULL; + + qInstallMsgHandler(RpcConnection::connIdMsgHandler); } RpcServer::~RpcServer()