forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDebug.cpp
290 lines (263 loc) · 11.4 KB
/
TDebug.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/***************************************************************************
* Copyright (C) 2008-2009 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* Copyright (C) 2018, 2021-2022 by Stephen Lyons *
* - slysven@virginmedia.com *
* Copyright (C) 2021 by Vadim Peretokin - vperetokin@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "TDebug.h"
#include "TConsole.h"
#include "TTabBar.h"
#include "mudlet.h"
TDebug::TDebug(const QColor& c, const QColor& d)
: fgColor(c)
, bgColor(d)
{
}
// This is the method that pushes the accumulated text out to the Central Debug
// Console. Handles 'msg' beginning with 'csmContinue', otherwise if more than 1
// profile active, prepends msg with an indicator of the profile from which it
// came, which is deduced from the supplied Host pointer.
TDebug& TDebug::operator>>(Host* pHost)
{
if (Q_UNLIKELY(!mudlet::smpDebugConsole)) {
if (Q_LIKELY(!msg.isEmpty())) {
// Don't enqueue empty messages
auto tag = deduceProfileTag(msg, pHost);
TDebugMessage const newMessage(msg, tag, fgColor, bgColor);
smMessageQueue.enqueue(newMessage);
}
} else {
if (Q_UNLIKELY(!smMessageQueue.isEmpty())) {
// The smpDebugConsole must have just come on-line - so unload all
// the stacked up messages:
while (!smMessageQueue.isEmpty()) {
const auto& message = smMessageQueue.dequeue();
if (message.mTag.isNull()) {
mudlet::smpDebugConsole->print(message.mMessage, message.mForeground, message.mBackground);
} else {
mudlet::smpDebugConsole->print(message.mTag % message.mMessage, message.mForeground, message.mBackground);
}
}
}
auto tag = deduceProfileTag(msg, pHost);
if (tag.isNull()) {
// We use an empty message with no host pointer to flush out the
// enqueued messages the first time the CDC is shown - so in that
// case we will already done everything needed in previous chunk
// of code. Otherwise just print the message without a tag marking:
if (!msg.isEmpty()) {
mudlet::smpDebugConsole->print(msg, fgColor, bgColor);
}
} else if (tag == csmTagSystemMessage || Q_UNLIKELY(tag == csmTagFault) || TDebug::smIdentifierMap.count() > 1) {
// This is a system message or something went wrong in identifying the profile or more than one profile is active
mudlet::smpDebugConsole->print(tag % msg, fgColor, bgColor);
} else {
// Only one profile active - so don't print the tag:
mudlet::smpDebugConsole->print(msg, fgColor, bgColor);
}
}
return *this;
}
TDebug& TDebug::operator<<(const QString& t)
{
msg += t;
return *this;
}
TDebug& TDebug::operator<<(const QChar& t)
{
msg += t;
return *this;
}
TDebug& TDebug::operator<<(const int& t)
{
msg += QString::number(t);
return *this;
}
TDebug& TDebug::operator<<(const QMap<QString, QString>& map)
{
for (QMap<QString, QString>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it) {
msg += qsl("(%1, %2)").arg(it.key(), it.value());
}
msg += "), ";
return *this;
}
TDebug& TDebug::operator<<(const QMap<QString, int>& map)
{
for (QMap<QString, int>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it) {
msg += qsl("(%1, %2)").arg(it.key(), QString::number(it.value()));
}
msg += "), ";
return *this;
}
TDebug& TDebug::operator<<(const QMap<int, QString>& map)
{
for (QMap<int, QString>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it) {
msg += qsl("(%1, %2)").arg(QString::number(it.key()), it.value());
}
msg += "), ";
return *this;
}
TDebug& TDebug::operator<<(const QMap<int, int>& map)
{
for (QMap<int, int>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it) {
msg += qsl("(%1, %2)").arg(QString::number(it.key()), QString::number(it.value()));
}
msg += "), ";
return *this;
}
TDebug& TDebug::operator<<(const QList<QString>& list)
{
for (QList<QString>::const_iterator it = list.constBegin(); it != list.constEnd(); ++it) {
msg += qsl("%1, ").arg(*it);
}
msg += ")";
return *this;
}
TDebug& TDebug::operator<<(const QList<int>& list)
{
for (QList<int>::const_iterator it = list.constBegin(); it != list.constEnd(); ++it) {
msg += qsl("%1, ").arg(QString::number(*it));
}
msg += "), ";
return *this;
}
void TDebug::changeHostName(const Host* pHost, const QString& newName)
{
if (pHost) {
QPair<QString, QString>& pair = TDebug::smIdentifierMap[pHost];
pair.first = newName;
mudlet::self()->mpTabBar->applyPrefixToDisplayedText(newName, pair.second);
}
}
/* static */ void TDebug::addHost(Host* pHost, const QString hostName)
{
if (!initialised) {
smAvailableIdentifiers << qsl("[A] ") << qsl("[B] ") << qsl("[C] ") << qsl("[D] ") << qsl("[E] ")
<< qsl("[F] ") << qsl("[G] ") << qsl("[H] ") << qsl("[I] ") << qsl("[J] ")
<< qsl("[K] ") << qsl("[L] ") << qsl("[M] ") << qsl("[N] ") << qsl("[O] ")
<< qsl("[P] ") << qsl("[Q] ") << qsl("[R] ") << qsl("[S] ") << qsl("[T] ")
<< qsl("[U] ") << qsl("[V] ") << qsl("[W] ") << qsl("[X] ") << qsl("[Y] ")
<< qsl("[Z] ");
initialised = true;
}
if (!pHost) {
return;
}
QPair<QString, QString> newIdentifier;
if (TDebug::smAvailableIdentifiers.isEmpty()) {
// Run out of identifiers - use fall-back one:
newIdentifier = qMakePair(hostName, csmTagOverflow);
TDebug::smIdentifierMap.insert(pHost, newIdentifier);
} else {
newIdentifier = qMakePair(hostName, smAvailableIdentifiers.dequeue());
TDebug::smIdentifierMap.insert(pHost, newIdentifier);
}
TDebug localMessage(Qt::blue, Qt::white);
localMessage << qsl("Profile '%1' started.\n").arg(hostName) >> nullptr;
TDebug tableMessage(Qt::white, Qt::black);
tableMessage << TDebug::displayNewTable() >> nullptr;
if (mudlet::smDebugMode) {
// Can't use TTabBar::applyPrefixToDisplayedText(hostName, newIdentifier.second)
// here as the profile's tab has not been added to the tabbar yet.
// Instead arrange for all the tabs to be refreshed when we are next
// idle:
QTimer::singleShot(0, mudlet::self(), []() {
mudlet::self()->refreshTabBar();
});
}
}
/* static */ void TDebug::removeHost(Host* pHost, const QString hostName)
{
auto identifier = TDebug::smIdentifierMap.take(pHost);
// Check for the use of non-profile specific tags:
if (identifier.second != csmTagOverflow && identifier.second != csmTagSystemMessage && identifier.second != csmTagFault) {
// is a normal identifier so push it in at the back of the queue for reuse:
smAvailableIdentifiers.enqueue(identifier.second);
}
TDebug localMessage(Qt::darkGray, Qt::white);
localMessage << qsl("Profile '%1' ended.\n").arg(hostName) >> nullptr;
TDebug tableMessage(Qt::white, Qt::black);
tableMessage << TDebug::displayNewTable() >> nullptr;
}
/* static */ QString TDebug::displayNewTable()
{
if (TDebug::smIdentifierMap.count() <= 1) {
return QString();
}
// We do not translate this currently as the Central Debug Console does not
// get translated content - yet?
QStringList messageLines;
QMapIterator<const Host*, QPair<QString, QString>> itIdentifier(TDebug::smIdentifierMap);
while (itIdentifier.hasNext()) {
itIdentifier.next();
if (itIdentifier.key()) {
// Each identifier includes spaces, so no need to include one before
// the '=' sign:
messageLines.append(qsl(" %1= \"%2\"").arg(itIdentifier.value().second, itIdentifier.value().first));
}
}
if (messageLines.count() > 1) {
std::sort(messageLines.begin(), messageLines.end());
}
messageLines.prepend(qsl(" %1= System message, not belonging to a specific profile").arg(csmTagSystemMessage));
// The line wrapping of these texts is a bit less than one might expect
// because the default size will clip the text otherwise, unless the
// user resizes the CDC:
messageLines.prepend(qsl("%1 profiles active now. Each message from a profile \n"
"will be prefixed as follows:")
.arg(TDebug::smIdentifierMap.count()));
return messageLines.join(QChar::LineFeed).append(QChar::LineFeed);
}
/* static */ void TDebug::flushMessageQueue()
{
TDebug localMessage(Qt::black, Qt::white);
localMessage << QString() >> nullptr;
}
// This will strip a TDebug::csmContinue QChar if present from the start of text:
/* static */ QString TDebug::deduceProfileTag(QString& text, Host* pHost)
{
if (text.startsWith(csmContinue)) {
text.remove(0, 1);
}
if (pHost) {
if (!smIdentifierMap.contains(pHost)) {
// Oops, we do not have that Host on file, better create something
// for it - this will also cause a pair of new TDebug messages to
// be created and processed prior to the call to this method being
// completed:
addHost(pHost, pHost->getName());
}
// By now smIdentifierMap WILL contain something for pHost - but use an
// the "fault" mark (a bang/exclaimation point) if something is really
// screwy and it does not, as it happens we have a method that will do
// that already:
return getTag(pHost);
}
// Must be a system message - or the dummy one to flush the queue.
if (!text.isEmpty()) {
// A system message:
return csmTagSystemMessage;
}
// The dummy one:
return QString();
}
/* static */ QString TDebug::getTag(Host* pHost)
{
return smIdentifierMap.value(pHost, qMakePair(QString(), csmTagFault)).second;
}