2018-06-26 14:09:39 +02:00
|
|
|
#include "LoggingChannel.hpp"
|
2017-03-11 11:32:19 +01:00
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "Application.hpp"
|
|
|
|
#include "debug/Log.hpp"
|
2018-06-28 19:46:45 +02:00
|
|
|
#include "singletons/Paths.hpp"
|
|
|
|
#include "singletons/Settings.hpp"
|
2018-06-06 20:30:11 +02:00
|
|
|
|
2017-03-11 11:32:19 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
2017-03-11 11:32:19 +01:00
|
|
|
|
2018-02-03 17:14:56 +01:00
|
|
|
QByteArray endline("\n");
|
|
|
|
|
2018-06-06 20:30:11 +02:00
|
|
|
LoggingChannel::LoggingChannel(const QString &_channelName)
|
2017-03-11 11:32:19 +01:00
|
|
|
: channelName(_channelName)
|
|
|
|
{
|
2018-06-06 20:30:11 +02:00
|
|
|
if (this->channelName.startsWith("/whispers")) {
|
|
|
|
this->subDirectory = "Whispers";
|
|
|
|
} else if (channelName.startsWith("/mentions")) {
|
|
|
|
this->subDirectory = "Mentions";
|
|
|
|
} else {
|
2018-08-06 21:17:03 +02:00
|
|
|
this->subDirectory =
|
|
|
|
QStringLiteral("Channels") + QDir::separator() + channelName;
|
2018-06-06 20:30:11 +02:00
|
|
|
}
|
2017-03-11 11:32:19 +01:00
|
|
|
|
2018-06-21 13:02:34 +02:00
|
|
|
// FOURTF: change this when adding more providers
|
|
|
|
this->subDirectory = "Twitch/" + this->subDirectory;
|
|
|
|
|
2018-06-06 20:30:11 +02:00
|
|
|
auto app = getApp();
|
2017-03-11 11:32:19 +01:00
|
|
|
|
2018-06-06 20:30:11 +02:00
|
|
|
app->settings->logPath.connect([this](const QString &logPath, auto) {
|
|
|
|
auto app = getApp();
|
2017-03-11 11:32:19 +01:00
|
|
|
|
2018-06-06 20:30:11 +02:00
|
|
|
if (logPath.isEmpty()) {
|
2018-06-21 13:02:34 +02:00
|
|
|
this->baseDirectory = app->paths->messageLogDirectory;
|
2018-06-06 20:30:11 +02:00
|
|
|
} else {
|
|
|
|
this->baseDirectory = logPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->openLogFile();
|
|
|
|
});
|
2017-03-11 11:32:19 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 14:23:55 +01:00
|
|
|
LoggingChannel::~LoggingChannel()
|
2017-03-11 11:32:19 +01:00
|
|
|
{
|
|
|
|
this->appendLine(this->generateClosingString());
|
|
|
|
this->fileHandle.close();
|
|
|
|
}
|
|
|
|
|
2018-02-03 17:14:56 +01:00
|
|
|
void LoggingChannel::openLogFile()
|
|
|
|
{
|
2018-06-06 20:30:11 +02:00
|
|
|
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";
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
QString directory =
|
|
|
|
this->baseDirectory + QDir::separator() + this->subDirectory;
|
2018-06-06 20:30:11 +02:00
|
|
|
|
|
|
|
if (!QDir().mkpath(directory)) {
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("Unable to create logging path");
|
2018-06-06 20:30:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-03 17:14:56 +01:00
|
|
|
// Open file handle to log file of current date
|
2018-06-06 20:30:11 +02:00
|
|
|
QString fileName = directory + QDir::separator() + baseFileName;
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("Logging to {}", fileName);
|
2018-06-06 20:30:11 +02:00
|
|
|
this->fileHandle.setFileName(fileName);
|
2018-02-03 17:14:56 +01:00
|
|
|
|
|
|
|
this->fileHandle.open(QIODevice::Append);
|
2018-06-06 20:30:11 +02:00
|
|
|
|
|
|
|
this->appendLine(this->generateOpeningString(now));
|
2018-02-03 17:14:56 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
void LoggingChannel::addMessage(std::shared_ptr<Message> message)
|
2017-03-11 11:32:19 +01:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-03-11 11:32:19 +01:00
|
|
|
QString str;
|
|
|
|
str.append('[');
|
|
|
|
str.append(now.toString("HH:mm:ss"));
|
|
|
|
str.append("] ");
|
2018-01-28 14:23:55 +01:00
|
|
|
|
2018-02-03 17:14:56 +01:00
|
|
|
str.append(message->searchText);
|
|
|
|
str.append(endline);
|
2018-01-28 14:23:55 +01:00
|
|
|
|
2017-03-11 11:32:19 +01:00
|
|
|
this->appendLine(str);
|
|
|
|
}
|
|
|
|
|
2018-01-28 14:23:55 +01:00
|
|
|
QString LoggingChannel::generateOpeningString(const QDateTime &now) const
|
2017-03-11 11:32:19 +01:00
|
|
|
{
|
|
|
|
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);
|
2017-03-11 11:32:19 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-28 14:23:55 +01:00
|
|
|
QString LoggingChannel::generateClosingString(const QDateTime &now) const
|
2017-03-11 11:32:19 +01:00
|
|
|
{
|
|
|
|
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);
|
2017-03-11 11:32:19 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-28 14:23:55 +01:00
|
|
|
void LoggingChannel::appendLine(const QString &line)
|
2017-03-11 11:32:19 +01:00
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|