From 578795fbc3276e9011cc0d987720d3977e10057a Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Wed, 6 Jun 2018 20:30:11 +0200 Subject: [PATCH] Fix logging to a custom folder Progress on #352 --- src/singletons/helper/loggingchannel.cpp | 58 ++++++++++++------ src/singletons/helper/loggingchannel.hpp | 5 +- src/singletons/loggingmanager.cpp | 43 +------------ src/singletons/loggingmanager.hpp | 3 - src/singletons/pathmanager.cpp | 18 ------ src/singletons/pathmanager.hpp | 5 +- src/singletons/settingsmanager.hpp | 3 +- src/widgets/settingspages/moderationpage.cpp | 64 ++++++++------------ 8 files changed, 69 insertions(+), 130 deletions(-) diff --git a/src/singletons/helper/loggingchannel.cpp b/src/singletons/helper/loggingchannel.cpp index 475690964..363157958 100644 --- a/src/singletons/helper/loggingchannel.cpp +++ b/src/singletons/helper/loggingchannel.cpp @@ -1,5 +1,10 @@ #include "loggingchannel.hpp" +#include "application.hpp" +#include "debug/log.hpp" +#include "singletons/pathmanager.hpp" +#include "singletons/settingsmanager.hpp" + #include #include @@ -9,17 +14,30 @@ namespace singletons { QByteArray endline("\n"); -LoggingChannel::LoggingChannel(const QString &_channelName, const QString &_baseDirectory) +LoggingChannel::LoggingChannel(const QString &_channelName) : channelName(_channelName) - , baseDirectory(_baseDirectory) { - QDateTime now = QDateTime::currentDateTime(); + if (this->channelName.startsWith("/whispers")) { + this->subDirectory = "Whispers"; + } else if (channelName.startsWith("/mentions")) { + this->subDirectory = "Mentions"; + } else { + this->subDirectory = QStringLiteral("Channels") + QDir::separator() + channelName; + } - this->dateString = this->generateDateString(now); + auto app = getApp(); - this->openLogFile(); + app->settings->logPath.connect([this](const QString &logPath, auto) { + auto app = getApp(); - this->appendLine(this->generateOpeningString(now)); + if (logPath.isEmpty()) { + this->baseDirectory = app->paths->logsFolderPath; + } else { + this->baseDirectory = logPath; + } + + this->openLogFile(); + }); } LoggingChannel::~LoggingChannel() @@ -30,6 +48,9 @@ LoggingChannel::~LoggingChannel() void LoggingChannel::openLogFile() { + QDateTime now = QDateTime::currentDateTime(); + this->dateString = this->generateDateString(now); + if (this->fileHandle.isOpen()) { this->fileHandle.flush(); this->fileHandle.close(); @@ -37,10 +58,21 @@ void LoggingChannel::openLogFile() QString baseFileName = this->channelName + "-" + this->dateString + ".log"; + QString directory = this->baseDirectory + QDir::separator() + this->subDirectory; + + if (!QDir().mkpath(directory)) { + debug::Log("Unable to create logging path"); + return; + } + // Open file handle to log file of current date - this->fileHandle.setFileName(this->baseDirectory + QDir::separator() + baseFileName); + QString fileName = directory + QDir::separator() + baseFileName; + debug::Log("Logging to {}", fileName); + this->fileHandle.setFileName(fileName); this->fileHandle.open(QIODevice::Append); + + this->appendLine(this->generateOpeningString(now)); } void LoggingChannel::addMessage(std::shared_ptr message) @@ -88,18 +120,6 @@ QString LoggingChannel::generateClosingString(const QDateTime &now) const void LoggingChannel::appendLine(const QString &line) { - /* - auto a1 = line.toUtf8(); - auto a2 = line.toLatin1(); - auto a3 = line.toLocal8Bit(); - - auto a4 = line.data(); - - auto a5 = line.toStdString(); - */ - - // this->fileHandle.write(a5.c_str(), a5.length()); - // this->fileHandle.write(a5.c_str(), a5.length()); this->fileHandle.write(line.toUtf8()); this->fileHandle.flush(); } diff --git a/src/singletons/helper/loggingchannel.hpp b/src/singletons/helper/loggingchannel.hpp index 499169119..2a4dd4dd8 100644 --- a/src/singletons/helper/loggingchannel.hpp +++ b/src/singletons/helper/loggingchannel.hpp @@ -14,7 +14,7 @@ namespace singletons { class LoggingChannel : boost::noncopyable { - explicit LoggingChannel(const QString &_channelName, const QString &_baseDirectory); + explicit LoggingChannel(const QString &_channelName); public: ~LoggingChannel(); @@ -31,7 +31,8 @@ private: QString generateDateString(const QDateTime &now); const QString channelName; - const QString baseDirectory; + QString baseDirectory; + QString subDirectory; QFile fileHandle; diff --git a/src/singletons/loggingmanager.cpp b/src/singletons/loggingmanager.cpp index 9963d20c7..0078ccba2 100644 --- a/src/singletons/loggingmanager.cpp +++ b/src/singletons/loggingmanager.cpp @@ -28,7 +28,7 @@ void LoggingManager::addMessage(const QString &channelName, messages::MessagePtr auto it = this->loggingChannels.find(channelName); if (it == this->loggingChannels.end()) { - auto channel = new LoggingChannel(channelName, this->getDirectoryForChannel(channelName)); + auto channel = new LoggingChannel(channelName); channel->addMessage(message); this->loggingChannels.emplace(channelName, std::unique_ptr(std::move(channel))); @@ -37,46 +37,5 @@ void LoggingManager::addMessage(const QString &channelName, messages::MessagePtr } } -QString LoggingManager::getDirectoryForChannel(const QString &channelName) -{ - // auto customPath = getApp()->settings->logPath; - auto customPath = QString(""); - - if (channelName.startsWith("/whispers")) { - if (customPath != "") - return customPath + "/Whispers"; - return this->pathManager->whispersLogsFolderPath; - } else if (channelName.startsWith("/mentions")) { - if (customPath != "") - return customPath + "/Mentions"; - return this->pathManager->mentionsLogsFolderPath; - } else { - if (customPath != "") { - QString logPath(customPath + QDir::separator() + channelName); - - if (!this->pathManager->createFolder(logPath)) { - debug::Log("Error creating channel logs folder for channel {}", channelName); - } - - return logPath; - } - QString logPath(this->pathManager->channelsLogsFolderPath + QDir::separator() + - channelName); - - if (!this->pathManager->createFolder(logPath)) { - debug::Log("Error creating channel logs folder for channel {}", channelName); - } - - return logPath; - } -} - -void LoggingManager::refreshLoggingPath() -{ - // if (app->settings->logPath == "") { - - //} -} - } // namespace singletons } // namespace chatterino diff --git a/src/singletons/loggingmanager.hpp b/src/singletons/loggingmanager.hpp index b7e2d91c3..c67acfad0 100644 --- a/src/singletons/loggingmanager.hpp +++ b/src/singletons/loggingmanager.hpp @@ -22,11 +22,8 @@ public: void addMessage(const QString &channelName, messages::MessagePtr message); - void refreshLoggingPath(); - private: std::map> loggingChannels; - QString getDirectoryForChannel(const QString &channelName); }; } // namespace singletons diff --git a/src/singletons/pathmanager.cpp b/src/singletons/pathmanager.cpp index fb87e6a43..98553f941 100644 --- a/src/singletons/pathmanager.cpp +++ b/src/singletons/pathmanager.cpp @@ -66,24 +66,6 @@ PathManager::PathManager(int argc, char **argv) if (!QDir().mkpath(this->logsFolderPath)) { throw std::runtime_error("Error creating logs folder"); } - - this->channelsLogsFolderPath = this->logsFolderPath + "/Channels"; - - if (!QDir().mkpath(this->channelsLogsFolderPath)) { - throw std::runtime_error("Error creating channel logs folder"); - } - - this->whispersLogsFolderPath = this->logsFolderPath + "/Whispers"; - - if (!QDir().mkpath(this->whispersLogsFolderPath)) { - throw std::runtime_error("Error creating whisper logs folder"); - } - - this->mentionsLogsFolderPath = this->logsFolderPath + "/Mentions"; - - if (!QDir().mkpath(this->mentionsLogsFolderPath)) { - throw std::runtime_error("Error creating mentions logs folder"); - } } bool PathManager::createFolder(const QString &folderPath) diff --git a/src/singletons/pathmanager.hpp b/src/singletons/pathmanager.hpp index 5d377587a..bd3e079b7 100644 --- a/src/singletons/pathmanager.hpp +++ b/src/singletons/pathmanager.hpp @@ -19,11 +19,8 @@ public: // %APPDATA%/chatterino/Cache or ExecutablePath/Cache for portable mode QString cacheFolderPath; - // Logs + // Default folder for logs. %APPDATA%/chatterino/Logs or ExecutablePath/Logs for portable mode QString logsFolderPath; - QString channelsLogsFolderPath; - QString whispersLogsFolderPath; - QString mentionsLogsFolderPath; QString appPathHash; diff --git a/src/singletons/settingsmanager.hpp b/src/singletons/settingsmanager.hpp index 3a9491792..2e71ab748 100644 --- a/src/singletons/settingsmanager.hpp +++ b/src/singletons/settingsmanager.hpp @@ -107,8 +107,7 @@ public: /// Logging BoolSetting enableLogging = {"/logging/enabled", false}; - QStringSetting logPath = {"/logging/path", - QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)}; + QStringSetting logPath = {"/logging/path", ""}; QStringSetting pathHighlightSound = {"/highlighting/highlightSoundPath", "qrc:/sounds/ping2.wav"}; diff --git a/src/widgets/settingspages/moderationpage.cpp b/src/widgets/settingspages/moderationpage.cpp index d1c33c178..c5f2070d6 100644 --- a/src/widgets/settingspages/moderationpage.cpp +++ b/src/widgets/settingspages/moderationpage.cpp @@ -46,12 +46,16 @@ ModerationPage::ModerationPage() // Logs (copied from LoggingMananger) auto created = logs.emplace(); - if (app->settings->logPath == "") { - created->setText("Logs are saved to " + - CreateLink(QCoreApplication::applicationDirPath(), true)); - } else { - created->setText("Logs are saved to " + CreateLink(app->settings->logPath, true)); - } + + app->settings->logPath.connect([app, created](const QString &logPath, auto) mutable { + if (logPath == "") { + created->setText("Logs are saved to " + + CreateLink(app->paths->logsFolderPath, true)); + } else { + created->setText("Logs are saved to " + CreateLink(logPath, true)); + } + }); + created->setTextFormat(Qt::RichText); created->setTextInteractionFlags(Qt::TextBrowserInteraction | Qt::LinksAccessibleByKeyboard | @@ -60,44 +64,24 @@ ModerationPage::ModerationPage() logs.append(this->createCheckBox("Enable logging", app->settings->enableLogging)); logs->addStretch(1); - /* -auto selectDir = logs.emplace("Set custom logpath"); + auto selectDir = logs.emplace("Set custom logpath"); -// Setting custom logpath -QObject::connect( - selectDir.getElement(), &QPushButton::clicked, this, - [this, created, app, dirMemory = QString{app->settings->logPath}]() mutable { - auto dirName = QFileDialog::getExistingDirectory(this); - created->setText("Logs are saved to " + CreateLink(dirName, true)); + // Setting custom logpath + QObject::connect(selectDir.getElement(), &QPushButton::clicked, this, [this]() { + auto app = getApp(); + auto dirName = QFileDialog::getExistingDirectory(this); - if (dirName == "" && dirMemory == "") { - created->setText("Logs are saved to " + - CreateLink(QStandardPaths::writableLocation( - QStandardPaths::AppDataLocation), - true)); - } else if (dirName == "") { - dirName = dirMemory; - created->setText("Logs are saved to " + CreateLink(dirName, true)); - } + app->settings->logPath = dirName; + }); - app->settings->logPath = dirName; - dirMemory = dirName; - app->logging->refreshLoggingPath(); - }); -// Reset custom logpath -auto resetDir = logs.emplace("Reset logpath"); -QObject::connect( - resetDir.getElement(), &QPushButton::clicked, this, [this, created, app]() mutable { - app->settings->logPath = ""; - created->setText( - "Logs are saved to " + - CreateLink(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation), - true)); - app->logging->refreshLoggingPath(); - }); + // Reset custom logpath + auto resetDir = logs.emplace("Reset logpath"); + QObject::connect(resetDir.getElement(), &QPushButton::clicked, this, []() { + auto app = getApp(); + app->settings->logPath = ""; + }); -// Logs end -*/ + // Logs end } auto modMode = tabs.appendTab(new QVBoxLayout, "Moderation mode");