| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/LogWidget.h" |
| 5 | |
| 6 | #include <QCheckBox> |
| 7 | #include <QComboBox> |
| 8 | #include <QFont> |
| 9 | #include <QFontDatabase> |
| 10 | #include <QGridLayout> |
| 11 | #include <QPlainTextEdit> |
| 12 | #include <QPushButton> |
| 13 | #include <QTimer> |
| 14 | |
| 15 | #include "DolphinQt/Settings.h" |
| 16 | |
| 17 | // Delay in ms between calls of UpdateLog() |
| 18 | constexpr int UPDATE_LOG_DELAY = 100; |
| 19 | // Maximum lines to process at a time |
| 20 | constexpr size_t MAX_LOG_LINES_TO_UPDATE = 200; |
| 21 | // Timestamp length |
| 22 | constexpr size_t TIMESTAMP_LENGTH = 10; |
| 23 | |
| 24 | // A helper function to construct QString from std::string_view in one line |
| 25 | static QString QStringFromStringView(std::string_view str) |
| 26 | { |
| 27 | return QString::fromUtf8(str.data(), static_cast<int>(str.size())); |
| 28 | } |
| 29 | |
| 30 | LogWidget::LogWidget(QWidget* parent) : QDockWidget(parent), m_timer(new QTimer(this)) |
| 31 | { |
| 32 | setWindowTitle(tr("Log")); |
| 33 | setObjectName(QStringLiteral("log")); |
| 34 | |
| 35 | setHidden(!Settings::Instance().IsLogVisible()); |
| 36 | setAllowedAreas(Qt::AllDockWidgetAreas); |
| 37 | |
| 38 | CreateWidgets(); |
| 39 | LoadSettings(); |
| 40 | |
| 41 | ConnectWidgets(); |
| 42 | |
| 43 | connect(m_timer, &QTimer::timeout, this, &LogWidget::UpdateLog); |
| 44 | connect(this, &QDockWidget::visibilityChanged, [this](bool visible) { |
| 45 | if (visible) |
| 46 | m_timer->start(UPDATE_LOG_DELAY); |
| 47 | else |
| 48 | m_timer->stop(); |
| 49 | }); |
| 50 | |
| 51 | connect(&Settings::Instance(), &Settings::DebugFontChanged, this, &LogWidget::UpdateFont); |
| 52 | |
| 53 | Common::Log::LogManager::GetInstance()->RegisterListener( |
| 54 | Common::Log::LogListener::LOG_WINDOW_LISTENER, std::make_unique<LogListenerImpl>(this)); |
| 55 | } |
| 56 | |
| 57 | LogWidget::~LogWidget() |
| 58 | { |
| 59 | SaveSettings(); |
| 60 | |
| 61 | Common::Log::LogManager::GetInstance()->RegisterListener( |
| 62 | Common::Log::LogListener::LOG_WINDOW_LISTENER, nullptr); |
| 63 | } |
| 64 | |
| 65 | void LogWidget::UpdateLog() |
| 66 | { |
| 67 | std::vector<LogEntry> elements_to_push; |
| 68 | { |
| 69 | std::lock_guard lock(m_log_mutex); |
| 70 | if (m_log_ring_buffer.empty()) |
| 71 | return; |
| 72 | |
| 73 | elements_to_push.reserve(std::min(MAX_LOG_LINES_TO_UPDATE, m_log_ring_buffer.size())); |
| 74 | |
| 75 | for (size_t i = 0; !m_log_ring_buffer.empty() && i < MAX_LOG_LINES_TO_UPDATE; i++) |
| 76 | elements_to_push.push_back(m_log_ring_buffer.pop_front()); |
| 77 | } |
| 78 | |
| 79 | for (auto& line : elements_to_push) |
| 80 | { |
| 81 | const char* color = "white"; |
| 82 | switch (std::get<Common::Log::LogLevel>(line)) |
| 83 | { |
| 84 | case Common::Log::LogLevel::LERROR: |
| 85 | color = "red"; |
| 86 | break; |
| 87 | case Common::Log::LogLevel::LWARNING: |
| 88 | color = "yellow"; |
| 89 | break; |
| 90 | case Common::Log::LogLevel::LNOTICE: |
| 91 | color = "lime"; |
| 92 | break; |
| 93 | case Common::Log::LogLevel::LINFO: |
| 94 | color = "cyan"; |
| 95 | break; |
| 96 | case Common::Log::LogLevel::LDEBUG: |
| 97 | color = "lightgrey"; |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | const std::string_view str_view(std::get<std::string>(line)); |
| 102 | |
| 103 | m_log_text->appendHtml( |
| 104 | QStringLiteral("%1<span style=\"color: %2; white-space: pre\">%3</span>") |
| 105 | .arg(QStringFromStringView(str_view.substr(0, TIMESTAMP_LENGTH)), |
| 106 | QString::fromUtf8(color), |
| 107 | QStringFromStringView(str_view.substr(TIMESTAMP_LENGTH)).toHtmlEscaped())); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void LogWidget::UpdateFont() |
| 112 | { |
| 113 | QFont f; |
| 114 | |
| 115 | switch (m_log_font->currentIndex()) |
| 116 | { |
| 117 | case 0: // Default font |
| 118 | break; |
| 119 | case 1: // Monospace font |
| 120 | f = QFont(QFontDatabase::systemFont(QFontDatabase::FixedFont).family()); |
| 121 | break; |
| 122 | case 2: // Debugger font |
| 123 | f = Settings::Instance().GetDebugFont(); |
| 124 | break; |
| 125 | } |
| 126 | m_log_text->setFont(f); |
| 127 | } |
| 128 | |
| 129 | void LogWidget::CreateWidgets() |
| 130 | { |
| 131 | // Log |
| 132 | m_log_text = new QPlainTextEdit; |
| 133 | m_log_wrap = new QCheckBox(tr("Word Wrap")); |
| 134 | m_log_font = new QComboBox; |
| 135 | m_log_clear = new QPushButton(tr("Clear")); |
| 136 | |
| 137 | m_log_font->addItems({tr("Default Font"), tr("Monospaced Font"), tr("Selected Font")}); |
| 138 | |
| 139 | auto* log_layout = new QGridLayout; |
| 140 | log_layout->addWidget(m_log_wrap, 0, 0); |
| 141 | log_layout->addWidget(m_log_font, 0, 1); |
| 142 | log_layout->addWidget(m_log_clear, 0, 2); |
| 143 | log_layout->addWidget(m_log_text, 1, 0, 1, -1); |
| 144 | |
| 145 | QWidget* widget = new QWidget; |
| 146 | widget->setLayout(log_layout); |
| 147 | |
| 148 | setWidget(widget); |
| 149 | |
| 150 | m_log_text->setReadOnly(true); |
| 151 | m_log_text->setUndoRedoEnabled(false); |
| 152 | m_log_text->setMaximumBlockCount(MAX_LOG_LINES); |
| 153 | |
| 154 | m_log_text->setStyleSheet( |
| 155 | QStringLiteral("QPlainTextEdit { background-color: black; color: white; }")); |
| 156 | } |
| 157 | |
| 158 | void LogWidget::ConnectWidgets() |
| 159 | { |
| 160 | connect(m_log_clear, &QPushButton::clicked, [this] { |
| 161 | m_log_text->clear(); |
| 162 | m_log_ring_buffer.clear(); |
| 163 | }); |
| 164 | connect(m_log_wrap, &QCheckBox::toggled, this, &LogWidget::SaveSettings); |
| 165 | connect(m_log_font, &QComboBox::currentIndexChanged, this, &LogWidget::SaveSettings); |
| 166 | connect(this, &QDockWidget::topLevelChanged, this, &LogWidget::SaveSettings); |
| 167 | connect(&Settings::Instance(), &Settings::LogVisibilityChanged, this, &LogWidget::setVisible); |
| 168 | } |
| 169 | |
| 170 | void LogWidget::LoadSettings() |
| 171 | { |
| 172 | auto& settings = Settings::GetQSettings(); |
| 173 | |
| 174 | restoreGeometry(settings.value(QStringLiteral("logwidget/geometry")).toByteArray()); |
| 175 | setFloating(settings.value(QStringLiteral("logwidget/floating")).toBool()); |
| 176 | |
| 177 | // Log - Wrap Lines |
| 178 | m_log_wrap->setChecked(settings.value(QStringLiteral("logging/wraplines")).toBool()); |
| 179 | m_log_text->setLineWrapMode(m_log_wrap->isChecked() ? QPlainTextEdit::WidgetWidth : |
| 180 | QPlainTextEdit::NoWrap); |
| 181 | m_log_text->setHorizontalScrollBarPolicy(m_log_wrap->isChecked() ? Qt::ScrollBarAsNeeded : |
| 182 | Qt::ScrollBarAlwaysOn); |
| 183 | |
| 184 | // Log - Font Selection |
| 185 | m_log_font->setCurrentIndex(settings.value(QStringLiteral("logging/font")).toInt()); |
| 186 | UpdateFont(); |
| 187 | } |
| 188 | |
| 189 | void LogWidget::SaveSettings() |
| 190 | { |
| 191 | auto& settings = Settings::GetQSettings(); |
| 192 | |
| 193 | settings.setValue(QStringLiteral("logwidget/geometry"), saveGeometry()); |
| 194 | settings.setValue(QStringLiteral("logwidget/floating"), isFloating()); |
| 195 | |
| 196 | // Log - Wrap Lines |
| 197 | settings.setValue(QStringLiteral("logging/wraplines"), m_log_wrap->isChecked()); |
| 198 | m_log_text->setLineWrapMode(m_log_wrap->isChecked() ? QPlainTextEdit::WidgetWidth : |
| 199 | QPlainTextEdit::NoWrap); |
| 200 | m_log_text->setHorizontalScrollBarPolicy(m_log_wrap->isChecked() ? Qt::ScrollBarAsNeeded : |
| 201 | Qt::ScrollBarAlwaysOn); |
| 202 | |
| 203 | // Log - Font Selection |
| 204 | settings.setValue(QStringLiteral("logging/font"), m_log_font->currentIndex()); |
| 205 | UpdateFont(); |
| 206 | } |
| 207 | |
| 208 | void LogWidget::Log(Common::Log::LogLevel level, const char* text) |
| 209 | { |
| 210 | size_t text_length = strlen(text); |
| 211 | while (text_length > 0 && text[text_length - 1] == '\n') |
| 212 | text_length--; |
| 213 | |
| 214 | std::lock_guard lock(m_log_mutex); |
| 215 | m_log_ring_buffer.emplace(std::piecewise_construct, std::forward_as_tuple(text, text_length), |
| 216 | std::forward_as_tuple(level)); |
| 217 | } |
| 218 | |
| 219 | void LogWidget::closeEvent(QCloseEvent*) |
| 220 | { |
| 221 | Settings::Instance().SetLogVisible(false); |
| 222 | } |