Browse Source

added clearlog()+ bug fix

master
Hatef 3 years ago
parent
commit
bfa93bc86f
  1. 2
      UDP.pro
  2. 24
      logger.cpp
  3. 3
      logger.h
  4. 7
      main.cpp
  5. 3
      socket.cpp

2
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 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \ SOURCES += \
logger.cpp \
main.cpp \ main.cpp \
logger.cpp \
socket.cpp socket.cpp
# Default rules for deployment. # Default rules for deployment.

24
logger.cpp

@ -17,36 +17,42 @@ QHash<QtMsgType, QString> Logger::_logTitles = {
Logger::Logger(QString fileName) Logger::Logger(QString fileName)
{ {
_logFile = new QFile; _logFile = new QFile;
//_logFile->setFileName(fileName);
_logFile->setFileName(fileName); _logFile->setFileName(fileName);
_logFile->open(QIODevice::Append | QIODevice::Text); _logFile->open(QIODevice::Append | QIODevice::Text);
qInstallMessageHandler(Logger::messageOutput); qInstallMessageHandler(Logger::messageOutput);
} }
Logger::~Logger() Logger::~Logger()
{ {
qDebug() << "Logger closed" << "\n"; qInfo() << "The Logger closed" << "\n";
delete _logFile; delete _logFile;
} }
void Logger::makeFinishingLine() void Logger::clearLog()
{
if(_logFile)
{
_logFile->resize(0);
}
}
void Logger::makeStartingLine()
{ {
if(_logFile)
{
_logFile->write( _logFile->write(
"**************************************************************************************\n"); "**************************************************************************************\n");
_logFile->flush(); _logFile->flush();
}
} }
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 | %6\n"). QString log = QObject::tr("%1 | %2 | %3 | %4 | %5\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(context.line).
arg(QString(context.file). arg(QString(context.function).section('(', -2, -2). //Function name only
section('\\', -1)). //File name without file path
arg(QString(context.function).
section('(', -2, -2). //Function name only
section(' ', -1). section(' ', -1).
section(':', -1)). section(':', -1)).
arg(msg); arg(msg);

3
logger.h

@ -15,7 +15,8 @@ public:
Logger(QString fileName = "./DefaultHatefLogFile.log"); Logger(QString fileName = "./DefaultHatefLogFile.log");
~Logger(); ~Logger();
static void makeFinishingLine(); static void makeStartingLine();
static void clearLog();
static void messageOutput(QtMsgType type, const QMessageLogContext& context, static void messageOutput(QtMsgType type, const QMessageLogContext& context,
const QString& msg); const QString& msg);
}; };

7
main.cpp

@ -6,7 +6,10 @@ int main(int argc, char* argv[])
{ {
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
Logger mylog("thetest5.log"); Logger mylog("thetest6.log");
//mylog.clearLog();
Logger::makeStartingLine();
UdpSocket server; UdpSocket server;
UdpSocket client; UdpSocket client;
@ -15,7 +18,5 @@ int main(int argc, char* argv[])
client.sayMsg("MsgC"); client.sayMsg("MsgC");
server.sayMsg("MsgS"); server.sayMsg("MsgS");
Logger::makeFinishingLine();
return a.exec(); return a.exec();
} }

3
socket.cpp

@ -7,11 +7,12 @@ UdpSocket::UdpSocket(QObject* parent, quint16 port) :
_socket = new QUdpSocket(this); _socket = new QUdpSocket(this);
_socket->bind(QHostAddress::LocalHost, port); _socket->bind(QHostAddress::LocalHost, port);
connect(_socket, &QUdpSocket::readyRead, this, &UdpSocket::readyRead); connect(_socket, &QUdpSocket::readyRead, this, &UdpSocket::readyRead);
qInfo() << "A Socket built" << "\n";
} }
UdpSocket::~UdpSocket() UdpSocket::~UdpSocket()
{ {
qDebug() << "Udp socket deleted!" << "\n"; qInfo() << "Udp socket deleted!" << "\n";
delete _socket; delete _socket;
} }

Loading…
Cancel
Save