Browse Source

added clearlog()+ bug fix

master
Hatef 2 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
SOURCES += \
logger.cpp \
main.cpp \
logger.cpp \
socket.cpp
# Default rules for deployment.

24
logger.cpp

@ -17,36 +17,42 @@ QHash<QtMsgType, QString> 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()
{
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);

3
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);
};

7
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();
}

3
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;
}

Loading…
Cancel
Save