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

114 lines
2.6 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "loggingchannel.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, const QString &_baseDirectory)
: channelName(_channelName)
, baseDirectory(_baseDirectory)
{
QDateTime now = QDateTime::currentDateTime();
2018-02-03 17:14:56 +01:00
this->dateString = this->generateDateString(now);
2018-02-03 17:14:56 +01:00
this->openLogFile();
this->appendLine(this->generateOpeningString(now));
}
LoggingChannel::~LoggingChannel()
{
this->appendLine(this->generateClosingString());
this->fileHandle.close();
}
2018-02-03 17:14:56 +01:00
void LoggingChannel::openLogFile()
{
if (this->fileHandle.isOpen()) {
this->fileHandle.flush();
this->fileHandle.close();
}
QString baseFileName = this->channelName + "-" + this->dateString + ".log";
// Open file handle to log file of current date
this->fileHandle.setFileName(this->baseDirectory + QDir::separator() + baseFileName);
this->fileHandle.open(QIODevice::Append);
}
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)
{
/*
2018-02-03 17:14:56 +01:00
auto a1 = line.toUtf8();
auto a2 = line.toLatin1();
auto a3 = line.toLocal8Bit();
auto a4 = line.data();
auto a5 = line.toStdString();
*/
2018-02-03 17:14:56 +01:00
// this->fileHandle.write(a5.c_str(), a5.length());
// this->fileHandle.write(a5.c_str(), a5.length());
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