Added option to log streams by their ID, allowing for easier "per-stream" log analyzing (#5507)
This commit is contained in:
@@ -33,7 +33,7 @@ Logging::Logging(Settings &settings)
|
||||
}
|
||||
|
||||
void Logging::addMessage(const QString &channelName, MessagePtr message,
|
||||
const QString &platformName)
|
||||
const QString &platformName, const QString &streamID)
|
||||
{
|
||||
this->threadGuard.guard();
|
||||
|
||||
@@ -54,7 +54,7 @@ void Logging::addMessage(const QString &channelName, MessagePtr message,
|
||||
if (platIt == this->loggingChannels_.end())
|
||||
{
|
||||
auto *channel = new LoggingChannel(channelName, platformName);
|
||||
channel->addMessage(message);
|
||||
channel->addMessage(message, streamID);
|
||||
auto map = std::map<QString, std::unique_ptr<LoggingChannel>>();
|
||||
this->loggingChannels_[platformName] = std::move(map);
|
||||
auto &ref = this->loggingChannels_.at(platformName);
|
||||
@@ -65,12 +65,12 @@ void Logging::addMessage(const QString &channelName, MessagePtr message,
|
||||
if (chanIt == platIt->second.end())
|
||||
{
|
||||
auto *channel = new LoggingChannel(channelName, platformName);
|
||||
channel->addMessage(message);
|
||||
channel->addMessage(message, streamID);
|
||||
platIt->second.emplace(channelName, std::move(channel));
|
||||
}
|
||||
else
|
||||
{
|
||||
chanIt->second->addMessage(message);
|
||||
chanIt->second->addMessage(message, streamID);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,8 @@ public:
|
||||
virtual ~ILogging() = default;
|
||||
|
||||
virtual void addMessage(const QString &channelName, MessagePtr message,
|
||||
const QString &platformName) = 0;
|
||||
const QString &platformName,
|
||||
const QString &streamID) = 0;
|
||||
};
|
||||
|
||||
class Logging : public ILogging
|
||||
@@ -31,7 +32,8 @@ public:
|
||||
Logging(Settings &settings);
|
||||
|
||||
void addMessage(const QString &channelName, MessagePtr message,
|
||||
const QString &platformName) override;
|
||||
const QString &platformName,
|
||||
const QString &streamID) override;
|
||||
|
||||
private:
|
||||
using PlatformName = QString;
|
||||
|
||||
@@ -447,6 +447,10 @@ public:
|
||||
BoolSetting enableLogging = {"/logging/enabled", false};
|
||||
BoolSetting onlyLogListedChannels = {"/logging/onlyLogListedChannels",
|
||||
false};
|
||||
BoolSetting separatelyStoreStreamLogs = {
|
||||
"/logging/separatelyStoreStreamLogs",
|
||||
false,
|
||||
};
|
||||
|
||||
QStringSetting logPath = {"/logging/path", ""};
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "LoggingChannel.hpp"
|
||||
#include "singletons/helper/LoggingChannel.hpp"
|
||||
|
||||
#include "Application.hpp"
|
||||
#include "common/QLogging.hpp"
|
||||
@@ -7,16 +7,58 @@
|
||||
#include "singletons/Paths.hpp"
|
||||
#include "singletons/Settings.hpp"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDir>
|
||||
|
||||
namespace {
|
||||
|
||||
const QByteArray ENDLINE("\n");
|
||||
|
||||
void appendLine(QFile &fileHandle, const QString &line)
|
||||
{
|
||||
assert(fileHandle.isOpen());
|
||||
assert(fileHandle.isWritable());
|
||||
|
||||
fileHandle.write(line.toUtf8());
|
||||
fileHandle.flush();
|
||||
}
|
||||
|
||||
QString generateOpeningString(
|
||||
const QDateTime &now = QDateTime::currentDateTime())
|
||||
{
|
||||
QString ret("# Start logging at ");
|
||||
|
||||
ret.append(now.toString("yyyy-MM-dd HH:mm:ss "));
|
||||
ret.append(now.timeZoneAbbreviation());
|
||||
ret.append(ENDLINE);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
QString generateClosingString(
|
||||
const QDateTime &now = QDateTime::currentDateTime())
|
||||
{
|
||||
QString ret("# Stop logging at ");
|
||||
|
||||
ret.append(now.toString("yyyy-MM-dd HH:mm:ss"));
|
||||
ret.append(now.timeZoneAbbreviation());
|
||||
ret.append(ENDLINE);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
QString generateDateString(const QDateTime &now)
|
||||
{
|
||||
return now.toString("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
QByteArray endline("\n");
|
||||
|
||||
LoggingChannel::LoggingChannel(const QString &_channelName,
|
||||
const QString &_platform)
|
||||
: channelName(_channelName)
|
||||
, platform(_platform)
|
||||
LoggingChannel::LoggingChannel(QString _channelName, QString _platform)
|
||||
: channelName(std::move(_channelName))
|
||||
, platform(std::move(_platform))
|
||||
{
|
||||
if (this->channelName.startsWith("/whispers"))
|
||||
{
|
||||
@@ -54,14 +96,15 @@ LoggingChannel::LoggingChannel(const QString &_channelName,
|
||||
|
||||
LoggingChannel::~LoggingChannel()
|
||||
{
|
||||
this->appendLine(this->generateClosingString());
|
||||
appendLine(this->fileHandle, generateClosingString());
|
||||
this->fileHandle.close();
|
||||
this->currentStreamFileHandle.close();
|
||||
}
|
||||
|
||||
void LoggingChannel::openLogFile()
|
||||
{
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
this->dateString = this->generateDateString(now);
|
||||
this->dateString = generateDateString(now);
|
||||
|
||||
if (this->fileHandle.isOpen())
|
||||
{
|
||||
@@ -87,14 +130,45 @@ void LoggingChannel::openLogFile()
|
||||
|
||||
this->fileHandle.open(QIODevice::Append);
|
||||
|
||||
this->appendLine(this->generateOpeningString(now));
|
||||
appendLine(this->fileHandle, generateOpeningString(now));
|
||||
}
|
||||
|
||||
void LoggingChannel::addMessage(MessagePtr message)
|
||||
void LoggingChannel::openStreamLogFile(const QString &streamID)
|
||||
{
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
this->currentStreamID = streamID;
|
||||
|
||||
if (this->currentStreamFileHandle.isOpen())
|
||||
{
|
||||
this->currentStreamFileHandle.flush();
|
||||
this->currentStreamFileHandle.close();
|
||||
}
|
||||
|
||||
QString baseFileName = this->channelName + "-" + streamID + ".log";
|
||||
|
||||
QString directory =
|
||||
this->baseDirectory + QDir::separator() + this->subDirectory;
|
||||
|
||||
if (!QDir().mkpath(directory))
|
||||
{
|
||||
qCDebug(chatterinoHelper) << "Unable to create logging path";
|
||||
return;
|
||||
}
|
||||
|
||||
QString fileName = directory + QDir::separator() + baseFileName;
|
||||
qCDebug(chatterinoHelper) << "Logging stream to" << fileName;
|
||||
this->currentStreamFileHandle.setFileName(fileName);
|
||||
|
||||
this->currentStreamFileHandle.open(QIODevice::Append);
|
||||
appendLine(this->currentStreamFileHandle, generateOpeningString(now));
|
||||
}
|
||||
|
||||
void LoggingChannel::addMessage(const MessagePtr &message,
|
||||
const QString &streamID)
|
||||
{
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
|
||||
QString messageDateString = this->generateDateString(now);
|
||||
QString messageDateString = generateDateString(now);
|
||||
if (messageDateString != this->dateString)
|
||||
{
|
||||
this->dateString = messageDateString;
|
||||
@@ -154,42 +228,19 @@ void LoggingChannel::addMessage(MessagePtr message)
|
||||
}
|
||||
}
|
||||
str.append(messageText);
|
||||
str.append(endline);
|
||||
str.append(ENDLINE);
|
||||
|
||||
this->appendLine(str);
|
||||
}
|
||||
appendLine(this->fileHandle, str);
|
||||
|
||||
QString LoggingChannel::generateOpeningString(const QDateTime &now) const
|
||||
{
|
||||
QString ret("# Start logging at ");
|
||||
if (!streamID.isEmpty() && getSettings()->separatelyStoreStreamLogs)
|
||||
{
|
||||
if (this->currentStreamID != streamID)
|
||||
{
|
||||
this->openStreamLogFile(streamID);
|
||||
}
|
||||
|
||||
ret.append(now.toString("yyyy-MM-dd HH:mm:ss "));
|
||||
ret.append(now.timeZoneAbbreviation());
|
||||
ret.append(endline);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
QString LoggingChannel::generateClosingString(const QDateTime &now) const
|
||||
{
|
||||
QString ret("# Stop logging at ");
|
||||
|
||||
ret.append(now.toString("yyyy-MM-dd HH:mm:ss"));
|
||||
ret.append(now.timeZoneAbbreviation());
|
||||
ret.append(endline);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void LoggingChannel::appendLine(const QString &line)
|
||||
{
|
||||
this->fileHandle.write(line.toUtf8());
|
||||
this->fileHandle.flush();
|
||||
}
|
||||
|
||||
QString LoggingChannel::generateDateString(const QDateTime &now)
|
||||
{
|
||||
return now.toString("yyyy-MM-dd");
|
||||
appendLine(this->currentStreamFileHandle, str);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QFile>
|
||||
#include <QString>
|
||||
|
||||
@@ -14,8 +13,7 @@ using MessagePtr = std::shared_ptr<const Message>;
|
||||
|
||||
class LoggingChannel
|
||||
{
|
||||
explicit LoggingChannel(const QString &_channelName,
|
||||
const QString &platform);
|
||||
explicit LoggingChannel(QString _channelName, QString _platform);
|
||||
|
||||
public:
|
||||
~LoggingChannel();
|
||||
@@ -26,19 +24,11 @@ public:
|
||||
LoggingChannel(LoggingChannel &&) = delete;
|
||||
LoggingChannel &operator=(LoggingChannel &&) = delete;
|
||||
|
||||
void addMessage(MessagePtr message);
|
||||
void addMessage(const MessagePtr &message, const QString &streamID);
|
||||
|
||||
private:
|
||||
void openLogFile();
|
||||
|
||||
QString generateOpeningString(
|
||||
const QDateTime &now = QDateTime::currentDateTime()) const;
|
||||
QString generateClosingString(
|
||||
const QDateTime &now = QDateTime::currentDateTime()) const;
|
||||
|
||||
void appendLine(const QString &line);
|
||||
|
||||
QString generateDateString(const QDateTime &now);
|
||||
void openStreamLogFile(const QString &streamID);
|
||||
|
||||
const QString channelName;
|
||||
const QString platform;
|
||||
@@ -46,6 +36,8 @@ private:
|
||||
QString subDirectory;
|
||||
|
||||
QFile fileHandle;
|
||||
QFile currentStreamFileHandle;
|
||||
QString currentStreamID;
|
||||
|
||||
QString dateString;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user