From 347f216abfbaf4cf1acc78caf70463c78ca4e0b5 Mon Sep 17 00:00:00 2001 From: nerix Date: Wed, 17 May 2023 23:32:50 +0200 Subject: [PATCH] Add Command to Set Logging/Filter Rules at Runtime (#4637) Co-authored-by: pajlada --- CHANGELOG.md | 2 + src/CMakeLists.txt | 2 + .../commands/CommandController.cpp | 3 ++ .../commands/builtin/chatterino/Debugging.cpp | 45 +++++++++++++++++++ .../commands/builtin/chatterino/Debugging.hpp | 15 +++++++ 5 files changed, 67 insertions(+) create mode 100644 src/controllers/commands/builtin/chatterino/Debugging.cpp create mode 100644 src/controllers/commands/builtin/chatterino/Debugging.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index eb8bc822f..85ec55c55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unversioned +- Dev: Added command to set Qt's logging filter/rules at runtime (`/c2-set-logging-rules`). (#4637) + ## 2.4.4 - Minor: Added a Send button in the input box so you can click to send a message. This is disabled by default and can be enabled with the "Show send message button" setting. (#4607) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0fa8d9073..fe4a44fa4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -60,6 +60,8 @@ set(SOURCE_FILES controllers/accounts/AccountModel.cpp controllers/accounts/AccountModel.hpp + controllers/commands/builtin/chatterino/Debugging.cpp + controllers/commands/builtin/chatterino/Debugging.hpp controllers/commands/builtin/twitch/ChatSettings.cpp controllers/commands/builtin/twitch/ChatSettings.hpp controllers/commands/builtin/twitch/ShieldMode.cpp diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index a68f0545f..3c7c4febd 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -7,6 +7,7 @@ #include "common/QLogging.hpp" #include "common/SignalVector.hpp" #include "controllers/accounts/AccountController.hpp" +#include "controllers/commands/builtin/chatterino/Debugging.hpp" #include "controllers/commands/builtin/twitch/ChatSettings.hpp" #include "controllers/commands/builtin/twitch/ShieldMode.hpp" #include "controllers/commands/Command.hpp" @@ -3211,6 +3212,8 @@ void CommandController::initialize(Settings &, Paths &paths) this->registerCommand("/shield", &commands::shieldModeOn); this->registerCommand("/shieldoff", &commands::shieldModeOff); + + this->registerCommand("/c2-set-logging-rules", &commands::setLoggingRules); } void CommandController::save() diff --git a/src/controllers/commands/builtin/chatterino/Debugging.cpp b/src/controllers/commands/builtin/chatterino/Debugging.cpp new file mode 100644 index 000000000..7482d68ce --- /dev/null +++ b/src/controllers/commands/builtin/chatterino/Debugging.cpp @@ -0,0 +1,45 @@ +#include "controllers/commands/builtin/chatterino/Debugging.hpp" + +#include "common/Channel.hpp" +#include "controllers/commands/CommandContext.hpp" +#include "messages/MessageBuilder.hpp" + +#include +#include + +namespace chatterino::commands { + +QString setLoggingRules(const CommandContext &ctx) +{ + if (ctx.words.size() < 2) + { + ctx.channel->addMessage(makeSystemMessage( + "Usage: /c2-set-logging-rules . To enable debug logging " + "for all categories from chatterino, use " + "'chatterino.*.debug=true'. For the format on the rules, see " + "https://doc.qt.io/qt-6/" + "qloggingcategory.html#configuring-categories")); + return {}; + } + + auto filterRules = ctx.words.mid(1).join('\n'); + + QLoggingCategory::setFilterRules(filterRules); + + auto message = + QStringLiteral("Updated filter rules to '%1'.").arg(filterRules); + + if (!qgetenv("QT_LOGGING_RULES").isEmpty()) + { + message += QStringLiteral( + " Warning: Logging rules were previously set by the " + "QT_LOGGING_RULES environment variable. This might cause " + "interference - see: " + "https://doc.qt.io/qt-6/qloggingcategory.html#setFilterRules"); + } + + ctx.channel->addMessage(makeSystemMessage(message)); + return {}; +} + +} // namespace chatterino::commands diff --git a/src/controllers/commands/builtin/chatterino/Debugging.hpp b/src/controllers/commands/builtin/chatterino/Debugging.hpp new file mode 100644 index 000000000..8cd2330ba --- /dev/null +++ b/src/controllers/commands/builtin/chatterino/Debugging.hpp @@ -0,0 +1,15 @@ +#pragma once + +class QString; + +namespace chatterino { + +struct CommandContext; + +} // namespace chatterino + +namespace chatterino::commands { + +QString setLoggingRules(const CommandContext &ctx); + +} // namespace chatterino::commands