From bfa93bc86f32727c98aeb1cd6b5ba8e133c5f681 Mon Sep 17 00:00:00 2001 From: Hatef Date: Wed, 29 Jun 2022 16:31:15 +0430 Subject: [PATCH] added clearlog()+ bug fix --- UDP.pro | 2 +- logger.cpp | 30 ++++++++++++++++++------------ logger.h | 3 ++- main.cpp | 7 ++++--- socket.cpp | 3 ++- 5 files changed, 27 insertions(+), 18 deletions(-) diff --git a/UDP.pro b/UDP.pro index 7d729a6..ef48da6 100644 --- a/UDP.pro +++ b/UDP.pro @@ -23,8 +23,8 @@ DEFINES += QT_DEPRECATED_WARNINGS #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ - logger.cpp \ main.cpp \ + logger.cpp \ socket.cpp # Default rules for deployment. diff --git a/logger.cpp b/logger.cpp index 6b539ce..40384b5 100644 --- a/logger.cpp +++ b/logger.cpp @@ -17,36 +17,42 @@ QHash Logger::_logTitles = { 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"; + qInfo() << "The Logger closed" << "\n"; delete _logFile; } -void Logger::makeFinishingLine() +void Logger::clearLog() { - _logFile->write( - "**************************************************************************************\n"); - _logFile->flush(); + if(_logFile) + { + _logFile->resize(0); + } +} + +void Logger::makeStartingLine() +{ + if(_logFile) + { + _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"). + QString log = QObject::tr("%1 | %2 | %3 | %4 | %5\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 + arg(QString(context.function).section('(', -2, -2). //Function name only section(' ', -1). section(':', -1)). arg(msg); diff --git a/logger.h b/logger.h index 30e7dee..754185f 100644 --- a/logger.h +++ b/logger.h @@ -15,7 +15,8 @@ public: Logger(QString fileName = "./DefaultHatefLogFile.log"); ~Logger(); - static void makeFinishingLine(); + static void makeStartingLine(); + static void clearLog(); static void messageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg); }; diff --git a/main.cpp b/main.cpp index 6bdc451..6859dae 100644 --- a/main.cpp +++ b/main.cpp @@ -6,7 +6,10 @@ int main(int argc, char* argv[]) { QCoreApplication a(argc, argv); - Logger mylog("thetest5.log"); + Logger mylog("thetest6.log"); + //mylog.clearLog(); + Logger::makeStartingLine(); + UdpSocket server; UdpSocket client; @@ -15,7 +18,5 @@ 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 e54be12..d5c28fa 100644 --- a/socket.cpp +++ b/socket.cpp @@ -7,11 +7,12 @@ UdpSocket::UdpSocket(QObject* parent, quint16 port) : _socket = new QUdpSocket(this); _socket->bind(QHostAddress::LocalHost, port); connect(_socket, &QUdpSocket::readyRead, this, &UdpSocket::readyRead); + qInfo() << "A Socket built" << "\n"; } UdpSocket::~UdpSocket() { - qDebug() << "Udp socket deleted!" << "\n"; + qInfo() << "Udp socket deleted!" << "\n"; delete _socket; }