From 62f4f787d0055a57a2c6325d98a154d26c867244 Mon Sep 17 00:00:00 2001 From: Jules Fouchy Date: Fri, 4 Aug 2023 16:49:24 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20[MessageConsole]=20Do=20the=20li?= =?UTF-8?q?miting=20of=20the=20size=20during=20imgui=5Fwindow()=20to=20avo?= =?UTF-8?q?id=20deadlocks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Cool/Log/MessageConsole.cpp | 24 ++++++++++++++++-------- src/Cool/Log/MessageConsole.h | 23 ++++++++++++----------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/Cool/Log/MessageConsole.cpp b/src/Cool/Log/MessageConsole.cpp index 0ecb5ac47..22e8d5f93 100644 --- a/src/Cool/Log/MessageConsole.cpp +++ b/src/Cool/Log/MessageConsole.cpp @@ -51,14 +51,6 @@ void MessageConsole::on_message_sent(const internal::RawMessageId& id) { _is_open = true; _message_just_sent = id; - if (_messages.underlying_container().underlying_container().size() > 999) // If there are too many messages the console starts to cause lag. - { - auto const it = std::find_if(_messages.begin(), _messages.end(), [](auto const& pair) { - return is_clearable(pair.second); - }); - if (it != _messages.end()) - _messages.underlying_container().underlying_container().erase(it); - } } void MessageConsole::remove(const MessageId& id) @@ -195,8 +187,24 @@ void MessageConsole::show_number_of_messages_of_given_severity(MessageSeverity s } } +void MessageConsole::remove_messages_to_keep_size_below(size_t max_number_of_messages) +{ + while (_messages.underlying_container().underlying_container().size() > max_number_of_messages) + { + auto const it = std::find_if(_messages.begin(), _messages.end(), [](auto const& pair) { + return is_clearable(pair.second); + }); + if (it != _messages.end()) + _messages.underlying_container().underlying_container().erase(it); + else + break; + } +} + void MessageConsole::imgui_window() { + remove_messages_to_keep_size_below(999); // If there are too many messages the console starts to cause lag. + if (_is_open) { refresh_counts_per_severity(); diff --git a/src/Cool/Log/MessageConsole.h b/src/Cool/Log/MessageConsole.h index 9e6aa91b0..c6883630f 100644 --- a/src/Cool/Log/MessageConsole.h +++ b/src/Cool/Log/MessageConsole.h @@ -45,17 +45,18 @@ class MessageConsole { void imgui_window(); private: - void remove(const internal::RawMessageId&); - void close_window_if_empty(); - void close_window(); - void on_message_sent(const internal::RawMessageId&); - void show_number_of_messages_of_given_severity(MessageSeverity); - void refresh_counts_per_severity(); - void imgui_menu_bar(); - void imgui_show_all_messages(); - auto there_are_clearable_messages() const -> bool; - auto there_are_clearable_messages(MessageSeverity) const -> bool; - auto should_show(const internal::MessageWithMetadata&) const -> bool; + void remove(const internal::RawMessageId&); + void close_window_if_empty(); + void close_window(); + void on_message_sent(const internal::RawMessageId&); + void show_number_of_messages_of_given_severity(MessageSeverity); + void refresh_counts_per_severity(); + void imgui_menu_bar(); + void imgui_show_all_messages(); + [[nodiscard]] auto there_are_clearable_messages() const -> bool; + [[nodiscard]] auto there_are_clearable_messages(MessageSeverity) const -> bool; + [[nodiscard]] auto should_show(const internal::MessageWithMetadata&) const -> bool; + void remove_messages_to_keep_size_below(size_t max_number_of_messages); class MessagesCountPerSeverity { public: