mirror-chatterino2/src/singletons/helper/loggingchannel.cpp

134 lines
3.3 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "loggingchannel.hpp"
#include "application.hpp"
#include "debug/log.hpp"
#include "singletons/pathmanager.hpp"
#include "singletons/settingsmanager.hpp"
#include <QDir>
#include <ctime>
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace singletons {
2018-02-03 17:14:56 +01:00
QByteArray endline("\n");
LoggingChannel::LoggingChannel(const QString &_channelName)
: channelName(_channelName)
{
if (this->channelName.startsWith("/whispers")) {
this->subDirectory = "Whispers";
} else if (channelName.startsWith("/mentions")) {
this->subDirectory = "Mentions";
} else {
this->subDirectory = QStringLiteral("Channels") + QDir::separator() + channelName;
}
auto app = getApp();
app->settings->logPath.connect([this](const QString &logPath, auto) {
auto app = getApp();
if (logPath.isEmpty()) {
this->baseDirectory = app->paths->logsFolderPath;
} else {
this->baseDirectory = logPath;
}
this->openLogFile();
});
}
LoggingChannel::~LoggingChannel()
{
this->appendLine(this->generateClosingString());
this->fileHandle.close();
}
2018-02-03 17:14:56 +01:00
void LoggingChannel::openLogFile()
{
QDateTime now = QDateTime::currentDateTime();
this->dateString = this->generateDateString(now);
2018-02-03 17:14:56 +01:00
if (this->fileHandle.isOpen()) {
this->fileHandle.flush();
this->fileHandle.close();
}
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;
}
2018-02-03 17:14:56 +01:00
// Open file handle to log file of current date
QString fileName = directory + QDir::separator() + baseFileName;
debug::Log("Logging to {}", fileName);
this->fileHandle.setFileName(fileName);
2018-02-03 17:14:56 +01:00
this->fileHandle.open(QIODevice::Append);
this->appendLine(this->generateOpeningString(now));
2018-02-03 17:14:56 +01:00
}
void LoggingChannel::addMessage(std::shared_ptr<messages::Message> message)
{
QDateTime now = QDateTime::currentDateTime();
2018-02-03 17:14:56 +01:00
QString messageDateString = this->generateDateString(now);
if (messageDateString != this->dateString) {
this->dateString = messageDateString;
this->openLogFile();
}
QString str;
str.append('[');
str.append(now.toString("HH:mm:ss"));
str.append("] ");
2018-02-03 17:14:56 +01:00
str.append(message->searchText);
str.append(endline);
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());
2018-02-03 17:14:56 +01:00
ret.append(endline);
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());
2018-02-03 17:14:56 +01:00
ret.append(endline);
return ret;
}
void LoggingChannel::appendLine(const QString &line)
{
this->fileHandle.write(line.toUtf8());
this->fileHandle.flush();
}
2018-02-03 17:14:56 +01:00
QString LoggingChannel::generateDateString(const QDateTime &now)
{
return now.toString("yyyy-MM-dd");
}
} // namespace singletons
2017-04-14 17:52:22 +02:00
} // namespace chatterino