Prepended qDebug output with connection id for easier debugging

This commit is contained in:
Srivats P. 2014-05-22 06:20:55 +05:30
parent 87982a4227
commit 8e14e0c15b
3 changed files with 28 additions and 1 deletions

View File

@ -29,9 +29,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <QHostAddress>
#include <QString>
#include <QTcpSocket>
#include <QThreadStorage>
#include <QtGlobal>
#include <qendian.h>
#include <stdio.h>
#include <stdlib.h>
static QThreadStorage<QString*> 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);
}

View File

@ -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);

View File

@ -34,6 +34,8 @@ protected:
RpcServer::RpcServer()
{
service = NULL;
qInstallMsgHandler(RpcConnection::connIdMsgHandler);
}
RpcServer::~RpcServer()