diff --git a/UDP.pro b/UDP.pro index ad5ec11..7d729a6 100644 --- a/UDP.pro +++ b/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 diff --git a/logger.cpp b/logger.cpp new file mode 100644 index 0000000..6b539ce --- /dev/null +++ b/logger.cpp @@ -0,0 +1,56 @@ +#include "logger.h" + +#include +#include +#include + +QFile* Logger::_logFile = Q_NULLPTR; + +QHash 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(); +} diff --git a/logger.h b/logger.h new file mode 100644 index 0000000..30e7dee --- /dev/null +++ b/logger.h @@ -0,0 +1,23 @@ +#ifndef LOGGER_H +#define LOGGER_H + +#include +#include +#include + +class Logger +{ +private: + static QFile* _logFile; + static QHash _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 diff --git a/main.cpp b/main.cpp index bd9c84b..6bdc451 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,12 @@ #include #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(); } diff --git a/socket.cpp b/socket.cpp index 2bbaf1b..e54be12 100644 --- a/socket.cpp +++ b/socket.cpp @@ -1,5 +1,4 @@ #include "socket.h" -#include 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); } diff --git a/socket.h b/socket.h index 51ea612..d56821f 100644 --- a/socket.h +++ b/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);