2017-08-27 22:10:06 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-08-27 22:10:06 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/LogWidget.h"
|
2017-08-27 22:10:06 +00:00
|
|
|
|
|
|
|
#include <QCheckBox>
|
|
|
|
#include <QComboBox>
|
|
|
|
#include <QFont>
|
|
|
|
#include <QFontDatabase>
|
2019-08-30 21:55:40 +00:00
|
|
|
#include <QGridLayout>
|
|
|
|
#include <QPlainTextEdit>
|
2017-08-27 22:10:06 +00:00
|
|
|
#include <QPushButton>
|
|
|
|
#include <QTimer>
|
2019-08-30 21:55:40 +00:00
|
|
|
|
|
|
|
#include "Core/ConfigManager.h"
|
2017-08-27 22:10:06 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Settings.h"
|
2017-08-27 22:10:06 +00:00
|
|
|
|
|
|
|
// Delay in ms between calls of UpdateLog()
|
|
|
|
constexpr int UPDATE_LOG_DELAY = 100;
|
|
|
|
// Maximum lines to process at a time
|
2019-08-30 21:55:40 +00:00
|
|
|
constexpr size_t MAX_LOG_LINES_TO_UPDATE = 200;
|
2017-08-27 22:10:06 +00:00
|
|
|
// Timestamp length
|
2019-08-30 22:15:03 +00:00
|
|
|
constexpr size_t TIMESTAMP_LENGTH = 10;
|
|
|
|
|
|
|
|
// A helper function to construct QString from std::string_view in one line
|
|
|
|
static QString QStringFromStringView(std::string_view str)
|
|
|
|
{
|
|
|
|
return QString::fromUtf8(str.data(), static_cast<int>(str.size()));
|
|
|
|
}
|
2017-08-27 22:10:06 +00:00
|
|
|
|
|
|
|
LogWidget::LogWidget(QWidget* parent) : QDockWidget(parent), m_timer(new QTimer(this))
|
|
|
|
{
|
|
|
|
setWindowTitle(tr("Log"));
|
2018-04-19 09:32:00 +00:00
|
|
|
setObjectName(QStringLiteral("log"));
|
|
|
|
|
2017-08-27 22:10:06 +00:00
|
|
|
setHidden(!Settings::Instance().IsLogVisible());
|
|
|
|
setAllowedAreas(Qt::AllDockWidgetAreas);
|
|
|
|
|
|
|
|
CreateWidgets();
|
|
|
|
LoadSettings();
|
|
|
|
|
|
|
|
ConnectWidgets();
|
|
|
|
|
|
|
|
connect(m_timer, &QTimer::timeout, this, &LogWidget::UpdateLog);
|
2019-08-30 22:09:47 +00:00
|
|
|
connect(this, &QDockWidget::visibilityChanged, [this](bool visible) {
|
|
|
|
if (visible)
|
|
|
|
m_timer->start(UPDATE_LOG_DELAY);
|
|
|
|
else
|
|
|
|
m_timer->stop();
|
|
|
|
});
|
2017-08-27 22:10:06 +00:00
|
|
|
|
2018-03-29 01:01:25 +00:00
|
|
|
connect(&Settings::Instance(), &Settings::DebugFontChanged, this, &LogWidget::UpdateFont);
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
Common::Log::LogManager::GetInstance()->RegisterListener(LogListener::LOG_WINDOW_LISTENER, this);
|
2017-08-27 22:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LogWidget::~LogWidget()
|
|
|
|
{
|
|
|
|
SaveSettings();
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
Common::Log::LogManager::GetInstance()->RegisterListener(LogListener::LOG_WINDOW_LISTENER,
|
|
|
|
nullptr);
|
2017-08-27 22:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogWidget::UpdateLog()
|
|
|
|
{
|
2019-08-30 22:15:03 +00:00
|
|
|
std::vector<LogEntry> elements_to_push;
|
|
|
|
{
|
|
|
|
std::lock_guard lock(m_log_mutex);
|
2019-08-31 18:27:15 +00:00
|
|
|
if (m_log_ring_buffer.empty())
|
2019-08-30 22:15:03 +00:00
|
|
|
return;
|
2017-08-27 22:10:06 +00:00
|
|
|
|
2019-08-31 18:27:15 +00:00
|
|
|
elements_to_push.reserve(std::min(MAX_LOG_LINES_TO_UPDATE, m_log_ring_buffer.size()));
|
2017-08-27 22:10:06 +00:00
|
|
|
|
2019-08-31 18:27:15 +00:00
|
|
|
for (size_t i = 0; !m_log_ring_buffer.empty() && i < MAX_LOG_LINES_TO_UPDATE; i++)
|
2020-03-23 08:31:01 +00:00
|
|
|
elements_to_push.push_back(m_log_ring_buffer.pop_front());
|
2019-08-30 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& line : elements_to_push)
|
2017-08-27 22:10:06 +00:00
|
|
|
{
|
2019-08-30 22:15:03 +00:00
|
|
|
const char* color = "white";
|
2019-11-28 09:19:24 +00:00
|
|
|
switch (std::get<Common::Log::LOG_LEVELS>(line))
|
2019-08-30 22:15:03 +00:00
|
|
|
{
|
2019-11-28 09:19:24 +00:00
|
|
|
case Common::Log::LOG_LEVELS::LERROR:
|
2019-08-30 22:15:03 +00:00
|
|
|
color = "red";
|
|
|
|
break;
|
2019-11-28 09:19:24 +00:00
|
|
|
case Common::Log::LOG_LEVELS::LWARNING:
|
2019-08-30 22:15:03 +00:00
|
|
|
color = "yellow";
|
|
|
|
break;
|
2019-11-28 09:19:24 +00:00
|
|
|
case Common::Log::LOG_LEVELS::LNOTICE:
|
2019-08-30 22:15:03 +00:00
|
|
|
color = "lime";
|
|
|
|
break;
|
2019-11-28 09:19:24 +00:00
|
|
|
case Common::Log::LOG_LEVELS::LINFO:
|
2019-08-30 22:15:03 +00:00
|
|
|
color = "cyan";
|
|
|
|
break;
|
2019-11-28 09:19:24 +00:00
|
|
|
case Common::Log::LOG_LEVELS::LDEBUG:
|
2019-08-30 22:15:03 +00:00
|
|
|
color = "lightgrey";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string_view str_view(std::get<std::string>(line));
|
|
|
|
|
|
|
|
m_log_text->appendHtml(
|
|
|
|
QStringLiteral("%1 <span style=\"color: %2; white-space: pre\">%3</span>")
|
|
|
|
.arg(QStringFromStringView(str_view.substr(0, TIMESTAMP_LENGTH)),
|
|
|
|
QString::fromUtf8(color),
|
|
|
|
QStringFromStringView(str_view.substr(TIMESTAMP_LENGTH)).toHtmlEscaped()));
|
2017-08-27 22:10:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogWidget::UpdateFont()
|
|
|
|
{
|
|
|
|
QFont f;
|
|
|
|
|
|
|
|
switch (m_log_font->currentIndex())
|
|
|
|
{
|
|
|
|
case 0: // Default font
|
|
|
|
break;
|
|
|
|
case 1: // Monospace font
|
2020-09-15 08:33:30 +00:00
|
|
|
f = QFont(QFontDatabase::systemFont(QFontDatabase::FixedFont).family());
|
2017-08-27 22:10:06 +00:00
|
|
|
break;
|
2018-03-29 01:01:25 +00:00
|
|
|
case 2: // Debugger font
|
|
|
|
f = Settings::Instance().GetDebugFont();
|
|
|
|
break;
|
2017-08-27 22:10:06 +00:00
|
|
|
}
|
|
|
|
m_log_text->setFont(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogWidget::CreateWidgets()
|
|
|
|
{
|
|
|
|
// Log
|
2019-08-30 21:55:40 +00:00
|
|
|
m_log_text = new QPlainTextEdit;
|
2017-08-27 22:10:06 +00:00
|
|
|
m_log_wrap = new QCheckBox(tr("Word Wrap"));
|
|
|
|
m_log_font = new QComboBox;
|
|
|
|
m_log_clear = new QPushButton(tr("Clear"));
|
|
|
|
|
2018-03-29 01:01:25 +00:00
|
|
|
m_log_font->addItems({tr("Default Font"), tr("Monospaced Font"), tr("Selected Font")});
|
2017-08-27 22:10:06 +00:00
|
|
|
|
|
|
|
auto* log_layout = new QGridLayout;
|
|
|
|
log_layout->addWidget(m_log_wrap, 0, 0);
|
|
|
|
log_layout->addWidget(m_log_font, 0, 1);
|
|
|
|
log_layout->addWidget(m_log_clear, 0, 2);
|
|
|
|
log_layout->addWidget(m_log_text, 1, 0, 1, -1);
|
|
|
|
|
|
|
|
QWidget* widget = new QWidget;
|
|
|
|
widget->setLayout(log_layout);
|
|
|
|
|
|
|
|
setWidget(widget);
|
|
|
|
|
|
|
|
m_log_text->setReadOnly(true);
|
2019-08-30 21:55:40 +00:00
|
|
|
m_log_text->setUndoRedoEnabled(false);
|
|
|
|
m_log_text->setMaximumBlockCount(MAX_LOG_LINES);
|
2017-08-27 22:10:06 +00:00
|
|
|
|
|
|
|
QPalette palette = m_log_text->palette();
|
|
|
|
palette.setColor(QPalette::Base, Qt::black);
|
|
|
|
palette.setColor(QPalette::Text, Qt::white);
|
|
|
|
m_log_text->setPalette(palette);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogWidget::ConnectWidgets()
|
|
|
|
{
|
2019-08-30 22:05:01 +00:00
|
|
|
connect(m_log_clear, &QPushButton::clicked, [this] {
|
|
|
|
m_log_text->clear();
|
2019-08-31 18:27:15 +00:00
|
|
|
m_log_ring_buffer.clear();
|
2019-08-30 22:05:01 +00:00
|
|
|
});
|
2017-08-27 22:10:06 +00:00
|
|
|
connect(m_log_wrap, &QCheckBox::toggled, this, &LogWidget::SaveSettings);
|
2019-07-30 13:35:46 +00:00
|
|
|
connect(m_log_font, qOverload<int>(&QComboBox::currentIndexChanged), this,
|
2017-08-27 22:10:06 +00:00
|
|
|
&LogWidget::SaveSettings);
|
|
|
|
connect(this, &QDockWidget::topLevelChanged, this, &LogWidget::SaveSettings);
|
2019-08-30 22:05:01 +00:00
|
|
|
connect(&Settings::Instance(), &Settings::LogVisibilityChanged, this, &LogWidget::setVisible);
|
2017-08-27 22:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogWidget::LoadSettings()
|
|
|
|
{
|
2018-03-23 11:10:53 +00:00
|
|
|
auto& settings = Settings::GetQSettings();
|
2017-08-27 22:10:06 +00:00
|
|
|
|
|
|
|
restoreGeometry(settings.value(QStringLiteral("logwidget/geometry")).toByteArray());
|
|
|
|
setFloating(settings.value(QStringLiteral("logwidget/floating")).toBool());
|
|
|
|
|
|
|
|
// Log - Wrap Lines
|
|
|
|
m_log_wrap->setChecked(settings.value(QStringLiteral("logging/wraplines")).toBool());
|
2019-08-30 21:55:40 +00:00
|
|
|
m_log_text->setLineWrapMode(m_log_wrap->isChecked() ? QPlainTextEdit::WidgetWidth :
|
|
|
|
QPlainTextEdit::NoWrap);
|
2020-01-15 21:59:09 +00:00
|
|
|
m_log_text->setHorizontalScrollBarPolicy(m_log_wrap->isChecked() ? Qt::ScrollBarAsNeeded :
|
|
|
|
Qt::ScrollBarAlwaysOn);
|
2017-08-27 22:10:06 +00:00
|
|
|
|
|
|
|
// Log - Font Selection
|
|
|
|
// Currently "Debugger Font" is not supported as there is no Qt Debugger, defaulting to Monospace
|
|
|
|
m_log_font->setCurrentIndex(std::min(settings.value(QStringLiteral("logging/font")).toInt(), 1));
|
|
|
|
UpdateFont();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogWidget::SaveSettings()
|
|
|
|
{
|
2018-03-23 11:10:53 +00:00
|
|
|
auto& settings = Settings::GetQSettings();
|
2017-08-27 22:10:06 +00:00
|
|
|
|
|
|
|
settings.setValue(QStringLiteral("logwidget/geometry"), saveGeometry());
|
|
|
|
settings.setValue(QStringLiteral("logwidget/floating"), isFloating());
|
|
|
|
|
|
|
|
// Log - Wrap Lines
|
|
|
|
settings.setValue(QStringLiteral("logging/wraplines"), m_log_wrap->isChecked());
|
2019-08-30 21:55:40 +00:00
|
|
|
m_log_text->setLineWrapMode(m_log_wrap->isChecked() ? QPlainTextEdit::WidgetWidth :
|
|
|
|
QPlainTextEdit::NoWrap);
|
2020-01-15 21:59:09 +00:00
|
|
|
m_log_text->setHorizontalScrollBarPolicy(m_log_wrap->isChecked() ? Qt::ScrollBarAsNeeded :
|
|
|
|
Qt::ScrollBarAlwaysOn);
|
2017-08-27 22:10:06 +00:00
|
|
|
|
|
|
|
// Log - Font Selection
|
|
|
|
settings.setValue(QStringLiteral("logging/font"), m_log_font->currentIndex());
|
|
|
|
UpdateFont();
|
|
|
|
}
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
void LogWidget::Log(Common::Log::LOG_LEVELS level, const char* text)
|
2017-08-27 22:10:06 +00:00
|
|
|
{
|
2019-08-30 22:15:03 +00:00
|
|
|
size_t text_length = strlen(text);
|
|
|
|
while (text_length > 0 && text[text_length - 1] == '\n')
|
|
|
|
text_length--;
|
2019-02-08 12:21:21 +00:00
|
|
|
|
2019-08-30 22:15:03 +00:00
|
|
|
std::lock_guard lock(m_log_mutex);
|
2019-08-31 18:27:15 +00:00
|
|
|
m_log_ring_buffer.emplace(std::piecewise_construct, std::forward_as_tuple(text, text_length),
|
|
|
|
std::forward_as_tuple(level));
|
2017-08-27 22:10:06 +00:00
|
|
|
}
|
2017-09-20 11:18:36 +00:00
|
|
|
|
|
|
|
void LogWidget::closeEvent(QCloseEvent*)
|
|
|
|
{
|
|
|
|
Settings::Instance().SetLogVisible(false);
|
|
|
|
}
|