Browse Source

added logger

master
Hatef 3 years ago
parent
commit
c67f7c99a9
  1. 3
      UDP.pro
  2. 56
      logger.cpp
  3. 23
      logger.h
  4. 4
      main.cpp
  5. 25
      socket.cpp
  6. 2
      socket.h

3
UDP.pro

@ -23,7 +23,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
Tesp_Script.cpp \
logger.cpp \
main.cpp \
socket.cpp
@ -33,4 +33,5 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
logger.h \
socket.h

56
logger.cpp

@ -0,0 +1,56 @@
#include "logger.h"
#include <QDateTime>
#include <QDir>
#include <QObject>
QFile* Logger::_logFile = Q_NULLPTR;
QHash<QtMsgType, QString> Logger::_logTitles = {
{QtMsgType::QtDebugMsg, " Debug "},
{QtMsgType::QtInfoMsg, " Info "},
{QtMsgType::QtWarningMsg, "Warning "},
{QtMsgType::QtCriticalMsg, "Critical"},
{QtMsgType::QtFatalMsg, " Fatal "}
};
Logger::Logger(QString fileName)
{
_logFile = new QFile;
//_logFile->setFileName(fileName);
_logFile->setFileName(fileName);
_logFile->open(QIODevice::Append | QIODevice::Text);
qInstallMessageHandler(Logger::messageOutput);
}
Logger::~Logger()
{
qDebug() << "Logger closed" << "\n";
delete _logFile;
}
void Logger::makeFinishingLine()
{
_logFile->write(
"**************************************************************************************\n");
_logFile->flush();
}
void Logger::messageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
QString log = QObject::tr("%1 | %2 | %3 | %4 | %5 | %6\n").
arg(QDateTime::currentDateTime().toString("yyyy/MM/dd - hh:mm:ss")).
arg(Logger::_logTitles.value(type)).
arg(context.line).
arg(QString(context.file).
section('\\', -1)). //File name without file path
arg(QString(context.function).
section('(', -2, -2). //Function name only
section(' ', -1).
section(':', -1)).
arg(msg);
_logFile->write(log.toLocal8Bit());
_logFile->flush();
}

23
logger.h

@ -0,0 +1,23 @@
#ifndef LOGGER_H
#define LOGGER_H
#include <QDebug>
#include <QFile>
#include <QHash>
class Logger
{
private:
static QFile* _logFile;
static QHash<QtMsgType, QString> _logTitles;
public:
Logger(QString fileName = "./DefaultHatefLogFile.log");
~Logger();
static void makeFinishingLine();
static void messageOutput(QtMsgType type, const QMessageLogContext& context,
const QString& msg);
};
#endif //LOGGER_H

4
main.cpp

@ -1,10 +1,12 @@
#include <QCoreApplication>
#include "socket.h"
#include "logger.h"
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
Logger mylog("thetest5.log");
UdpSocket server;
UdpSocket client;
@ -13,5 +15,7 @@ int main(int argc, char* argv[])
client.sayMsg("MsgC");
server.sayMsg("MsgS");
Logger::makeFinishingLine();
return a.exec();
}

25
socket.cpp

@ -1,5 +1,4 @@
#include "socket.h"
#include <QDateTime>
UdpSocket::UdpSocket(QObject* parent, quint16 port) :
QObject(parent),
@ -12,12 +11,13 @@ UdpSocket::UdpSocket(QObject* parent, quint16 port) :
UdpSocket::~UdpSocket()
{
qDebug() << "Udp socket deleted!" << "\n";
delete _socket;
}
void UdpSocket::sayHello()
{
sayMsg("Connected :))");
sayMsg("Connected!");
}
void UdpSocket::sayMsg(QString Msg)
@ -27,24 +27,19 @@ void UdpSocket::sayMsg(QString Msg)
_socket->writeDatagram(Data, QHostAddress::LocalHost, _port);
}
QString UdpSocket::getTime()
void UdpSocket::logReceivedData(QHostAddress& addr, quint16& port, QByteArray& msg)
{
QDateTime current_time_object = QDateTime::currentDateTime();
QString formatted_time = current_time_object.toString("yyyy/MM/dd - hh:mm:ss");
return formatted_time;
qDebug() << "Address : " << addr.toString();
qDebug() << "Port : " << port;
qDebug() << "Content : " << msg << '\n';
}
void UdpSocket::readyRead()
{
QByteArray Buffer;
Buffer.resize(_socket->pendingDatagramSize());
QByteArray buffer;
buffer.resize(_socket->pendingDatagramSize());
QHostAddress sender_addr;
quint16 sender_port;
_socket->readDatagram(Buffer.data(), Buffer.size(), &sender_addr, &sender_port);
qDebug() << "New Message arrived at" << getTime();
qDebug() << "Address :" << sender_addr.toString();
qDebug() << "Port : " << sender_port;
qDebug() << "Content : " << Buffer << '\n';
_socket->readDatagram(buffer.data(), buffer.size(), &sender_addr, &sender_port);
logReceivedData(sender_addr, sender_port, buffer);
}

2
socket.h

@ -12,7 +12,7 @@ private:
QUdpSocket* _socket;
const quint16 _port;
QString getTime();
void logReceivedData(QHostAddress& addr, quint16& port, QByteArray& msg);
public:
explicit UdpSocket(QObject* parent = nullptr, quint16 port = 1234);

Loading…
Cancel
Save