2017-03-11 11:32:19 +01:00
|
|
|
#include "loggingchannel.h"
|
|
|
|
#include "loggingmanager.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
|
|
|
namespace logging {
|
2017-03-11 11:32:19 +01:00
|
|
|
|
|
|
|
Channel::Channel(const QString &_channelName, const QString &_baseDirectory)
|
|
|
|
: channelName(_channelName)
|
|
|
|
, baseDirectory(_baseDirectory)
|
|
|
|
{
|
|
|
|
QDateTime now = QDateTime::currentDateTime();
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
this->fileName = this->channelName + "-" + now.toString("yyyy-MM-dd") + ".log";
|
2017-03-11 11:32:19 +01:00
|
|
|
|
|
|
|
// Open file handle to log file of current date
|
2017-04-12 17:46:44 +02:00
|
|
|
this->fileHandle.setFileName(this->baseDirectory + QDir::separator() + this->fileName);
|
2017-03-11 11:32:19 +01:00
|
|
|
|
|
|
|
this->fileHandle.open(QIODevice::Append);
|
|
|
|
this->appendLine(this->generateOpeningString(now));
|
|
|
|
}
|
|
|
|
|
|
|
|
Channel::~Channel()
|
|
|
|
{
|
|
|
|
this->appendLine(this->generateClosingString());
|
|
|
|
this->fileHandle.close();
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void Channel::append(std::shared_ptr<messages::Message> message)
|
2017-03-11 11:32:19 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
QString Channel::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());
|
|
|
|
ret.append('\n');
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
QString Channel::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());
|
|
|
|
ret.append('\n');
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void Channel::appendLine(const QString &line)
|
2017-03-11 11:32:19 +01:00
|
|
|
{
|
|
|
|
this->fileHandle.write(line.toUtf8());
|
|
|
|
this->fileHandle.flush();
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace logging
|
|
|
|
} // namespace chatterino
|