Browse Source

Remove excessive usage of pointer

adaptive-webui-19844
Chocobo1 6 years ago
parent
commit
874bc84efc
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 28
      src/app/filelogger.cpp
  2. 5
      src/app/filelogger.h

28
src/app/filelogger.cpp

@ -30,7 +30,6 @@
#include <QDateTime> #include <QDateTime>
#include <QDir> #include <QDir>
#include <QFile>
#include <QTextStream> #include <QTextStream>
#include "base/global.h" #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) FileLogger::FileLogger(const QString &path, const bool backup, const int maxSize, const bool deleteOld, const int age, const FileLogAgeType ageType)
: m_backup(backup) : m_backup(backup)
, m_maxSize(maxSize) , m_maxSize(maxSize)
, m_logFile(nullptr)
{ {
m_flusher.setInterval(0); m_flusher.setInterval(0);
m_flusher.setSingleShot(true); m_flusher.setSingleShot(true);
@ -59,9 +57,7 @@ FileLogger::FileLogger(const QString &path, const bool backup, const int maxSize
FileLogger::~FileLogger() FileLogger::~FileLogger()
{ {
if (!m_logFile) return;
closeLogFile(); closeLogFile();
delete m_logFile;
} }
void FileLogger::changePath(const QString &newPath) void FileLogger::changePath(const QString &newPath)
@ -73,11 +69,8 @@ void FileLogger::changePath(const QString &newPath)
if (tmpPath != m_path) { if (tmpPath != m_path) {
m_path = tmpPath; m_path = tmpPath;
if (m_logFile) {
closeLogFile(); closeLogFile();
delete m_logFile; m_logFile.setFileName(m_path);
}
m_logFile = new QFile(m_path);
openLogFile(); openLogFile();
} }
} }
@ -119,9 +112,9 @@ void FileLogger::setMaxSize(const int value)
void FileLogger::addLogMessage(const Log::Msg &msg) 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) { switch (msg.type) {
case Log::INFO: 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; 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(); closeLogFile();
int counter = 0; int counter = 0;
QString backupLogFilename = m_path + ".bak"; QString backupLogFilename = m_path + ".bak";
@ -159,16 +152,15 @@ void FileLogger::addLogMessage(const Log::Msg &msg)
void FileLogger::flushLog() void FileLogger::flushLog()
{ {
if (m_logFile) if (m_logFile.isOpen())
m_logFile->flush(); m_logFile.flush();
} }
void FileLogger::openLogFile() void FileLogger::openLogFile()
{ {
if (!m_logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text) if (!m_logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)
|| !m_logFile->setPermissions(QFile::ReadOwner | QFile::WriteOwner)) { || !m_logFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner)) {
delete m_logFile; m_logFile.close();
m_logFile = nullptr;
LogMsg(tr("An error occurred while trying to open the log file. Logging to file is disabled."), Log::CRITICAL); 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() void FileLogger::closeLogFile()
{ {
m_flusher.stop(); m_flusher.stop();
m_logFile->close(); m_logFile.close();
} }

5
src/app/filelogger.h

@ -29,11 +29,10 @@
#ifndef FILELOGGER_H #ifndef FILELOGGER_H
#define FILELOGGER_H #define FILELOGGER_H
#include <QFile>
#include <QObject> #include <QObject>
#include <QTimer> #include <QTimer>
class QFile;
namespace Log namespace Log
{ {
struct Msg; struct Msg;
@ -71,7 +70,7 @@ private:
QString m_path; QString m_path;
bool m_backup; bool m_backup;
int m_maxSize; int m_maxSize;
QFile *m_logFile; QFile m_logFile;
QTimer m_flusher; QTimer m_flusher;
}; };

Loading…
Cancel
Save