Fix logging to a custom folder

Progress on #352
This commit is contained in:
Rasmus Karlsson 2018-06-06 20:30:11 +02:00
parent 7cd2d77524
commit 578795fbc3
8 changed files with 69 additions and 130 deletions

View file

@ -1,5 +1,10 @@
#include "loggingchannel.hpp" #include "loggingchannel.hpp"
#include "application.hpp"
#include "debug/log.hpp"
#include "singletons/pathmanager.hpp"
#include "singletons/settingsmanager.hpp"
#include <QDir> #include <QDir>
#include <ctime> #include <ctime>
@ -9,17 +14,30 @@ namespace singletons {
QByteArray endline("\n"); QByteArray endline("\n");
LoggingChannel::LoggingChannel(const QString &_channelName, const QString &_baseDirectory) LoggingChannel::LoggingChannel(const QString &_channelName)
: channelName(_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() LoggingChannel::~LoggingChannel()
@ -30,6 +48,9 @@ LoggingChannel::~LoggingChannel()
void LoggingChannel::openLogFile() void LoggingChannel::openLogFile()
{ {
QDateTime now = QDateTime::currentDateTime();
this->dateString = this->generateDateString(now);
if (this->fileHandle.isOpen()) { if (this->fileHandle.isOpen()) {
this->fileHandle.flush(); this->fileHandle.flush();
this->fileHandle.close(); this->fileHandle.close();
@ -37,10 +58,21 @@ void LoggingChannel::openLogFile()
QString baseFileName = this->channelName + "-" + this->dateString + ".log"; 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 // 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->fileHandle.open(QIODevice::Append);
this->appendLine(this->generateOpeningString(now));
} }
void LoggingChannel::addMessage(std::shared_ptr<messages::Message> message) void LoggingChannel::addMessage(std::shared_ptr<messages::Message> message)
@ -88,18 +120,6 @@ QString LoggingChannel::generateClosingString(const QDateTime &now) const
void LoggingChannel::appendLine(const QString &line) 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.write(line.toUtf8());
this->fileHandle.flush(); this->fileHandle.flush();
} }

View file

@ -14,7 +14,7 @@ namespace singletons {
class LoggingChannel : boost::noncopyable class LoggingChannel : boost::noncopyable
{ {
explicit LoggingChannel(const QString &_channelName, const QString &_baseDirectory); explicit LoggingChannel(const QString &_channelName);
public: public:
~LoggingChannel(); ~LoggingChannel();
@ -31,7 +31,8 @@ private:
QString generateDateString(const QDateTime &now); QString generateDateString(const QDateTime &now);
const QString channelName; const QString channelName;
const QString baseDirectory; QString baseDirectory;
QString subDirectory;
QFile fileHandle; QFile fileHandle;

View file

@ -28,7 +28,7 @@ void LoggingManager::addMessage(const QString &channelName, messages::MessagePtr
auto it = this->loggingChannels.find(channelName); auto it = this->loggingChannels.find(channelName);
if (it == this->loggingChannels.end()) { if (it == this->loggingChannels.end()) {
auto channel = new LoggingChannel(channelName, this->getDirectoryForChannel(channelName)); auto channel = new LoggingChannel(channelName);
channel->addMessage(message); channel->addMessage(message);
this->loggingChannels.emplace(channelName, this->loggingChannels.emplace(channelName,
std::unique_ptr<LoggingChannel>(std::move(channel))); std::unique_ptr<LoggingChannel>(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 singletons
} // namespace chatterino } // namespace chatterino

View file

@ -22,11 +22,8 @@ public:
void addMessage(const QString &channelName, messages::MessagePtr message); void addMessage(const QString &channelName, messages::MessagePtr message);
void refreshLoggingPath();
private: private:
std::map<QString, std::unique_ptr<LoggingChannel>> loggingChannels; std::map<QString, std::unique_ptr<LoggingChannel>> loggingChannels;
QString getDirectoryForChannel(const QString &channelName);
}; };
} // namespace singletons } // namespace singletons

View file

@ -66,24 +66,6 @@ PathManager::PathManager(int argc, char **argv)
if (!QDir().mkpath(this->logsFolderPath)) { if (!QDir().mkpath(this->logsFolderPath)) {
throw std::runtime_error("Error creating logs folder"); 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) bool PathManager::createFolder(const QString &folderPath)

View file

@ -19,11 +19,8 @@ public:
// %APPDATA%/chatterino/Cache or ExecutablePath/Cache for portable mode // %APPDATA%/chatterino/Cache or ExecutablePath/Cache for portable mode
QString cacheFolderPath; QString cacheFolderPath;
// Logs // Default folder for logs. %APPDATA%/chatterino/Logs or ExecutablePath/Logs for portable mode
QString logsFolderPath; QString logsFolderPath;
QString channelsLogsFolderPath;
QString whispersLogsFolderPath;
QString mentionsLogsFolderPath;
QString appPathHash; QString appPathHash;

View file

@ -107,8 +107,7 @@ public:
/// Logging /// Logging
BoolSetting enableLogging = {"/logging/enabled", false}; BoolSetting enableLogging = {"/logging/enabled", false};
QStringSetting logPath = {"/logging/path", QStringSetting logPath = {"/logging/path", ""};
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)};
QStringSetting pathHighlightSound = {"/highlighting/highlightSoundPath", QStringSetting pathHighlightSound = {"/highlighting/highlightSoundPath",
"qrc:/sounds/ping2.wav"}; "qrc:/sounds/ping2.wav"};

View file

@ -46,12 +46,16 @@ ModerationPage::ModerationPage()
// Logs (copied from LoggingMananger) // Logs (copied from LoggingMananger)
auto created = logs.emplace<QLabel>(); auto created = logs.emplace<QLabel>();
if (app->settings->logPath == "") {
created->setText("Logs are saved to " + app->settings->logPath.connect([app, created](const QString &logPath, auto) mutable {
CreateLink(QCoreApplication::applicationDirPath(), true)); if (logPath == "") {
} else { created->setText("Logs are saved to " +
created->setText("Logs are saved to " + CreateLink(app->settings->logPath, true)); CreateLink(app->paths->logsFolderPath, true));
} } else {
created->setText("Logs are saved to " + CreateLink(logPath, true));
}
});
created->setTextFormat(Qt::RichText); created->setTextFormat(Qt::RichText);
created->setTextInteractionFlags(Qt::TextBrowserInteraction | created->setTextInteractionFlags(Qt::TextBrowserInteraction |
Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByKeyboard |
@ -60,44 +64,24 @@ ModerationPage::ModerationPage()
logs.append(this->createCheckBox("Enable logging", app->settings->enableLogging)); logs.append(this->createCheckBox("Enable logging", app->settings->enableLogging));
logs->addStretch(1); logs->addStretch(1);
/* auto selectDir = logs.emplace<QPushButton>("Set custom logpath");
auto selectDir = logs.emplace<QPushButton>("Set custom logpath");
// Setting custom logpath // Setting custom logpath
QObject::connect( QObject::connect(selectDir.getElement(), &QPushButton::clicked, this, [this]() {
selectDir.getElement(), &QPushButton::clicked, this, auto app = getApp();
[this, created, app, dirMemory = QString{app->settings->logPath}]() mutable { auto dirName = QFileDialog::getExistingDirectory(this);
auto dirName = QFileDialog::getExistingDirectory(this);
created->setText("Logs are saved to " + CreateLink(dirName, true));
if (dirName == "" && dirMemory == "") { app->settings->logPath = dirName;
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; // Reset custom logpath
dirMemory = dirName; auto resetDir = logs.emplace<QPushButton>("Reset logpath");
app->logging->refreshLoggingPath(); QObject::connect(resetDir.getElement(), &QPushButton::clicked, this, []() {
}); auto app = getApp();
// Reset custom logpath app->settings->logPath = "";
auto resetDir = logs.emplace<QPushButton>("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();
});
// Logs end // Logs end
*/
} }
auto modMode = tabs.appendTab(new QVBoxLayout, "Moderation mode"); auto modMode = tabs.appendTab(new QVBoxLayout, "Moderation mode");