mirror-chatterino2/src/singletons/Logging.cpp
askepticaldreamer 4c782ce90c
Add feature to select channels to log (#4302)
* Add checkbox for custom logging and table with channels to log on Logs page

* Add checkbox to enable and disable logging per channel

* Return from addMessage before logging if custom logging enabled and channel does not have logging enabled

* Use clang-format to fix formatting

* Add CHANGELOG.md entry

* Resolve PR comments

* Remove toggle for channels so any channel listed will be logged

* Move Only log channels listed below checkbox to just above table

* Fix formatting

* Re-order changelog

* ChannelLog constructor: Copy & move instead of const ref & copy

* ChannelLog::createEmpty: Curly brace initialize instead of repeating
name

* ChannelLog toString & createEmpty: nodiscard

* Use COUNT paradigm in model column

* Remove ChanneLoggingModel source file comments

* Use Column::Channel in getRowFromItem

* Rename `getItemFromRow` parameter and mark it as unused

* Curly brace initialize ChannelLog

* private & friend class the model

* Filter out channels to log using a set instead of iterating over a vector every time a message comes in

* Rename `ChannelLog::channel` member to `ChannelLog::channelName`

Also made it private

* mini comment on ChannelLog

Co-authored-by: Felanbird <41973452+Felanbird@users.noreply.github.com>
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
2023-01-15 11:47:22 +00:00

72 lines
1.9 KiB
C++

#include "singletons/Logging.hpp"
#include "singletons/helper/LoggingChannel.hpp"
#include "singletons/Paths.hpp"
#include "singletons/Settings.hpp"
#include <QDir>
#include <QStandardPaths>
#include <memory>
#include <utility>
namespace chatterino {
void Logging::initialize(Settings &settings, Paths & /*paths*/)
{
settings.loggedChannels.delayedItemsChanged.connect([this, &settings]() {
this->threadGuard.guard();
this->onlyLogListedChannels.clear();
for (const auto &loggedChannel : *settings.loggedChannels.readOnly())
{
this->onlyLogListedChannels.insert(loggedChannel.channelName());
}
});
}
void Logging::addMessage(const QString &channelName, MessagePtr message,
const QString &platformName)
{
this->threadGuard.guard();
if (!getSettings()->enableLogging)
{
return;
}
if (getSettings()->onlyLogListedChannels)
{
if (!this->onlyLogListedChannels.contains(channelName))
{
return;
}
}
auto platIt = this->loggingChannels_.find(platformName);
if (platIt == this->loggingChannels_.end())
{
auto channel = new LoggingChannel(channelName, platformName);
channel->addMessage(message);
auto map = std::map<QString, std::unique_ptr<LoggingChannel>>();
this->loggingChannels_[platformName] = std::move(map);
auto &ref = this->loggingChannels_.at(platformName);
ref.emplace(channelName, std::move(channel));
return;
}
auto chanIt = platIt->second.find(channelName);
if (chanIt == platIt->second.end())
{
auto channel = new LoggingChannel(channelName, platformName);
channel->addMessage(message);
platIt->second.emplace(channelName, std::move(channel));
}
else
{
chanIt->second->addMessage(message);
}
}
} // namespace chatterino