qBittorrent/src/app/filelogger.cpp

194 lines
5.5 KiB
C++
Raw Normal View History

2016-01-25 02:06:06 +03:00
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2016 sledgehammer999 <hammered999@gmail.com>
2016-01-25 02:06:06 +03:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
2018-05-31 12:19:07 +03:00
#include "filelogger.h"
#include <chrono>
2016-03-14 15:39:13 +03:00
#include <QDateTime>
2016-01-25 02:06:06 +03:00
#include <QDir>
#include <QTextStream>
#include <QVector>
2018-05-31 12:19:07 +03:00
#include "base/global.h"
2016-01-25 02:06:06 +03:00
#include "base/logger.h"
#include "base/utils/fs.h"
namespace
{
const std::chrono::seconds FLUSH_INTERVAL {2};
}
FileLogger::FileLogger(const Path &path, const bool backup
, const int maxSize, const bool deleteOld, const int age
, const FileLogAgeType ageType)
2016-03-14 15:39:13 +03:00
: m_backup(backup)
, m_maxSize(maxSize)
2016-01-25 02:06:06 +03:00
{
m_flusher.setInterval(FLUSH_INTERVAL);
2016-01-25 02:06:06 +03:00
m_flusher.setSingleShot(true);
2018-04-18 16:59:41 +03:00
connect(&m_flusher, &QTimer::timeout, this, &FileLogger::flushLog);
2016-01-25 02:06:06 +03:00
2016-03-14 15:39:13 +03:00
changePath(path);
if (deleteOld)
this->deleteOld(age, ageType);
2018-05-31 12:19:07 +03:00
const Logger *const logger = Logger::instance();
for (const Log::Msg &msg : asConst(logger->getMessages()))
2016-01-25 02:06:06 +03:00
addLogMessage(msg);
2018-04-18 16:59:41 +03:00
connect(logger, &Logger::newLogMessage, this, &FileLogger::addLogMessage);
2016-01-25 02:06:06 +03:00
}
FileLogger::~FileLogger()
{
closeLogFile();
}
void FileLogger::changePath(const Path &newPath)
2016-01-25 02:06:06 +03:00
{
// compare paths as strings to perform case sensitive comparison on all the platforms
if (newPath.data() == m_path.parentPath().data())
return;
2016-01-25 02:06:06 +03:00
closeLogFile();
2016-01-25 02:06:06 +03:00
m_path = newPath / Path(u"qbittorrent.log"_qs);
m_logFile.setFileName(m_path.data());
Utils::Fs::mkpath(newPath);
openLogFile();
2016-03-14 15:39:13 +03:00
}
2016-01-25 02:06:06 +03:00
2016-03-14 15:39:13 +03:00
void FileLogger::deleteOld(const int age, const FileLogAgeType ageType)
{
const QDateTime date = QDateTime::currentDateTime();
const QDir dir {m_path.parentPath().data()};
const QFileInfoList fileList = dir.entryInfoList(QStringList(u"qbittorrent.log.bak*"_qs)
2019-03-08 12:14:20 +03:00
, (QDir::Files | QDir::Writable), (QDir::Time | QDir::Reversed));
2016-01-25 02:06:06 +03:00
2020-11-16 10:02:11 +03:00
for (const QFileInfo &file : fileList)
{
2018-05-11 21:02:15 +03:00
QDateTime modificationDate = file.lastModified();
2020-11-16 10:02:11 +03:00
switch (ageType)
{
2018-05-11 21:02:15 +03:00
case DAYS:
modificationDate = modificationDate.addDays(age);
break;
case MONTHS:
modificationDate = modificationDate.addMonths(age);
break;
default:
modificationDate = modificationDate.addYears(age);
}
if (modificationDate > date)
2016-01-25 02:06:06 +03:00
break;
Utils::Fs::removeFile(Path(file.absoluteFilePath()));
2016-01-25 02:06:06 +03:00
}
}
2019-03-08 12:14:20 +03:00
void FileLogger::setBackup(const bool value)
2016-03-14 15:39:13 +03:00
{
m_backup = value;
}
void FileLogger::setMaxSize(const int value)
2016-03-14 15:39:13 +03:00
{
m_maxSize = value;
}
2016-01-25 02:06:06 +03:00
void FileLogger::addLogMessage(const Log::Msg &msg)
{
2019-03-10 20:00:47 +03:00
if (!m_logFile.isOpen()) return;
2016-01-25 02:06:06 +03:00
2020-03-26 10:49:14 +03:00
QTextStream stream(&m_logFile);
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
stream.setCodec("UTF-8");
#endif
2016-01-25 02:06:06 +03:00
2020-11-16 10:02:11 +03:00
switch (msg.type)
{
2016-01-25 02:06:06 +03:00
case Log::INFO:
stream << QStringView(u"(I) ");
2016-01-25 02:06:06 +03:00
break;
case Log::WARNING:
stream << QStringView(u"(W) ");
2016-01-25 02:06:06 +03:00
break;
case Log::CRITICAL:
stream << QStringView(u"(C) ");
2016-01-25 02:06:06 +03:00
break;
default:
stream << QStringView(u"(N) ");
2016-01-25 02:06:06 +03:00
}
stream << QDateTime::fromSecsSinceEpoch(msg.timestamp).toString(Qt::ISODate) << QStringView(u" - ") << msg.message << QChar(u'\n');
2016-01-25 02:06:06 +03:00
2020-11-16 10:02:11 +03:00
if (m_backup && (m_logFile.size() >= m_maxSize))
{
2016-01-25 02:06:06 +03:00
closeLogFile();
int counter = 0;
Path backupLogFilename = m_path + u".bak";
2016-01-25 02:06:06 +03:00
while (backupLogFilename.exists())
2020-11-16 10:02:11 +03:00
{
2016-01-25 02:06:06 +03:00
++counter;
backupLogFilename = m_path + u".bak" + QString::number(counter);
2016-01-25 02:06:06 +03:00
}
Utils::Fs::renameFile(m_path, backupLogFilename);
2016-01-25 02:06:06 +03:00
openLogFile();
}
2020-11-16 10:02:11 +03:00
else
{
if (!m_flusher.isActive())
m_flusher.start();
2016-01-25 02:06:06 +03:00
}
}
void FileLogger::flushLog()
{
2019-03-10 20:00:47 +03:00
if (m_logFile.isOpen())
m_logFile.flush();
2016-01-25 02:06:06 +03:00
}
void FileLogger::openLogFile()
{
2019-03-10 20:00:47 +03:00
if (!m_logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)
2020-11-16 10:02:11 +03:00
|| !m_logFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner))
{
2019-03-10 20:00:47 +03:00
m_logFile.close();
2019-03-08 12:14:20 +03:00
LogMsg(tr("An error occurred while trying to open the log file. Logging to file is disabled."), Log::CRITICAL);
2016-01-25 02:06:06 +03:00
}
}
void FileLogger::closeLogFile()
{
m_flusher.stop();
2019-03-10 20:00:47 +03:00
m_logFile.close();
2016-01-25 02:06:06 +03:00
}