forked from PCSX2/pcsx2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogWindow.cpp
391 lines (323 loc) · 10.8 KB
/
LogWindow.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team
// SPDX-License-Identifier: GPL-3.0+
#include "LogWindow.h"
#include "MainWindow.h"
#include "QtHost.h"
#include "SettingWidgetBinder.h"
#include <QtCore/QLatin1StringView>
#include <QtCore/QUtf8StringView>
#include <QtGui/QIcon>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QScrollBar>
#include <mutex>
// Need a lock so the other threads don't try to write to a deleting window.
LogWindow* g_log_window;
static std::mutex s_log_mutex;
static LOGLEVEL GetWindowLogLevel()
{
#ifdef _DEBUG
return LOGLEVEL_DEBUG;
#else
return (IsDevBuild || Host::GetBaseBoolSettingValue("Logging", "EnableVerbose", false)) ? LOGLEVEL_DEV : LOGLEVEL_INFO;
#endif
}
LogWindow::LogWindow(bool attach_to_main)
: QMainWindow()
, m_attached_to_main_window(attach_to_main)
{
restoreSize();
createUi();
Log::SetHostOutputLevel(GetWindowLogLevel(), &LogWindow::logCallback);
}
LogWindow::~LogWindow() = default;
void LogWindow::updateSettings()
{
std::unique_lock lock(s_log_mutex);
const bool new_enabled = Host::GetBaseBoolSettingValue("Logging", "EnableLogWindow", false) && !QtHost::InNoGUIMode();
const bool attach_to_main = Host::GetBaseBoolSettingValue("Logging", "AttachLogWindowToMainWindow", true);
const bool curr_enabled = Log::IsHostOutputEnabled();
if (new_enabled == curr_enabled)
{
if (g_log_window && g_log_window->m_attached_to_main_window != attach_to_main)
{
g_log_window->m_attached_to_main_window = attach_to_main;
if (attach_to_main)
g_log_window->reattachToMainWindow();
}
// Update level.
if (new_enabled)
Log::SetHostOutputLevel(GetWindowLogLevel(), &LogWindow::logCallback);
return;
}
if (new_enabled)
{
g_log_window = new LogWindow(attach_to_main);
if (attach_to_main && g_main_window && g_main_window->isVisible())
g_log_window->reattachToMainWindow();
g_log_window->show();
}
else if (g_log_window)
{
g_log_window->m_destroying = true;
g_log_window->close();
g_log_window->deleteLater();
g_log_window = nullptr;
}
}
void LogWindow::destroy()
{
std::unique_lock lock(s_log_mutex);
if (!g_log_window)
return;
g_log_window->m_destroying = true;
g_log_window->close();
g_log_window->deleteLater();
g_log_window = nullptr;
}
void LogWindow::reattachToMainWindow()
{
// Skip when maximized.
if (g_main_window->windowState() & (Qt::WindowMaximized | Qt::WindowFullScreen))
return;
const QPoint new_pos = g_main_window->pos() + QPoint(g_main_window->width() + 10, 0);
if (pos() != new_pos)
move(new_pos);
}
void LogWindow::updateWindowTitle()
{
QString title;
const QString& serial = QtHost::GetCurrentGameSerial();
if (QtHost::IsVMValid() && !serial.isEmpty())
{
const QFileInfo fi(QtHost::GetCurrentGamePath());
title = tr("Log Window - %1 [%2]").arg(serial).arg(fi.fileName());
}
else
{
title = tr("Log Window");
}
setWindowTitle(title);
}
void LogWindow::createUi()
{
setWindowIcon(QtHost::GetAppIcon());
setWindowFlag(Qt::WindowCloseButtonHint, false);
updateWindowTitle();
QAction* action;
QMenuBar* menu = new QMenuBar(this);
setMenuBar(menu);
QMenu* log_menu = menu->addMenu("&Log");
action = log_menu->addAction(tr("&Clear"));
connect(action, &QAction::triggered, this, &LogWindow::onClearTriggered);
action = log_menu->addAction(tr("&Save..."));
connect(action, &QAction::triggered, this, &LogWindow::onSaveTriggered);
log_menu->addSeparator();
action = log_menu->addAction(tr("Cl&ose"));
connect(action, &QAction::triggered, this, &LogWindow::close);
QMenu* settings_menu = menu->addMenu(tr("&Settings"));
#if 0
// TODO: These are duplicated with the main window...
action = settings_menu->addAction(tr("Log To &System Console"));
action->setCheckable(true);
SettingWidgetBinder::BindWidgetToBoolSetting(nullptr, action, "Logging", "EnableSystemConsole", false);
action = settings_menu->addAction(tr("Log To &Debug Console"));
action->setCheckable(true);
SettingWidgetBinder::BindWidgetToBoolSetting(nullptr, action, "Logging", "EnableDebugConsole", false);
action = settings_menu->addAction(tr("Log To &File"));
action->setCheckable(true);
SettingWidgetBinder::BindWidgetToBoolSetting(nullptr, action, "Logging", "EnableFileLogging", false);
settings_menu->addSeparator();
#endif
action = settings_menu->addAction(tr("Attach To &Main Window"));
action->setCheckable(true);
SettingWidgetBinder::BindWidgetToBoolSetting(nullptr, action, "Logging", "AttachLogWindowToMainWindow", true);
action = settings_menu->addAction(tr("Show &Timestamps"));
action->setCheckable(true);
SettingWidgetBinder::BindWidgetToBoolSetting(nullptr, action, "Logging", "EnableTimestamps", true);
settings_menu->addSeparator();
// TODO: Log Level
m_text = new QPlainTextEdit(this);
m_text->setReadOnly(true);
m_text->setUndoRedoEnabled(false);
m_text->setTextInteractionFlags(Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse);
m_text->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
m_text->setWordWrapMode(QTextOption::WrapAnywhere);
#if defined(_WIN32)
QFont font("Consolas");
font.setPointSize(10);
#elif defined(__APPLE__)
QFont font("Monaco");
font.setPointSize(11);
#else
QFont font("Monospace");
font.setStyleHint(QFont::TypeWriter);
#endif
m_text->setFont(font);
setCentralWidget(m_text);
}
void LogWindow::onClearTriggered()
{
m_text->clear();
}
void LogWindow::onSaveTriggered()
{
const QString path = QFileDialog::getSaveFileName(this, tr("Select Log File"), QString(), tr("Log Files (*.txt)"));
if (path.isEmpty())
return;
QFile file(path);
if (!file.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::critical(this, tr("Error"), tr("Failed to open file for writing."));
return;
}
file.write(m_text->toPlainText().toUtf8());
file.close();
appendMessage(LOGLEVEL_INFO, Color_Default, tr("Log was written to %1.\n").arg(path));
}
void LogWindow::logCallback(LOGLEVEL level, ConsoleColors color, std::string_view message)
{
std::unique_lock lock(s_log_mutex);
if (!g_log_window)
return;
// I don't like the memory allocations here either...
QString qmessage;
qmessage.reserve(message.length() + 1);
qmessage.append(QUtf8StringView(message.data(), message.length()));
qmessage.append(QChar('\n'));
if (g_emu_thread->isOnUIThread())
{
g_log_window->appendMessage(static_cast<u32>(level), static_cast<u32>(color), qmessage);
}
else
{
QMetaObject::invokeMethod(g_log_window, "appendMessage", Qt::QueuedConnection,
Q_ARG(quint32, static_cast<u32>(level)), Q_ARG(quint32, static_cast<u32>(color)),
Q_ARG(const QString&, qmessage));
}
}
void LogWindow::closeEvent(QCloseEvent* event)
{
if (!m_destroying)
{
event->ignore();
return;
}
Log::SetHostOutputLevel(LOGLEVEL_NONE, nullptr);
saveSize();
QMainWindow::closeEvent(event);
}
void LogWindow::appendMessage(quint32 level, quint32 color, const QString& message)
{
QTextCursor temp_cursor = m_text->textCursor();
QScrollBar* scrollbar = m_text->verticalScrollBar();
const bool cursor_at_end = temp_cursor.atEnd();
const bool scroll_at_end = scrollbar->sliderPosition() == scrollbar->maximum();
temp_cursor.movePosition(QTextCursor::End);
{
static constexpr const QColor qcolors[2][ConsoleColors_Count] = {
// Light theme
{
QColor(0, 0, 0), // Color_Default
QColor(0, 0, 0), // Color_Black
QColor(128, 0, 0), // Color_Red
QColor(0, 128, 0), // Color_Green
QColor(0, 0, 128), // Color_Blue
QColor(160, 0, 160), // Color_Magenta
QColor(160, 120, 0), // Color_Orange
QColor(108, 108, 108), // Color_Gray
QColor(128, 180, 180), // Color_Cyan
QColor(180, 180, 128), // Color_Yellow
QColor(160, 160, 160), // Color_White
QColor(0, 0, 0), // Color_StrongBlack
QColor(128, 0, 0), // Color_StrongRed
QColor(0, 128, 0), // Color_StrongGreen
QColor(0, 0, 128), // Color_StrongBlue
QColor(160, 0, 160), // Color_StrongMagenta
QColor(160, 120, 0), // Color_StrongOrange
QColor(108, 108, 108), // Color_StrongGray
QColor(128, 180, 180), // Color_StrongCyan
QColor(180, 180, 128), // Color_StrongYellow
QColor(160, 160, 160), // Color_StrongWhite
},
// Dark theme
{
QColor(208, 208, 208), // Color_Default
QColor(255, 255, 255), // Color_Black
QColor(180, 0, 0), // Color_Red
QColor(0, 160, 0), // Color_Green
QColor(32, 32, 204), // Color_Blue
QColor(160, 0, 160), // Color_Magenta
QColor(160, 120, 0), // Color_Orange
QColor(128, 128, 128), // Color_Gray
QColor(128, 180, 180), // Color_Cyan
QColor(180, 180, 128), // Color_Yellow
QColor(160, 160, 160), // Color_White
QColor(255, 255, 255), // Color_StrongBlack
QColor(180, 0, 0), // Color_StrongRed
QColor(0, 160, 0), // Color_StrongGreen
QColor(32, 32, 204), // Color_StrongBlue
QColor(160, 0, 160), // Color_StrongMagenta
QColor(160, 120, 0), // Color_StrongOrange
QColor(128, 128, 128), // Color_StrongGray
QColor(128, 180, 180), // Color_StrongCyan
QColor(180, 180, 128), // Color_StrongYellow
QColor(160, 160, 160), // Color_StrongWhite
},
};
static constexpr const QColor timestamp_color = QColor(0xcc, 0xcc, 0xcc);
QTextCharFormat format = temp_cursor.charFormat();
if (Log::AreTimestampsEnabled())
{
const float message_time = Log::GetCurrentMessageTime();
const QString qtimestamp = QStringLiteral("[%1] ").arg(message_time, 10, 'f', 4);
format.setForeground(QBrush(timestamp_color));
temp_cursor.setCharFormat(format);
temp_cursor.insertText(qtimestamp);
}
const bool dark = static_cast<u32>(QtHost::IsDarkApplicationTheme());
// message has \n already
format.setForeground(QBrush(qcolors[static_cast<u32>(dark)][color]));
temp_cursor.setCharFormat(format);
temp_cursor.insertText(message);
}
if (cursor_at_end)
{
if (scroll_at_end)
{
m_text->setTextCursor(temp_cursor);
scrollbar->setSliderPosition(scrollbar->maximum());
}
else
{
// Can't let changing the cursor affect the scroll bar...
const int pos = scrollbar->sliderPosition();
m_text->setTextCursor(temp_cursor);
scrollbar->setSliderPosition(pos);
}
}
}
void LogWindow::saveSize()
{
const int current_width = Host::GetBaseIntSettingValue("UI", "LogWindowWidth", DEFAULT_WIDTH);
const int current_height = Host::GetBaseIntSettingValue("UI", "LogWindowHeight", DEFAULT_HEIGHT);
const QSize wsize = size();
bool changed = false;
if (current_width != wsize.width())
{
Host::SetBaseIntSettingValue("UI", "LogWindowWidth", wsize.width());
changed = true;
}
if (current_height != wsize.height())
{
Host::SetBaseIntSettingValue("UI", "LogWindowHeight", wsize.height());
changed = true;
}
if (changed)
Host::CommitBaseSettingChanges();
}
void LogWindow::restoreSize()
{
const int width = Host::GetBaseIntSettingValue("UI", "LogWindowWidth", DEFAULT_WIDTH);
const int height = Host::GetBaseIntSettingValue("UI", "LogWindowHeight", DEFAULT_HEIGHT);
resize(width, height);
}