Browse Source

Merge pull request #17233 from Chocobo1/log

Add shutdown logging
adaptive-webui-19844
Chocobo1 2 years ago committed by GitHub
parent
commit
f3a4764a1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/app/application.cpp
  2. 10
      src/app/filelogger.cpp
  3. 42
      src/base/bittorrent/statistics.cpp
  4. 11
      src/base/bittorrent/statistics.h
  5. 4
      src/base/logger.cpp
  6. 4
      src/gui/log/logmodel.cpp

8
src/app/application.cpp

@ -810,6 +810,8 @@ void Application::cleanup()
if (!m_isCleanupRun.testAndSetAcquire(0, 1)) if (!m_isCleanupRun.testAndSetAcquire(0, 1))
return; return;
LogMsg(tr("qBittorrent termination initiated"));
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
if (m_window) if (m_window)
{ {
@ -850,12 +852,14 @@ void Application::cleanup()
Net::ProxyConfigurationManager::freeInstance(); Net::ProxyConfigurationManager::freeInstance();
Preferences::freeInstance(); Preferences::freeInstance();
SettingsStorage::freeInstance(); SettingsStorage::freeInstance();
delete m_fileLogger;
Logger::freeInstance();
IconProvider::freeInstance(); IconProvider::freeInstance();
SearchPluginManager::freeInstance(); SearchPluginManager::freeInstance();
Utils::Fs::removeDirRecursively(Utils::Fs::tempPath()); Utils::Fs::removeDirRecursively(Utils::Fs::tempPath());
LogMsg(tr("qBittorrent is now ready to exit"));
Logger::freeInstance();
delete m_fileLogger;
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
if (m_window) if (m_window)
{ {

10
src/app/filelogger.cpp

@ -134,19 +134,19 @@ void FileLogger::addLogMessage(const Log::Msg &msg)
switch (msg.type) switch (msg.type)
{ {
case Log::INFO: case Log::INFO:
stream << u"(I) "; stream << QStringView(u"(I) ");
break; break;
case Log::WARNING: case Log::WARNING:
stream << u"(W) "; stream << QStringView(u"(W) ");
break; break;
case Log::CRITICAL: case Log::CRITICAL:
stream << u"(C) "; stream << QStringView(u"(C) ");
break; break;
default: default:
stream << u"(N) "; stream << QStringView(u"(N) ");
} }
stream << QDateTime::fromMSecsSinceEpoch(msg.timestamp).toString(Qt::ISODate) << u" - " << msg.message << u'\n'; stream << QDateTime::fromSecsSinceEpoch(msg.timestamp).toString(Qt::ISODate) << QStringView(u" - ") << msg.message << QChar(u'\n');
if (m_backup && (m_logFile.size() >= m_maxSize)) if (m_backup && (m_logFile.size() >= m_maxSize))
{ {

42
src/base/bittorrent/statistics.cpp

@ -28,30 +28,34 @@
#include "statistics.h" #include "statistics.h"
#include <QDateTime> #include <chrono>
#include <QTimer>
#include "base/global.h" #include "base/global.h"
#include "base/bittorrent/session.h" #include "base/bittorrent/session.h"
#include "base/bittorrent/sessionstatus.h" #include "base/bittorrent/sessionstatus.h"
#include "base/profile.h" #include "base/profile.h"
const qint64 SAVE_INTERVAL = 15 * 60 * 1000; using namespace std::chrono_literals;
using namespace BitTorrent; using namespace BitTorrent;
const qint64 SAVE_INTERVAL = std::chrono::milliseconds(15min).count();
Statistics::Statistics(Session *session) Statistics::Statistics(Session *session)
: QObject(session) : QObject(session)
, m_session(session) , m_session(session)
{ {
load(); load();
connect(&m_timer, &QTimer::timeout, this, &Statistics::gather); m_lastUpdateTimer.start();
m_timer.start(60 * 1000);
auto *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &Statistics::gather);
timer->start(60s);
} }
Statistics::~Statistics() Statistics::~Statistics()
{ {
if (m_dirty)
m_lastWrite = 0;
save(); save();
} }
@ -79,23 +83,25 @@ void Statistics::gather()
m_dirty = true; m_dirty = true;
} }
if (m_lastUpdateTimer.hasExpired(SAVE_INTERVAL))
save(); save();
} }
void Statistics::save() const void Statistics::save() const
{ {
const qint64 now = QDateTime::currentMSecsSinceEpoch(); if (!m_dirty)
if (!m_dirty || ((now - m_lastWrite) < SAVE_INTERVAL))
return; return;
SettingsPtr s = Profile::instance()->applicationSettings(u"qBittorrent-data"_qs); const QVariantHash stats =
QVariantHash v; {
v.insert(u"AlltimeDL"_qs, (m_alltimeDL + m_sessionDL)); {u"AlltimeDL"_qs, (m_alltimeDL + m_sessionDL)},
v.insert(u"AlltimeUL"_qs, (m_alltimeUL + m_sessionUL)); {u"AlltimeUL"_qs, (m_alltimeUL + m_sessionUL)}
s->setValue(u"Stats/AllStats"_qs, v); };
SettingsPtr settings = Profile::instance()->applicationSettings(u"qBittorrent-data"_qs);
settings->setValue(u"Stats/AllStats"_qs, stats);
m_lastUpdateTimer.start();
m_dirty = false; m_dirty = false;
m_lastWrite = now;
} }
void Statistics::load() void Statistics::load()
@ -103,6 +109,6 @@ void Statistics::load()
const SettingsPtr s = Profile::instance()->applicationSettings(u"qBittorrent-data"_qs); const SettingsPtr s = Profile::instance()->applicationSettings(u"qBittorrent-data"_qs);
const QVariantHash v = s->value(u"Stats/AllStats"_qs).toHash(); const QVariantHash v = s->value(u"Stats/AllStats"_qs).toHash();
m_alltimeDL = v[u"AlltimeDL"_qs].toULongLong(); m_alltimeDL = v[u"AlltimeDL"_qs].toLongLong();
m_alltimeUL = v[u"AlltimeUL"_qs].toULongLong(); m_alltimeUL = v[u"AlltimeUL"_qs].toLongLong();
} }

