| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/LogConfigWidget.h" |
| 5 | |
| 6 | #include <array> |
| 7 | |
| 8 | #include <QCheckBox> |
| 9 | #include <QGroupBox> |
| 10 | #include <QListWidget> |
| 11 | #include <QPushButton> |
| 12 | #include <QRadioButton> |
| 13 | #include <QVBoxLayout> |
| 14 | |
| 15 | #include "Common/Logging/Log.h" |
| 16 | #include "Common/Logging/LogManager.h" |
| 17 | |
| 18 | #include "DolphinQt/Settings.h" |
| 19 | |
| 20 | LogConfigWidget::LogConfigWidget(QWidget* parent) : QDockWidget(parent) |
| 21 | { |
| 22 | setWindowTitle(tr("Log Configuration")); |
| 23 | setObjectName(QStringLiteral("logconfig")); |
| 24 | |
| 25 | setHidden(!Settings::Instance().IsLogConfigVisible()); |
| 26 | setAllowedAreas(Qt::AllDockWidgetAreas); |
| 27 | |
| 28 | CreateWidgets(); |
| 29 | LoadSettings(); |
| 30 | ConnectWidgets(); |
| 31 | } |
| 32 | |
| 33 | LogConfigWidget::~LogConfigWidget() |
| 34 | { |
| 35 | SaveSettings(); |
| 36 | } |
| 37 | |
| 38 | void LogConfigWidget::CreateWidgets() |
| 39 | { |
| 40 | auto* layout = new QVBoxLayout; |
| 41 | |
| 42 | auto* verbosity = new QGroupBox(tr("Verbosity")); |
| 43 | auto* verbosity_layout = new QVBoxLayout; |
| 44 | verbosity->setLayout(verbosity_layout); |
| 45 | m_verbosity_notice = new QRadioButton(tr("Notice")); |
| 46 | m_verbosity_error = new QRadioButton(tr("Error")); |
| 47 | m_verbosity_warning = new QRadioButton(tr("Warning")); |
| 48 | m_verbosity_info = new QRadioButton(tr("Info")); |
| 49 | m_verbosity_debug = new QRadioButton(tr("Debug")); |
| 50 | if constexpr (Common::Log::MAX_EFFECTIVE_LOGLEVEL < Common::Log::LogLevel::LDEBUG) |
| 51 | { |
| 52 | m_verbosity_debug->setVisible(false); |
| 53 | } |
| 54 | |
| 55 | auto* outputs = new QGroupBox(tr("Logger Outputs")); |
| 56 | auto* outputs_layout = new QVBoxLayout; |
| 57 | outputs->setLayout(outputs_layout); |
| 58 | m_out_file = new QCheckBox(tr("Write to File")); |
| 59 | m_out_console = new QCheckBox(tr("Write to Console")); |
| 60 | m_out_window = new QCheckBox(tr("Write to Window")); |
| 61 | |
| 62 | auto* types = new QGroupBox(tr("Log Types")); |
| 63 | auto* types_layout = new QVBoxLayout; |
| 64 | types->setLayout(types_layout); |
| 65 | m_types_toggle = new QPushButton(tr("Toggle All Log Types")); |
| 66 | m_types_list = new QListWidget; |
| 67 | |
| 68 | const auto* const log_manager = Common::Log::LogManager::GetInstance(); |
| 69 | for (int i = 0; i < static_cast<int>(Common::Log::LogType::NUMBER_OF_LOGS); i++) |
| 70 | { |
| 71 | const auto log_type = static_cast<Common::Log::LogType>(i); |
| 72 | const QString full_name = QString::fromUtf8(log_manager->GetFullName(log_type)); |
| 73 | const QString short_name = QString::fromUtf8(log_manager->GetShortName(log_type)); |
| 74 | auto* widget = new QListWidgetItem(QStringLiteral("%1 (%2)").arg(full_name, short_name)); |
| 75 | widget->setCheckState(Qt::Unchecked); |
| 76 | m_types_list->addItem(widget); |
| 77 | } |
| 78 | |
| 79 | layout->addWidget(verbosity); |
| 80 | verbosity_layout->addWidget(m_verbosity_notice); |
| 81 | verbosity_layout->addWidget(m_verbosity_error); |
| 82 | verbosity_layout->addWidget(m_verbosity_warning); |
| 83 | verbosity_layout->addWidget(m_verbosity_info); |
| 84 | verbosity_layout->addWidget(m_verbosity_debug); |
| 85 | |
| 86 | layout->addWidget(outputs); |
| 87 | outputs_layout->addWidget(m_out_file); |
| 88 | outputs_layout->addWidget(m_out_console); |
| 89 | outputs_layout->addWidget(m_out_window); |
| 90 | |
| 91 | layout->addWidget(types); |
| 92 | types_layout->addWidget(m_types_toggle); |
| 93 | types_layout->addWidget(m_types_list); |
| 94 | |
| 95 | QWidget* widget = new QWidget; |
| 96 | widget->setLayout(layout); |
| 97 | |
| 98 | setWidget(widget); |
| 99 | } |
| 100 | |
| 101 | void LogConfigWidget::ConnectWidgets() |
| 102 | { |
| 103 | using Common::Log::LogLevel; |
| 104 | using ButtonLevelPair = std::pair<QRadioButton*, LogLevel>; |
| 105 | const std::array button_level_pairs = {ButtonLevelPair{m_verbosity_notice, LogLevel::LNOTICE}, |
| 106 | ButtonLevelPair{m_verbosity_error, LogLevel::LERROR}, |
| 107 | ButtonLevelPair{m_verbosity_warning, LogLevel::LWARNING}, |
| 108 | ButtonLevelPair{m_verbosity_info, LogLevel::LINFO}, |
| 109 | ButtonLevelPair{m_verbosity_debug, LogLevel::LDEBUG}}; |
| 110 | |
| 111 | for (const auto& [button, level] : button_level_pairs) |
| 112 | { |
| 113 | connect(button, &QRadioButton::clicked, [level](const bool checked) { |
| 114 | if (checked) |
| 115 | { |
| 116 | auto* const log_manager = Common::Log::LogManager::GetInstance(); |
| 117 | log_manager->SetConfigLogLevel(level); |
| 118 | } |
| 119 | }); |
| 120 | } |
| 121 | const auto update_verbosity_boldness = [button_level_pairs]() { |
| 122 | auto* const log_manager = Common::Log::LogManager::GetInstance(); |
| 123 | const LogLevel effective_level = log_manager->GetEffectiveLogLevel(); |
| 124 | for (const auto& [button, level] : button_level_pairs) |
| 125 | { |
| 126 | QFont font = button->font(); |
| 127 | if (level == effective_level) |
| 128 | { |
| 129 | const bool is_base_layer_active = |
| 130 | Config::GetActiveLayerForConfig(Common::Log::LOGGER_VERBOSITY) == |
| 131 | Config::LayerType::Base; |
| 132 | font.setBold(!is_base_layer_active); |
| 133 | |
| 134 | button->setChecked(true); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | font.setBold(false); |
| 139 | } |
| 140 | button->setFont(font); |
| 141 | } |
| 142 | }; |
| 143 | connect(&Settings::Instance(), &Settings::ConfigChanged, this, update_verbosity_boldness); |
| 144 | |
| 145 | connect(m_out_file, &QCheckBox::toggled, this, &LogConfigWidget::SaveSettings); |
| 146 | connect(m_out_console, &QCheckBox::toggled, this, &LogConfigWidget::SaveSettings); |
| 147 | connect(m_out_window, &QCheckBox::toggled, this, &LogConfigWidget::SaveSettings); |
| 148 | |
| 149 | connect(m_types_toggle, &QPushButton::clicked, [this] { |
| 150 | m_all_enabled = !m_all_enabled; |
| 151 | |
| 152 | // Don't save every time we change an item |
| 153 | m_block_save = true; |
| 154 | |
| 155 | for (int i = 0; i < m_types_list->count(); i++) |
| 156 | m_types_list->item(i)->setCheckState(m_all_enabled ? Qt::Checked : Qt::Unchecked); |
| 157 | |
| 158 | m_block_save = false; |
| 159 | |
| 160 | SaveSettings(); |
| 161 | }); |
| 162 | |
| 163 | connect(m_types_list, &QListWidget::itemChanged, this, &LogConfigWidget::SaveSettings); |
| 164 | |
| 165 | connect(&Settings::Instance(), &Settings::LogConfigVisibilityChanged, this, |
| 166 | [this](bool visible) { setHidden(!visible); }); |
| 167 | } |
| 168 | |
| 169 | void LogConfigWidget::LoadSettings() |
| 170 | { |
| 171 | const auto* const log_manager = Common::Log::LogManager::GetInstance(); |
| 172 | const auto& settings = Settings::GetQSettings(); |
| 173 | |
| 174 | restoreGeometry(settings.value(QStringLiteral("logconfigwidget/geometry")).toByteArray()); |
| 175 | setFloating(settings.value(QStringLiteral("logconfigwidget/floating")).toBool()); |
| 176 | |
| 177 | // Config - Verbosity |
| 178 | const Common::Log::LogLevel verbosity = log_manager->GetEffectiveLogLevel(); |
| 179 | m_verbosity_notice->setChecked(verbosity == Common::Log::LogLevel::LNOTICE); |
| 180 | m_verbosity_error->setChecked(verbosity == Common::Log::LogLevel::LERROR); |
| 181 | m_verbosity_warning->setChecked(verbosity == Common::Log::LogLevel::LWARNING); |
| 182 | m_verbosity_info->setChecked(verbosity == Common::Log::LogLevel::LINFO); |
| 183 | m_verbosity_debug->setChecked(verbosity == Common::Log::LogLevel::LDEBUG); |
| 184 | |
| 185 | // Config - Outputs |
| 186 | m_out_file->setChecked(log_manager->IsListenerEnabled(Common::Log::LogListener::FILE_LISTENER)); |
| 187 | m_out_console->setChecked( |
| 188 | log_manager->IsListenerEnabled(Common::Log::LogListener::CONSOLE_LISTENER)); |
| 189 | m_out_window->setChecked( |
| 190 | log_manager->IsListenerEnabled(Common::Log::LogListener::LOG_WINDOW_LISTENER)); |
| 191 | |
| 192 | // Config - Log Types |
| 193 | for (int i = 0; i < static_cast<int>(Common::Log::LogType::NUMBER_OF_LOGS); ++i) |
| 194 | { |
| 195 | const auto log_type = static_cast<Common::Log::LogType>(i); |
| 196 | const bool log_enabled = log_manager->IsEnabled(log_type); |
| 197 | |
| 198 | if (!log_enabled) |
| 199 | m_all_enabled = false; |
| 200 | |
| 201 | m_types_list->item(i)->setCheckState(log_enabled ? Qt::Checked : Qt::Unchecked); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void LogConfigWidget::SaveSettings() |
| 206 | { |
| 207 | if (m_block_save) |
| 208 | return; |
| 209 | |
| 210 | auto& settings = Settings::GetQSettings(); |
| 211 | |
| 212 | settings.setValue(QStringLiteral("logconfigwidget/geometry"), saveGeometry()); |
| 213 | settings.setValue(QStringLiteral("logconfigwidget/floating"), isFloating()); |
| 214 | |
| 215 | auto* const log_manager = Common::Log::LogManager::GetInstance(); |
| 216 | |
| 217 | // Config - Outputs |
| 218 | log_manager->EnableListener(Common::Log::LogListener::FILE_LISTENER, m_out_file->isChecked()); |
| 219 | log_manager->EnableListener(Common::Log::LogListener::CONSOLE_LISTENER, |
| 220 | m_out_console->isChecked()); |
| 221 | log_manager->EnableListener(Common::Log::LogListener::LOG_WINDOW_LISTENER, |
| 222 | m_out_window->isChecked()); |
| 223 | // Config - Log Types |
| 224 | for (int i = 0; i < static_cast<int>(Common::Log::LogType::NUMBER_OF_LOGS); ++i) |
| 225 | { |
| 226 | const auto type = static_cast<Common::Log::LogType>(i); |
| 227 | const bool enabled = m_types_list->item(i)->checkState() == Qt::Checked; |
| 228 | const bool was_enabled = log_manager->IsEnabled(type); |
| 229 | |
| 230 | if (enabled != was_enabled) |
| 231 | log_manager->SetEnable(type, enabled); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void LogConfigWidget::closeEvent(QCloseEvent*) |
| 236 | { |
| 237 | Settings::Instance().SetLogConfigVisible(false); |
| 238 | } |