Browse Source

logReceivedData moved to Logger class

master
Hatef 2 years ago
parent
commit
1a31290c5c
  1. 15
      logger.cpp
  2. 4
      logger.h
  3. 8
      main.cpp
  4. 11
      socket.cpp
  5. 2
      socket.h

15
logger.cpp

@ -50,13 +50,24 @@ void Logger::makeStartingLine()
} }
} }
/*******************************************************************************************/
void Logger::logReceivedData(const QByteArray& msg, const QHostAddress& addr, quint16 port)
{
qDebug() << "Address: " << addr.toString() << " - " << "Port: " << port << " - " <<
"Content: " << msg << '\n';
}
void Logger::logReceivedData(const QString& msg)
{
qDebug() << "Log : " << msg << '\n';
}
/*******************************************************************************************/ /*******************************************************************************************/
void Logger::messageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg) void Logger::messageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{ {
QString log = QObject::tr("%1 | %2 | %3 | %4 | %5\n"). QString log = QObject::tr("%1 | %2 | %3 | %4\n").
arg(QDateTime::currentDateTime().toString("yyyy/MM/dd - hh:mm:ss")). arg(QDateTime::currentDateTime().toString("yyyy/MM/dd - hh:mm:ss")).
arg(Logger::_logTitles.value(type)). arg(Logger::_logTitles.value(type)).
arg(context.line).
arg(QString(context.function).section('(', -2, -2). //Function name only arg(QString(context.function).section('(', -2, -2). //Function name only
section(' ', -1). section(' ', -1).
section(':', -1)). section(':', -1)).

4
logger.h

@ -4,6 +4,7 @@
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
#include <QHash> #include <QHash>
#include <QUdpSocket>
class Logger class Logger
{ {
@ -15,6 +16,9 @@ public:
Logger(const QString& fileName = "./DefaultHatefLogFile.log"); Logger(const QString& fileName = "./DefaultHatefLogFile.log");
~Logger(); ~Logger();
static void logReceivedData(const QByteArray& msg, const QHostAddress& addr, quint16 port);
static void logReceivedData(const QString& msg);
static void makeStartingLine(); static void makeStartingLine();
static void clearLog(); static void clearLog();
static void messageOutput(QtMsgType type, const QMessageLogContext& context, static void messageOutput(QtMsgType type, const QMessageLogContext& context,

8
main.cpp

@ -6,8 +6,8 @@ int main(int argc, char* argv[])
{ {
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
Logger mylog("thetest7.log"); Logger mylog("thetest8.log");
//mylog.clearLog(); mylog.clearLog();
Logger::makeStartingLine(); Logger::makeStartingLine();
UdpSocket server; UdpSocket server;
@ -15,8 +15,8 @@ int main(int argc, char* argv[])
client.sayHello(); client.sayHello();
server.sayHello(); server.sayHello();
client.sayMsg("MsgC"); client.sayMsg("Msg1C");
server.sayMsg("MsgS"); server.sayMsg("Msg1S");
return a.exec(); return a.exec();
} }

11
socket.cpp

@ -1,3 +1,4 @@
#include "logger.h"
#include "socket.h" #include "socket.h"
UdpSocket::UdpSocket(QObject* parent, quint16 port) : UdpSocket::UdpSocket(QObject* parent, quint16 port) :
@ -31,14 +32,6 @@ void UdpSocket::sayMsg(const QString& msg)
_socket->writeDatagram(Data, QHostAddress::LocalHost, _port); _socket->writeDatagram(Data, QHostAddress::LocalHost, _port);
} }
/*******************************************************************************************/
void UdpSocket::logReceivedData(const QHostAddress& addr, quint16 port, const QByteArray& msg) const
{
qDebug() << "Address : " << addr.toString();
qDebug() << "Port : " << port;
qDebug() << "Content : " << msg << '\n';
}
/*******************************************************************************************/ /*******************************************************************************************/
void UdpSocket::readyRead() void UdpSocket::readyRead()
{ {
@ -47,5 +40,5 @@ void UdpSocket::readyRead()
QHostAddress sender_addr; QHostAddress sender_addr;
quint16 sender_port; quint16 sender_port;
_socket->readDatagram(buffer.data(), buffer.size(), &sender_addr, &sender_port); _socket->readDatagram(buffer.data(), buffer.size(), &sender_addr, &sender_port);
logReceivedData(sender_addr, sender_port, buffer); Logger::logReceivedData(buffer, sender_addr, sender_port);
} }

2
socket.h

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

Loading…
Cancel
Save