Add log regex filter (#9)

This commit is contained in:
PabloMK7 2024-03-05 14:51:58 +01:00 committed by GitHub
parent bf0127d0ae
commit f112d975b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 57 additions and 2 deletions

View file

@ -3,6 +3,7 @@
// Refer to the license.txt file included.
#include <chrono>
#include <boost/regex.hpp>
#include <fmt/format.h>
@ -234,6 +235,19 @@ public:
filter = f;
}
bool SetRegexFilter(const std::string& regex) {
if (regex.empty()) {
regex_filter = boost::regex();
return true;
}
regex_filter = boost::regex(regex, boost::regex_constants::no_except);
if (regex_filter.status() != 0) {
regex_filter = boost::regex();
return false;
}
return true;
}
void SetColorConsoleBackendEnabled(bool enabled) {
color_console_backend.SetEnabled(enabled);
}
@ -243,8 +257,13 @@ public:
if (!filter.CheckMessage(log_class, log_level)) {
return;
}
message_queue.EmplaceWait(
CreateEntry(log_class, log_level, filename, line_num, function, std::move(message)));
Entry new_entry =
CreateEntry(log_class, log_level, filename, line_num, function, std::move(message));
if (!regex_filter.empty() &&
!boost::regex_search(FormatLogMessage(new_entry), regex_filter)) {
return;
}
message_queue.EmplaceWait(new_entry);
}
private:
@ -406,6 +425,7 @@ private:
static inline std::unique_ptr<Impl, decltype(&Deleter)> instance{nullptr, Deleter};
Filter filter;
boost::regex regex_filter;
DebuggerBackend debugger_backend{};
ColorConsoleBackend color_console_backend{};
FileBackend file_backend;
@ -446,6 +466,10 @@ void SetGlobalFilter(const Filter& filter) {
Impl::Instance().SetGlobalFilter(filter);
}
bool SetRegexFilter(const std::string& regex) {
return Impl::Instance().SetRegexFilter(regex);
}
void SetColorConsoleBackendEnabled(bool enabled) {
Impl::Instance().SetColorConsoleBackendEnabled(enabled);
}

View file

@ -26,5 +26,11 @@ void DisableLoggingInTests();
*/
void SetGlobalFilter(const Filter& filter);
/**
* Only allow messages that match the specified regex. The regex is matched against the final log
* text.
*/
bool SetRegexFilter(const std::string& regex);
void SetColorConsoleBackendEnabled(bool enabled);
} // namespace Common::Log

View file

@ -542,6 +542,7 @@ struct Values {
// Miscellaneous
Setting<std::string> log_filter{"*:Info", "log_filter"};
Setting<std::string> log_regex_filter{"", "log_regex_filter"};
// Video Dumping
std::string output_format;