move around files
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
#include "loggingchannel.h"
|
||||
#include "loggingmanager.h"
|
||||
|
||||
#include <QDir>
|
||||
|
||||
#include <ctime>
|
||||
|
||||
namespace chatterino {
|
||||
namespace logging {
|
||||
|
||||
Channel::Channel(const QString &_channelName, const QString &_baseDirectory)
|
||||
: channelName(_channelName)
|
||||
, baseDirectory(_baseDirectory)
|
||||
{
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
|
||||
this->fileName = this->channelName + "-" + now.toString("yyyy-MM-dd") + ".log";
|
||||
|
||||
// Open file handle to log file of current date
|
||||
this->fileHandle.setFileName(this->baseDirectory + QDir::separator() + this->fileName);
|
||||
|
||||
this->fileHandle.open(QIODevice::Append);
|
||||
this->appendLine(this->generateOpeningString(now));
|
||||
}
|
||||
|
||||
Channel::~Channel()
|
||||
{
|
||||
this->appendLine(this->generateClosingString());
|
||||
this->fileHandle.close();
|
||||
}
|
||||
|
||||
void Channel::append(std::shared_ptr<messages::Message> message)
|
||||
{
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
|
||||
QString str;
|
||||
str.append('[');
|
||||
str.append(now.toString("HH:mm:ss"));
|
||||
str.append("] ");
|
||||
str.append(message->getUserName());
|
||||
str.append(": ");
|
||||
str.append(message->getContent());
|
||||
str.append('\n');
|
||||
this->appendLine(str);
|
||||
}
|
||||
|
||||
QString Channel::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 Channel::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 Channel::appendLine(const QString &line)
|
||||
{
|
||||
this->fileHandle.write(line.toUtf8());
|
||||
this->fileHandle.flush();
|
||||
}
|
||||
|
||||
} // namespace logging
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef LOGGINGCHANNEL_H
|
||||
#define LOGGINGCHANNEL_H
|
||||
|
||||
#include "messages/message.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QFile>
|
||||
#include <QString>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace chatterino {
|
||||
namespace logging {
|
||||
|
||||
class Channel
|
||||
{
|
||||
public:
|
||||
explicit Channel(const QString &_channelName, const QString &_baseDirectory);
|
||||
~Channel();
|
||||
|
||||
void append(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;
|
||||
};
|
||||
|
||||
} // namespace logging
|
||||
} // namespace chatterino
|
||||
|
||||
#endif // LOGGINGCHANNEL_H
|
||||
@@ -0,0 +1,100 @@
|
||||
#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;
|
||||
|
||||
void init()
|
||||
{
|
||||
// Make sure all folders are properly created
|
||||
logBasePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) +
|
||||
QDir::separator() + "Logs";
|
||||
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(".");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static const QString &getBaseDirectory(const QString &channelName)
|
||||
{
|
||||
if (channelName == "/whispers") {
|
||||
return whispersBasePath;
|
||||
} else if (channelName == "/mentions") {
|
||||
return mentionsBasePath;
|
||||
} else {
|
||||
return channelBasePath;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Channel> get(const QString &channelName)
|
||||
{
|
||||
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
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef LOGGINGMANAGER_H
|
||||
#define LOGGINGMANAGER_H
|
||||
|
||||
#include "loggingchannel.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace chatterino {
|
||||
namespace logging {
|
||||
|
||||
void init();
|
||||
std::shared_ptr<Channel> get(const QString &channelName);
|
||||
|
||||
} // namespace logging
|
||||
} // namespace chatterino
|
||||
|
||||
#endif // LOGGINGMANAGER_H
|
||||
Reference in New Issue
Block a user