Basic logging implemented

Implemented scuffed logging settings page
Add helper function to PathManager to create an arbitrary folder
This commit is contained in:
Rasmus Karlsson
2018-01-28 14:23:55 +01:00
parent f474db9443
commit 2f0844ebd9
14 changed files with 201 additions and 151 deletions
+82
View File
@@ -0,0 +1,82 @@
#include "loggingchannel.hpp"
#include <QDir>
#include <ctime>
namespace chatterino {
namespace singletons {
LoggingChannel::LoggingChannel(const QString &_channelName, const QString &_baseDirectory)
: channelName(_channelName)
, baseDirectory(_baseDirectory)
{
QDateTime now = QDateTime::currentDateTime();
QString baseFileName = this->channelName + "-" + now.toString("yyyy-MM-dd") + ".log";
// Open file handle to log file of current date
this->fileHandle.setFileName(this->baseDirectory + QDir::separator() + baseFileName);
this->fileHandle.open(QIODevice::Append);
this->appendLine(this->generateOpeningString(now));
}
LoggingChannel::~LoggingChannel()
{
this->appendLine(this->generateClosingString());
this->fileHandle.close();
}
void LoggingChannel::addMessage(std::shared_ptr<messages::Message> message)
{
QDateTime now = QDateTime::currentDateTime();
QString str;
str.append('[');
str.append(now.toString("HH:mm:ss"));
str.append("] ");
if ((message->flags & messages::Message::MessageFlags::System) != 0) {
str.append(message->searchText);
str.append('\n');
} else {
str.append(message->loginName);
str.append(": ");
str.append(message->searchText);
str.append('\n');
}
this->appendLine(str);
}
QString LoggingChannel::generateOpeningString(const QDateTime &now) const
{
QString ret = QLatin1Literal("# Start logging at ");
ret.append(now.toString("yyyy-MM-dd HH:mm:ss "));
ret.append(now.timeZoneAbbreviation());
ret.append('\n');
return ret;
}
QString LoggingChannel::generateClosingString(const QDateTime &now) const
{
QString ret = QLatin1Literal("# Stop logging at ");
ret.append(now.toString("yyyy-MM-dd HH:mm:ss"));
ret.append(now.timeZoneAbbreviation());
ret.append('\n');
return ret;
}
void LoggingChannel::appendLine(const QString &line)
{
this->fileHandle.write(line.toUtf8());
this->fileHandle.flush();
}
} // namespace singletons
} // namespace chatterino
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include "messages/message.hpp"
#include <QDateTime>
#include <QFile>
#include <QString>
#include <boost/noncopyable.hpp>
#include <memory>
namespace chatterino {
namespace singletons {
class LoggingChannel : boost::noncopyable
{
explicit LoggingChannel(const QString &_channelName, const QString &_baseDirectory);
public:
~LoggingChannel();
void addMessage(std::shared_ptr<messages::Message> message);
private:
QString generateOpeningString(const QDateTime &now = QDateTime::currentDateTime()) const;
QString generateClosingString(const QDateTime &now = QDateTime::currentDateTime()) const;
void appendLine(const QString &line);
private:
QString channelName;
const QString &baseDirectory;
QString fileName;
QFile fileHandle;
friend class LoggingManager;
};
} // namespace singletons
} // namespace chatterino
+61
View File
@@ -0,0 +1,61 @@
#include "singletons/loggingmanager.hpp"
#include "debug/log.hpp"
#include "singletons/pathmanager.hpp"
#include "singletons/settingsmanager.hpp"
#include <QDir>
#include <QStandardPaths>
#include <unordered_map>
namespace chatterino {
namespace singletons {
LoggingManager::LoggingManager()
: pathManager(PathManager::getInstance())
{
}
LoggingManager &LoggingManager::getInstance()
{
static LoggingManager instance;
return instance;
}
void LoggingManager::addMessage(const QString &channelName, messages::MessagePtr message)
{
const auto &settings = singletons::SettingManager::getInstance();
if (!settings.enableLogging) {
return;
}
auto it = this->loggingChannels.find(channelName);
if (it == this->loggingChannels.end()) {
auto channel = new LoggingChannel(channelName, this->getDirectoryForChannel(channelName));
channel->addMessage(message);
this->loggingChannels.emplace(channelName, std::move(channel));
} else {
it->second->addMessage(message);
}
}
QString LoggingManager::getDirectoryForChannel(const QString &channelName)
{
if (channelName.startsWith("/whispers")) {
return this->pathManager.whispersLogsFolderPath;
} else if (channelName.startsWith("/mentions")) {
return this->pathManager.mentionsLogsFolderPath;
} else {
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;
}
}
} // namespace singletons
} // namespace chatterino
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include "messages/message.hpp"
#include "singletons/helper/loggingchannel.hpp"
#include <boost/noncopyable.hpp>
#include <memory>
namespace chatterino {
namespace singletons {
class PathManager;
class LoggingManager : boost::noncopyable
{
LoggingManager();
PathManager &pathManager;
public:
static LoggingManager &getInstance();
void addMessage(const QString &channelName, messages::MessagePtr message);
private:
std::map<QString, std::unique_ptr<LoggingChannel>> loggingChannels;
QString getDirectoryForChannel(const QString &channelName);
};
} // namespace singletons
} // namespace chatterino
+36
View File
@@ -57,8 +57,44 @@ bool PathManager::init(int argc, char **argv)
return false;
}
this->logsFolderPath = rootPath + "/Logs";
if (!QDir().mkpath(this->logsFolderPath)) {
printf("Error creating logs directory: %s\n", qPrintable(this->logsFolderPath));
return false;
}
this->channelsLogsFolderPath = this->logsFolderPath + "/Channels";
if (!QDir().mkpath(this->channelsLogsFolderPath)) {
printf("Error creating channelsLogs directory: %s\n",
qPrintable(this->channelsLogsFolderPath));
return false;
}
this->whispersLogsFolderPath = this->logsFolderPath + "/Whispers";
if (!QDir().mkpath(this->whispersLogsFolderPath)) {
printf("Error creating whispersLogs directory: %s\n",
qPrintable(this->whispersLogsFolderPath));
return false;
}
this->mentionsLogsFolderPath = this->logsFolderPath + "/Mentions";
if (!QDir().mkpath(this->mentionsLogsFolderPath)) {
printf("Error creating mentionsLogs directory: %s\n",
qPrintable(this->mentionsLogsFolderPath));
return false;
}
return true;
}
bool PathManager::createFolder(const QString &folderPath)
{
return QDir().mkpath(folderPath);
}
} // namespace singletons
} // namespace chatterino
+8
View File
@@ -17,6 +17,14 @@ public:
QString settingsFolderPath;
QString customFolderPath;
QString cacheFolderPath;
// Logs
QString logsFolderPath;
QString channelsLogsFolderPath;
QString whispersLogsFolderPath;
QString mentionsLogsFolderPath;
bool createFolder(const QString &folderPath);
};
} // namespace singletons
+3
View File
@@ -89,6 +89,9 @@ public:
BoolSetting enableHighlightTaskbar = {"/highlighting/enableTaskbarFlashing", true};
BoolSetting customHighlightSound = {"/highlighting/useCustomSound", false};
/// Logging
BoolSetting enableLogging = {"/logging/enabled", false};
ChatterinoSetting<std::vector<messages::HighlightPhrase>> highlightProperties = {
"/highlighting/highlights"};