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
+39 -19
View File
@@ -1,5 +1,10 @@
#include "loggingchannel.hpp"
#include "application.hpp"
#include "debug/log.hpp"
#include "singletons/pathmanager.hpp"
#include "singletons/settingsmanager.hpp"
#include <QDir>
#include <ctime>
@@ -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<messages::Message> 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();
}
+3 -2
View File
@@ -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;
+1 -42
View File
@@ -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<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 chatterino
-3
View File
@@ -22,11 +22,8 @@ public:
void addMessage(const QString &channelName, messages::MessagePtr message);
void refreshLoggingPath();
private:
std::map<QString, std::unique_ptr<LoggingChannel>> loggingChannels;
QString getDirectoryForChannel(const QString &channelName);
};
} // namespace singletons
-18
View File
@@ -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)
+1 -4
View File
@@ -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;
+1 -2
View File
@@ -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"};