Implement logging

Fixes #6
This commit is contained in:
Rasmus Karlsson 2018-02-03 17:14:56 +01:00
parent 25979d4535
commit f9a25171bf
2 changed files with 53 additions and 19 deletions

View file

@ -7,18 +7,18 @@
namespace chatterino {
namespace singletons {
QByteArray endline("\n");
LoggingChannel::LoggingChannel(const QString &_channelName, const QString &_baseDirectory)
: channelName(_channelName)
, baseDirectory(_baseDirectory)
{
QDateTime now = QDateTime::currentDateTime();
QString baseFileName = this->channelName + "-" + now.toString("yyyy-MM-dd") + ".log";
this->dateString = this->generateDateString(now);
// Open file handle to log file of current date
this->fileHandle.setFileName(this->baseDirectory + QDir::separator() + baseFileName);
this->openLogFile();
this->fileHandle.open(QIODevice::Append);
this->appendLine(this->generateOpeningString(now));
}
@ -28,24 +28,38 @@ LoggingChannel::~LoggingChannel()
this->fileHandle.close();
}
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();
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("] ");
if ((message->flags & messages::Message::MessageFlags::System) != 0) {
str.append(message->searchText);
str.append('\n');
} else {
str.append(message->loginName);
str.append(": ");
str.append(message->searchText);
str.append('\n');
}
str.append(endline);
this->appendLine(str);
}
@ -56,7 +70,7 @@ QString LoggingChannel::generateOpeningString(const QDateTime &now) const
ret.append(now.toString("yyyy-MM-dd HH:mm:ss "));
ret.append(now.timeZoneAbbreviation());
ret.append('\n');
ret.append(endline);
return ret;
}
@ -67,16 +81,31 @@ QString LoggingChannel::generateClosingString(const QDateTime &now) const
ret.append(now.toString("yyyy-MM-dd HH:mm:ss"));
ret.append(now.timeZoneAbbreviation());
ret.append('\n');
ret.append(endline);
return ret;
}
void LoggingChannel::appendLine(const QString &line)
{
auto a1 = line.toUtf8();
auto a2 = line.toLatin1();
auto a3 = line.toLocal8Bit();
auto a4 = line.data();
auto a5 = line.toStdString();
// this->fileHandle.write(a5.c_str(), a5.length());
// this->fileHandle.write(a5.c_str(), a5.length());
this->fileHandle.write(line.toUtf8());
this->fileHandle.flush();
}
QString LoggingChannel::generateDateString(const QDateTime &now)
{
return now.toString("yyyy-MM-dd");
}
} // namespace singletons
} // namespace chatterino

View file

@ -21,17 +21,22 @@ public:
void addMessage(std::shared_ptr<messages::Message> message);
private:
void openLogFile();
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;
QString generateDateString(const QDateTime &now);
const QString channelName;
const QString baseDirectory;
QFile fileHandle;
QString dateString;
friend class LoggingManager;
};