diff --git a/src/app/filelogger.cpp b/src/app/filelogger.cpp index 45d160b9f..54b8ed8e6 100644 --- a/src/app/filelogger.cpp +++ b/src/app/filelogger.cpp @@ -30,7 +30,6 @@ #include #include -#include #include #include "base/global.h" @@ -40,7 +39,6 @@ FileLogger::FileLogger(const QString &path, const bool backup, const int maxSize, const bool deleteOld, const int age, const FileLogAgeType ageType) : m_backup(backup) , m_maxSize(maxSize) - , m_logFile(nullptr) { m_flusher.setInterval(0); m_flusher.setSingleShot(true); @@ -59,9 +57,7 @@ FileLogger::FileLogger(const QString &path, const bool backup, const int maxSize FileLogger::~FileLogger() { - if (!m_logFile) return; closeLogFile(); - delete m_logFile; } void FileLogger::changePath(const QString &newPath) @@ -73,11 +69,8 @@ void FileLogger::changePath(const QString &newPath) if (tmpPath != m_path) { m_path = tmpPath; - if (m_logFile) { - closeLogFile(); - delete m_logFile; - } - m_logFile = new QFile(m_path); + closeLogFile(); + m_logFile.setFileName(m_path); openLogFile(); } } @@ -119,9 +112,9 @@ void FileLogger::setMaxSize(const int value) void FileLogger::addLogMessage(const Log::Msg &msg) { - if (!m_logFile) return; + if (!m_logFile.isOpen()) return; - QTextStream str(m_logFile); + QTextStream str(&m_logFile); switch (msg.type) { case Log::INFO: @@ -139,7 +132,7 @@ void FileLogger::addLogMessage(const Log::Msg &msg) str << QDateTime::fromMSecsSinceEpoch(msg.timestamp).toString(Qt::ISODate) << " - " << msg.message << endl; - if (m_backup && (m_logFile->size() >= m_maxSize)) { + if (m_backup && (m_logFile.size() >= m_maxSize)) { closeLogFile(); int counter = 0; QString backupLogFilename = m_path + ".bak"; @@ -159,16 +152,15 @@ void FileLogger::addLogMessage(const Log::Msg &msg) void FileLogger::flushLog() { - if (m_logFile) - m_logFile->flush(); + if (m_logFile.isOpen()) + m_logFile.flush(); } void FileLogger::openLogFile() { - if (!m_logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text) - || !m_logFile->setPermissions(QFile::ReadOwner | QFile::WriteOwner)) { - delete m_logFile; - m_logFile = nullptr; + if (!m_logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text) + || !m_logFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner)) { + m_logFile.close(); LogMsg(tr("An error occurred while trying to open the log file. Logging to file is disabled."), Log::CRITICAL); } } @@ -176,5 +168,5 @@ void FileLogger::openLogFile() void FileLogger::closeLogFile() { m_flusher.stop(); - m_logFile->close(); + m_logFile.close(); } diff --git a/src/app/filelogger.h b/src/app/filelogger.h index 5eefb3d90..78c8a2d5b 100644 --- a/src/app/filelogger.h +++ b/src/app/filelogger.h @@ -29,11 +29,10 @@ #ifndef FILELOGGER_H #define FILELOGGER_H +#include #include #include -class QFile; - namespace Log { struct Msg; @@ -71,7 +70,7 @@ private: QString m_path; bool m_backup; int m_maxSize; - QFile *m_logFile; + QFile m_logFile; QTimer m_flusher; };