From 65b1ed312c288a9400cb968bcdf450e0d9e7e1c9 Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 31 Dec 2023 13:51:40 +0100 Subject: [PATCH] refactor: Logging (chat logger) (#5058) It's no longer a singleton It's now a unique_ptr that dies together with the Application * Add getChatLogger to EmptyApplication * unrelated change: Access Application::instance statically * fix logging init order * Add changelog entry --- CHANGELOG.md | 1 + mocks/include/mocks/EmptyApplication.hpp | 6 ++++++ src/Application.cpp | 11 +++++++++-- src/Application.hpp | 11 +++++++++-- src/common/Channel.cpp | 3 ++- src/singletons/Logging.cpp | 2 +- src/singletons/Logging.hpp | 11 +++-------- 7 files changed, 31 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aedf2db88..717cc71f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,6 +87,7 @@ - Dev: Refactor `Emoji`'s EmojiMap into a vector. (#4980) - Dev: Refactor `DebugCount` and add copy button to debug popup. (#4921) - Dev: Refactor `common/Credentials`. (#4979) +- Dev: Refactor chat logger. (#5058) - Dev: Changed lifetime of context menus. (#4924) - Dev: Renamed `tools` directory to `scripts`. (#5035) - Dev: Refactor `ChannelView`, removing a bunch of clang-tidy warnings. (#4926) diff --git a/mocks/include/mocks/EmptyApplication.hpp b/mocks/include/mocks/EmptyApplication.hpp index 5917172b2..b1957898d 100644 --- a/mocks/include/mocks/EmptyApplication.hpp +++ b/mocks/include/mocks/EmptyApplication.hpp @@ -75,6 +75,12 @@ public: return nullptr; } + Logging *getChatLogger() override + { + assert(!"getChatLogger was called without being initialized"); + return nullptr; + } + ChatterinoBadges *getChatterinoBadges() override { return nullptr; diff --git a/src/Application.cpp b/src/Application.cpp index 5d6e4c63d..37ba100cd 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -128,12 +128,12 @@ Application::Application(Settings &_settings, Paths &_paths, const Args &_args) , userData(&this->emplace()) , sound(&this->emplace(makeSoundController(_settings))) , twitchLiveController(&this->emplace()) + , logging(new Logging(_settings)) #ifdef CHATTERINO_HAVE_PLUGINS , plugins(&this->emplace()) #endif - , logging(&this->emplace()) { - this->instance = this; + Application::instance = this; // We can safely ignore this signal's connection since the Application will always // be destroyed after fonts @@ -142,6 +142,8 @@ Application::Application(Settings &_settings, Paths &_paths, const Args &_args) }); } +Application::~Application() = default; + void Application::initialize(Settings &settings, Paths &paths) { assert(isAppInitialized == false); @@ -312,6 +314,11 @@ ITwitchIrcServer *Application::getTwitch() return this->twitch; } +Logging *Application::getChatLogger() +{ + return this->logging.get(); +} + void Application::save() { for (auto &singleton : this->singletons_) diff --git a/src/Application.hpp b/src/Application.hpp index 656c0510b..249f2acd0 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -68,6 +68,7 @@ public: virtual HighlightController *getHighlights() = 0; virtual NotificationController *getNotifications() = 0; virtual ITwitchIrcServer *getTwitch() = 0; + virtual Logging *getChatLogger() = 0; virtual ChatterinoBadges *getChatterinoBadges() = 0; virtual FfzBadges *getFfzBadges() = 0; virtual SeventvBadges *getSeventvBadges() = 0; @@ -89,6 +90,12 @@ public: static Application *instance; Application(Settings &_settings, Paths &_paths, const Args &_args); + ~Application() override; + + Application(const Application &) = delete; + Application(Application &&) = delete; + Application &operator=(const Application &) = delete; + Application &operator=(Application &&) = delete; void initialize(Settings &settings, Paths &paths); void load(); @@ -121,14 +128,13 @@ public: private: TwitchLiveController *const twitchLiveController{}; + const std::unique_ptr logging; public: #ifdef CHATTERINO_HAVE_PLUGINS PluginController *const plugins{}; #endif - /*[[deprecated]]*/ Logging *const logging{}; - const Args &getArgs() override { return this->args_; @@ -175,6 +181,7 @@ public: return this->highlights; } ITwitchIrcServer *getTwitch() override; + Logging *getChatLogger() override; ChatterinoBadges *getChatterinoBadges() override { return this->chatterinoBadges; diff --git a/src/common/Channel.cpp b/src/common/Channel.cpp index cd275c14a..ff5dd2834 100644 --- a/src/common/Channel.cpp +++ b/src/common/Channel.cpp @@ -101,7 +101,8 @@ void Channel::addMessage(MessagePtr message, { channelPlatform = "twitch"; } - app->logging->addMessage(this->name_, message, channelPlatform); + getIApp()->getChatLogger()->addMessage(this->name_, message, + channelPlatform); } if (this->messages_.pushBack(message, deleted)) diff --git a/src/singletons/Logging.cpp b/src/singletons/Logging.cpp index a83ecbd17..323a9d9da 100644 --- a/src/singletons/Logging.cpp +++ b/src/singletons/Logging.cpp @@ -12,7 +12,7 @@ namespace chatterino { -void Logging::initialize(Settings &settings, Paths & /*paths*/) +Logging::Logging(Settings &settings) { // We can safely ignore this signal connection since settings are only-ever destroyed // on application exit diff --git a/src/singletons/Logging.hpp b/src/singletons/Logging.hpp index 16c3cd81a..edd1ac07f 100644 --- a/src/singletons/Logging.hpp +++ b/src/singletons/Logging.hpp @@ -1,6 +1,5 @@ #pragma once -#include "common/Singleton.hpp" #include "util/QStringHash.hpp" #include "util/ThreadGuard.hpp" @@ -12,19 +11,15 @@ namespace chatterino { -class Paths; +class Settings; struct Message; using MessagePtr = std::shared_ptr; class LoggingChannel; -class Logging : public Singleton +class Logging { - Paths *pathManager = nullptr; - public: - Logging() = default; - - void initialize(Settings &settings, Paths &paths) override; + Logging(Settings &settings); void addMessage(const QString &channelName, MessagePtr message, const QString &platformName);