qBittorrent/src/app/filelogger.cpp

182 lines
5.2 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>
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};
}
2016-03-14 15:39:13 +03:00
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)
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();
}
2018-05-31 12:19:07 +03:00
void FileLogger::changePath(const QString &newPath)
2016-01-25 02:06:06 +03:00
{
2019-03-08 12:14:20 +03:00
const QDir dir(newPath);
dir.mkpath(newPath);
const QString tmpPath = dir.absoluteFilePath("qbittorrent.log");
2016-01-25 02:06:06 +03:00
if (tmpPath != m_path) {
m_path = tmpPath;
2019-03-10 20:00:47 +03:00
closeLogFile();
m_logFile.setFileName(m_path);
2016-01-25 02:06:06 +03:00
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(Utils::Fs::branchPath(m_path));
2019-03-08 12:14:20 +03:00
const QFileInfoList fileList = dir.entryInfoList(QStringList("qbittorrent.log.bak*")
, (QDir::Files | QDir::Writable), (QDir::Time | QDir::Reversed));
2016-01-25 02:06:06 +03:00
2019-03-08 12:14:20 +03:00
for (const QFileInfo &file : fileList) {
2018-05-11 21:02:15 +03:00
QDateTime modificationDate = file.lastModified();
switch (ageType) {
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;
2016-03-14 15:39:13 +03:00
Utils::Fs::forceRemove(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);
stream.setCodec("UTF-8");
2016-01-25 02:06:06 +03:00
switch (msg.type) {
case Log::INFO:
2020-03-26 10:49:14 +03:00
stream << "(I) ";
2016-01-25 02:06:06 +03:00
break;
case Log::WARNING:
2020-03-26 10:49:14 +03:00
stream << "(W) ";
2016-01-25 02:06:06 +03:00
break;
case Log::CRITICAL:
2020-03-26 10:49:14 +03:00
stream << "(C) ";
2016-01-25 02:06:06 +03:00
break;
default:
2020-03-26 10:49:14 +03:00
stream << "(N) ";
2016-01-25 02:06:06 +03:00
}
stream << QDateTime::fromMSecsSinceEpoch(msg.timestamp).toString(Qt::ISODate) << " - " << msg.message << '\n';
2016-01-25 02:06:06 +03:00
2019-03-10 20:00:47 +03:00
if (m_backup && (m_logFile.size() >= m_maxSize)) {
2016-01-25 02:06:06 +03:00
closeLogFile();
int counter = 0;
QString backupLogFilename = m_path + ".bak";
while (QFile::exists(backupLogFilename)) {
++counter;
backupLogFilename = m_path + ".bak" + QString::number(counter);
}
QFile::rename(m_path, backupLogFilename);
openLogFile();
}
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)
|| !m_logFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner)) {
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
}