|
|
|
#include "logger.h"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QObject>
|
|
|
|
|
|
|
|
QFile* Logger::_logFile = Q_NULLPTR;
|
|
|
|
|
|
|
|
QHash<QtMsgType, QString> Logger::_logTitles = {
|
|
|
|
{QtMsgType::QtDebugMsg, " Debug "},
|
|
|
|
{QtMsgType::QtInfoMsg, " Info "},
|
|
|
|
{QtMsgType::QtWarningMsg, "Warning "},
|
|
|
|
{QtMsgType::QtCriticalMsg, "Critical"},
|
|
|
|
{QtMsgType::QtFatalMsg, " Fatal "}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*******************************************************************************************/
|
|
|
|
Logger::Logger(const QString& fileName)
|
|
|
|
{
|
|
|
|
_logFile = new QFile;
|
|
|
|
_logFile->setFileName(fileName);
|
|
|
|
_logFile->open(QIODevice::Append | QIODevice::Text);
|
|
|
|
qInstallMessageHandler(Logger::messageOutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************************/
|
|
|
|
Logger::~Logger()
|
|
|
|
{
|
|
|
|
qInfo() << "The Logger closed" << "\n";
|
|
|
|
delete _logFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************************/
|
|
|
|
void Logger::clearLog()
|
|
|
|
{
|
|
|
|
if(_logFile)
|
|
|
|
{
|
|
|
|
_logFile->resize(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************************/
|
|
|
|
void Logger::makeStartingLine()
|
|
|
|
{
|
|
|
|
if(_logFile)
|
|
|
|
{
|
|
|
|
_logFile->write(
|
|
|
|
"**************************************************************************************\n");
|
|
|
|
_logFile->flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************************/
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
QString log = QObject::tr("%1 | %2 | %3 | %4\n").
|
|
|
|
arg(QDateTime::currentDateTime().toString("yyyy/MM/dd - hh:mm:ss")).
|
|
|
|
arg(Logger::_logTitles.value(type)).
|
|
|
|
arg(QString(context.function).section('(', -2, -2). //Function name only
|
|
|
|
section(' ', -1).
|
|
|
|
section(':', -1)).
|
|
|
|
arg(msg);
|
|
|
|
|
|
|
|
_logFile->write(log.toLocal8Bit());
|
|
|
|
_logFile->flush();
|
|
|
|
}
|