11
src/base/bittorrent/statistics.h

@ -28,8 +28,8 @@
#pragma once #pragma once
#include <QElapsedTimer>
#include <QObject> #include <QObject>
#include <QTimer>
namespace BitTorrent namespace BitTorrent
{ {
@ -55,13 +55,12 @@ private:
void save() const; void save() const;
void load(); void load();
BitTorrent::Session *m_session; BitTorrent::Session *m_session = nullptr;
mutable QElapsedTimer m_lastUpdateTimer;
mutable bool m_dirty = false;
qint64 m_alltimeUL = 0; qint64 m_alltimeUL = 0;
qint64 m_alltimeDL = 0; qint64 m_alltimeDL = 0;
qint64 m_sessionUL = 0; qint64 m_sessionUL = 0;
qint64 m_sessionDL = 0; qint64 m_sessionDL = 0;
mutable qint64 m_lastWrite = 0;
mutable bool m_dirty = false;
QTimer m_timer;
}; };

4
src/base/logger.cpp

@ -73,7 +73,7 @@ void Logger::freeInstance()
void Logger::addMessage(const QString &message, const Log::MsgType &type) void Logger::addMessage(const QString &message, const Log::MsgType &type)
{ {
QWriteLocker locker(&m_lock); QWriteLocker locker(&m_lock);
const Log::Msg msg = {m_msgCounter++, type, QDateTime::currentMSecsSinceEpoch(), message}; const Log::Msg msg = {m_msgCounter++, type, QDateTime::currentSecsSinceEpoch(), message};
m_messages.push_back(msg); m_messages.push_back(msg);
locker.unlock(); locker.unlock();
@ -83,7 +83,7 @@ void Logger::addMessage(const QString &message, const Log::MsgType &type)
void Logger::addPeer(const QString &ip, const bool blocked, const QString &reason) void Logger::addPeer(const QString &ip, const bool blocked, const QString &reason)
{ {
QWriteLocker locker(&m_lock); QWriteLocker locker(&m_lock);
const Log::Peer msg = {m_peerCounter++, blocked, QDateTime::currentMSecsSinceEpoch(), ip, reason}; const Log::Peer msg = {m_peerCounter++, blocked, QDateTime::currentSecsSinceEpoch(), ip, reason};
m_peers.push_back(msg); m_peers.push_back(msg);
locker.unlock(); locker.unlock();

4
src/gui/log/logmodel.cpp

@ -155,7 +155,7 @@ LogMessageModel::LogMessageModel(QObject *parent)
void LogMessageModel::handleNewMessage(const Log::Msg &message) void LogMessageModel::handleNewMessage(const Log::Msg &message)
{ {
const QString time = QLocale::system().toString(QDateTime::fromMSecsSinceEpoch(message.timestamp), QLocale::ShortFormat); const QString time = QLocale::system().toString(QDateTime::fromSecsSinceEpoch(message.timestamp), QLocale::ShortFormat);
const QString messageText = message.message; const QString messageText = message.message;
const QColor foreground = m_foregroundForMessageTypes[message.type]; const QColor foreground = m_foregroundForMessageTypes[message.type];
@ -173,7 +173,7 @@ LogPeerModel::LogPeerModel(QObject *parent)
void LogPeerModel::handleNewMessage(const Log::Peer &peer) void LogPeerModel::handleNewMessage(const Log::Peer &peer)
{ {
const QString time = QLocale::system().toString(QDateTime::fromMSecsSinceEpoch(peer.timestamp), QLocale::ShortFormat); const QString time = QLocale::system().toString(QDateTime::fromSecsSinceEpoch(peer.timestamp), QLocale::ShortFormat);
const QString message = peer.blocked const QString message = peer.blocked
? tr("%1 was blocked. Reason: %2.", "0.0.0.0 was blocked. Reason: reason for blocking.").arg(peer.ip, peer.reason) ? tr("%1 was blocked. Reason: %2.", "0.0.0.0 was blocked. Reason: reason for blocking.").arg(peer.ip, peer.reason)
: tr("%1 was banned", "0.0.0.0 was banned").arg(peer.ip); : tr("%1 was banned", "0.0.0.0 was banned").arg(peer.ip);

Loading…
Cancel
Save