2017-03-11 11:32:19 +01:00
|
|
|
#include "loggingmanager.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace logging {
|
|
|
|
|
|
|
|
static QString logBasePath;
|
|
|
|
static QString channelBasePath;
|
|
|
|
static QString whispersBasePath;
|
|
|
|
static QString mentionsBasePath;
|
|
|
|
|
|
|
|
std::unordered_map<std::string, std::weak_ptr<Channel>> channels;
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void init()
|
2017-03-11 11:32:19 +01:00
|
|
|
{
|
|
|
|
// Make sure all folders are properly created
|
2017-04-12 17:46:44 +02:00
|
|
|
logBasePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) +
|
|
|
|
QDir::separator() + "Logs";
|
2017-03-11 11:32:19 +01:00
|
|
|
channelBasePath = logBasePath + QDir::separator() + "Channels";
|
|
|
|
whispersBasePath = logBasePath + QDir::separator() + "Whispers";
|
|
|
|
mentionsBasePath = logBasePath + QDir::separator() + "Mentions";
|
|
|
|
|
|
|
|
{
|
|
|
|
QDir dir(logBasePath);
|
|
|
|
if (!dir.exists()) {
|
|
|
|
dir.mkpath(".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QDir dir(channelBasePath);
|
|
|
|
if (!dir.exists()) {
|
|
|
|
dir.mkpath(".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QDir dir(whispersBasePath);
|
|
|
|
if (!dir.exists()) {
|
|
|
|
dir.mkpath(".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QDir dir(mentionsBasePath);
|
|
|
|
if (!dir.exists()) {
|
|
|
|
dir.mkpath(".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
static const QString &getBaseDirectory(const QString &channelName)
|
2017-03-11 11:32:19 +01:00
|
|
|
{
|
|
|
|
if (channelName == "/whispers") {
|
|
|
|
return whispersBasePath;
|
|
|
|
} else if (channelName == "/mentions") {
|
|
|
|
return mentionsBasePath;
|
|
|
|
} else {
|
|
|
|
return channelBasePath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
std::shared_ptr<Channel> get(const QString &channelName)
|
2017-03-11 11:32:19 +01:00
|
|
|
{
|
|
|
|
if (channelName.isEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &baseDirectory = getBaseDirectory(channelName);
|
|
|
|
|
|
|
|
auto channel = channels.find(channelName.toStdString());
|
|
|
|
if (channel == std::end(channels)) {
|
|
|
|
// This channel is definitely not logged yet.
|
|
|
|
// Create shared_ptr that we will return, and store a weak_ptr reference
|
|
|
|
std::shared_ptr<Channel> ret =
|
|
|
|
std::shared_ptr<Channel>(new Channel(channelName, baseDirectory));
|
|
|
|
|
|
|
|
channels[channelName.toStdString()] = std::weak_ptr<Channel>(ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto ret = channels[channelName.toStdString()].lock()) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Channel> ret =
|
|
|
|
std::shared_ptr<Channel>(new Channel(channelName, baseDirectory));
|
|
|
|
|
|
|
|
channels[channelName.toStdString()] = std::weak_ptr<Channel>(ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace logging
|
|
|
|
} // namespace chatterino
|