Commit f0af01a2 authored by Ghislain MARY's avatar Ghislain MARY

Add time to log traces + improve display of log messages.

parent 6b964723
#include "connection.h"
#include <qendian.h>
Connection::Connection(QObject *parent) :
QTcpSocket(parent), mState(ReadingHeader)
......@@ -41,9 +42,14 @@ void Connection::processData()
return;
}
mLogLevel = static_cast<LogLevel>(mBuffer.at(0));
mMessageLength = ((static_cast<unsigned int>(mBuffer[1]) & 0xFF) << 8)
| (static_cast<unsigned int>(mBuffer[2]) & 0xFF);
memcpy(&mLogTime, mBuffer.constData(), 8);
#ifdef Q_LITTLE_ENDIAN
//Convert to LittleEndian
mLogTime = qbswap(mLogTime);
#endif
mLogLevel = static_cast<LogLevel>(static_cast<unsigned int>(mBuffer[8]));
mMessageLength = ((static_cast<unsigned int>(mBuffer[9]) & 0xFF) << 8)
| (static_cast<unsigned int>(mBuffer[10]) & 0xFF);
mBuffer.clear();
mState = ReadingMessage;
}
......@@ -51,7 +57,7 @@ void Connection::processData()
if (mState == ReadingMessage) {
if (hasEnoughData()) {
mBuffer = read(mMessageLength);
emit newMessage(mLogLevel, QString::fromUtf8(mBuffer));
emit newMessage(mLogTime, mLogLevel, QString::fromUtf8(mBuffer));
mBuffer.clear();
mMessageLength = 0;
mState = ReadingHeader;
......
......@@ -3,9 +3,10 @@
#include <QTcpSocket>
static const int HeaderLogTimeSize = 8;
static const int HeaderLogLevelSize = 1;
static const int HeaderMessageLengthSize = 2;
static const int HeaderSize = HeaderLogLevelSize + HeaderMessageLengthSize;
static const int HeaderSize = HeaderLogTimeSize + HeaderLogLevelSize + HeaderMessageLengthSize;
class Connection : public QTcpSocket
{
......@@ -25,7 +26,7 @@ public:
explicit Connection(QObject *parent = 0);
signals:
void newMessage(Connection::LogLevel level, const QString &message);
void newMessage(quint64 time, Connection::LogLevel level, const QString &message);
private slots:
void processReadyRead();
......@@ -37,6 +38,7 @@ private:
QByteArray mBuffer;
ParseState mState;
quint64 mLogTime;
LogLevel mLogLevel;
unsigned int mMessageLength;
};
......
......@@ -3,6 +3,7 @@
#include "ui_logger.h"
#include <QScrollBar>
#include <QTextTable>
Logger::Logger(QWidget *parent) :
QMainWindow(parent),
......@@ -20,13 +21,14 @@ Logger::Logger(QWidget *parent) :
mUi->setupUi(this);
mUi->textEdit->setReadOnly(true);
mTableFormat.setBorder(0);
connect(&mServer, SIGNAL(newConnection(Connection*)),
this, SLOT(notifyConnection(Connection*)));
connect(&mServer, SIGNAL(disconnected()),
this, SLOT(notifyDisconnection()));
connect(&mServer, SIGNAL(newMessage(Connection::LogLevel, QString)),
this, SLOT(appendMessage(Connection::LogLevel, QString)));
connect(&mServer, SIGNAL(newMessage(quint64, Connection::LogLevel, QString)),
this, SLOT(appendMessage(quint64, Connection::LogLevel, QString)));
}
Logger::~Logger()
......@@ -44,23 +46,31 @@ void Logger::notifyDisconnection()
mUi->statusBar->showMessage(QString("Disconnected"), 5000);
}
void Logger::appendMessage(Connection::LogLevel level, const QString &message)
void Logger::appendMessage(quint64 time, Connection::LogLevel level, const QString &message)
{
if (message.isEmpty())
return;
QScrollBar *bar = mUi->textEdit->verticalScrollBar();
int barpos = bar->value();
int barmax = bar->maximum();
QTextCursor cursor(mUi->textEdit->textCursor());
cursor.movePosition(QTextCursor::End);
QColor color = mUi->textEdit->textColor();
QTextTable *table = cursor.insertTable(1, 2, mTableFormat);
QTextCharFormat format;
format.setForeground(mUi->textEdit->textColor());
if (level == Connection::Debug) {
mUi->textEdit->setTextColor(Qt::gray);
format.setForeground(Qt::gray);
} else if (level == Connection::Warning) {
mUi->textEdit->setTextColor(Qt::darkYellow);
format.setForeground(Qt::darkYellow);
} else if (level == Connection::Error) {
mUi->textEdit->setTextColor(Qt::red);
format.setForeground(Qt::red);
}
table->cellAt(0, 0).firstCursorPosition().insertText('[' + QString("%1.%2").arg(time / 1000).arg(time % 1000) + "] ");
table->cellAt(0, 1).firstCursorPosition().insertText(message, format);
if (barpos == barmax) {
bar->setValue(bar->maximum());
} else {
bar->setValue(barpos);
}
mUi->textEdit->append(message);
mUi->textEdit->setTextColor(color);
QScrollBar *bar = mUi->textEdit->verticalScrollBar();
bar->setValue(bar->maximum());
}
......@@ -2,6 +2,7 @@
#define LOGGER_H
#include <QMainWindow>
#include <QTextTableFormat>
#include "server.h"
......@@ -20,12 +21,13 @@ public:
public slots:
void notifyConnection(Connection *connection);
void notifyDisconnection();
void appendMessage(Connection::LogLevel level, const QString &message);
void appendMessage(quint64 time, Connection::LogLevel level, const QString &message);
private:
Ui::Logger *mUi;
Server mServer;
QTextTableFormat mTableFormat;
};
#endif // LOGGER_H
......@@ -12,7 +12,7 @@ void Server::incomingConnection(qintptr socketDescriptor)
Connection *connection = new Connection(this);
connection->setSocketDescriptor(socketDescriptor);
connect(connection, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
connect(connection, SIGNAL(newMessage(Connection::LogLevel, QString)),
this, SIGNAL(newMessage(Connection::LogLevel, QString)));
connect(connection, SIGNAL(newMessage(quint64, Connection::LogLevel, QString)),
this, SIGNAL(newMessage(quint64, Connection::LogLevel, QString)));
emit newConnection(connection);
}
......@@ -14,7 +14,7 @@ public:
signals:
void newConnection(Connection *connection);
void disconnected();
void newMessage(Connection::LogLevel level, const QString &message);
void newMessage(quint64, Connection::LogLevel level, const QString &message);
protected:
void incomingConnection(qintptr socketDescriptor);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment