-
-
Notifications
You must be signed in to change notification settings - Fork 455
/
LoggingChannel.cpp
246 lines (202 loc) · 6.59 KB
/
LoggingChannel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "singletons/helper/LoggingChannel.hpp"
#include "Application.hpp"
#include "common/QLogging.hpp"
#include "messages/Message.hpp"
#include "messages/MessageThread.hpp"
#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 {
LoggingChannel::LoggingChannel(QString _channelName, QString _platform)
: channelName(std::move(_channelName))
, platform(std::move(_platform))
{
if (this->channelName.startsWith("/whispers"))
{
this->subDirectory = "Whispers";
}
else if (channelName.startsWith("/mentions"))
{
this->subDirectory = "Mentions";
}
else if (channelName.startsWith("/live"))
{
this->subDirectory = "Live";
}
else if (channelName.startsWith("/automod"))
{
this->subDirectory = "AutoMod";
}
else
{
this->subDirectory =
QStringLiteral("Channels") + QDir::separator() + channelName;
}
// enforce capitalized platform names
this->subDirectory = platform[0].toUpper() + platform.mid(1).toLower() +
QDir::separator() + this->subDirectory;
getSettings()->logPath.connect([this](const QString &logPath, auto) {
this->baseDirectory = logPath.isEmpty()
? getApp()->getPaths().messageLogDirectory
: logPath;
this->openLogFile();
});
}
LoggingChannel::~LoggingChannel()
{
appendLine(this->fileHandle, generateClosingString());
this->fileHandle.close();
this->currentStreamFileHandle.close();
}
void LoggingChannel::openLogFile()
{
QDateTime now = QDateTime::currentDateTime();
this->dateString = generateDateString(now);
if (this->fileHandle.isOpen())
{
this->fileHandle.flush();
this->fileHandle.close();
}
QString baseFileName = this->channelName + "-" + this->dateString + ".log";
QString directory =
this->baseDirectory + QDir::separator() + this->subDirectory;
if (!QDir().mkpath(directory))
{
qCDebug(chatterinoHelper) << "Unable to create logging path";
return;
}
// Open file handle to log file of current date
QString fileName = directory + QDir::separator() + baseFileName;
qCDebug(chatterinoHelper) << "Logging to" << fileName;
this->fileHandle.setFileName(fileName);
this->fileHandle.open(QIODevice::Append);
appendLine(this->fileHandle, generateOpeningString(now));
}
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 = generateDateString(now);
if (messageDateString != this->dateString)
{
this->dateString = messageDateString;
this->openLogFile();
}
QString str;
if (channelName.startsWith("/mentions") ||
channelName.startsWith("/automod"))
{
str.append("#" + message->channelName + " ");
}
str.append('[');
str.append(now.toString("HH:mm:ss"));
str.append("] ");
QString messageText;
if (message->loginName.isEmpty())
{
// This accounts for any messages not explicitly sent by a user, like
// system messages, parts of announcements, subs etc.
messageText = message->messageText;
}
else
{
if (message->localizedName.isEmpty())
{
messageText = message->loginName + ": " + message->messageText;
}
else
{
messageText = message->localizedName + " " + message->loginName +
": " + message->messageText;
}
}
if ((message->flags.has(MessageFlag::ReplyMessage) &&
getSettings()->stripReplyMention) &&
!getSettings()->hideReplyContext)
{
qsizetype colonIndex = messageText.indexOf(':');
if (colonIndex != -1)
{
QString rootMessageChatter;
if (message->replyParent)
{
rootMessageChatter = message->replyParent->loginName;
}
else
{
// we actually want to use 'reply-parent-user-login' tag here,
// but it's not worth storing just for this edge case
rootMessageChatter = message->replyThread->root()->loginName;
}
messageText.insert(colonIndex + 1, " @" + rootMessageChatter);
}
}
str.append(messageText);
str.append(ENDLINE);
appendLine(this->fileHandle, str);
if (!streamID.isEmpty() && getSettings()->separatelyStoreStreamLogs)
{
if (this->currentStreamID != streamID)
{
this->openStreamLogFile(streamID);
}
appendLine(this->currentStreamFileHandle, str);
}
}
} // namespace chatterino