1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-23 03:02:53 +01:00

Qt: batch stack suffix updates

No need to patch the suffix every single time. We can just do it when we exit the stack loop.
This commit is contained in:
Megamouse 2022-08-26 03:16:58 +02:00
parent a9dcad629b
commit ab31e96385
2 changed files with 67 additions and 33 deletions

View File

@ -230,14 +230,14 @@ void log_frame::CreateAndConnectActions()
m_clear_act = new QAction(tr("Clear"), this); m_clear_act = new QAction(tr("Clear"), this);
connect(m_clear_act, &QAction::triggered, [this]() connect(m_clear_act, &QAction::triggered, [this]()
{ {
m_old_log_text = ""; m_old_log_text.clear();
m_log->clear(); m_log->clear();
}); });
m_clear_tty_act = new QAction(tr("Clear"), this); m_clear_tty_act = new QAction(tr("Clear"), this);
connect(m_clear_tty_act, &QAction::triggered, [this]() connect(m_clear_tty_act, &QAction::triggered, [this]()
{ {
m_old_tty_text = ""; m_old_tty_text.clear();
m_tty->clear(); m_tty->clear();
}); });
@ -500,7 +500,9 @@ void log_frame::RepaintTextColors()
void log_frame::UpdateUI() void log_frame::UpdateUI()
{ {
const auto start = steady_clock::now(); const std::chrono::time_point start = steady_clock::now();
const std::chrono::time_point tty_timeout = start + 4ms;
const std::chrono::time_point log_timeout = start + 7ms;
// Check TTY logs // Check TTY logs
while (const u64 size = std::max<s64>(0, g_tty_size.load() - (m_tty_file ? m_tty_file.pos() : 0))) while (const u64 size = std::max<s64>(0, g_tty_size.load() - (m_tty_file ? m_tty_file.pos() : 0)))
@ -579,7 +581,7 @@ void log_frame::UpdateUI()
} }
// Limit processing time // Limit processing time
if (steady_clock::now() >= start + 4ms || buf.empty()) break; if (steady_clock::now() >= tty_timeout || buf.empty()) break;
} }
const auto font_start_tag = [](const QColor& color) -> const QString { return QStringLiteral("<font color = \"") % color.name() % QStringLiteral("\">"); }; const auto font_start_tag = [](const QColor& color) -> const QString { return QStringLiteral("<font color = \"") % color.name() % QStringLiteral("\">"); };
@ -595,7 +597,8 @@ void log_frame::UpdateUI()
m_log_text.resize(0); m_log_text.resize(0);
// Handle a common case in which we may need to override the previous repetition count // Handle a common case in which we may need to override the previous repetition count
bool is_first_rep = true; bool is_first_rep = m_stack_log;
usz first_rep_counter = m_log_counter;
// Batch output of multiple lines if possible (optimization) // Batch output of multiple lines if possible (optimization)
auto flush = [&]() auto flush = [&]()
@ -623,26 +626,33 @@ void log_frame::UpdateUI()
if (is_first_rep) if (is_first_rep)
{ {
// Overwrite existing repetition counter in our text document. // Overwrite existing repetition counter in our text document.
ensure(m_log_counter > 2); // Anything else is a bug ensure(first_rep_counter > m_log_counter); // Anything else is a bug
constexpr int size_of_x = 2; // " x"
text_cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); text_cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
text_cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, size_of_x + QString::number(m_log_counter - 1).size());
text_cursor.insertHtml(font_start_tag_stack % QStringLiteral("&nbsp;x") % QString::number(m_log_counter) % font_end_tag); if (m_log_counter > 1)
{
constexpr int size_of_x = 2; // " x"
const int size_of_number = static_cast<int>(QString::number(m_log_counter).size());
text_cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, size_of_x + size_of_number);
} }
else if (m_log_counter > 1)
text_cursor.insertHtml(font_start_tag_stack % QStringLiteral("&nbsp;x") % QString::number(first_rep_counter) % font_end_tag);
}
else
{ {
// Insert both messages and repetition prefix (append is more optimized than concatenation) // Insert both messages and repetition prefix (append is more optimized than concatenation)
m_log_text += font_end_tag; m_log_text += font_end_tag;
if (m_log_counter > 1)
{
m_log_text += font_start_tag_stack; m_log_text += font_start_tag_stack;
m_log_text += QStringLiteral(" x"); m_log_text += QStringLiteral(" x");
m_log_text += QString::number(m_log_counter); m_log_text += QString::number(m_log_counter);
m_log_text += font_end_tag; m_log_text += font_end_tag;
m_log->appendHtml(m_log_text);
} }
else
{
m_log_text += font_end_tag;
m_log->appendHtml(m_log_text); m_log->appendHtml(m_log_text);
} }
@ -662,6 +672,27 @@ void log_frame::UpdateUI()
m_log_text.clear(); m_log_text.clear();
}; };
const auto patch_first_stacked_message = [&]() -> bool
{
if (is_first_rep)
{
const bool needs_update = first_rep_counter > m_log_counter;
if (needs_update)
{
// Update the existing stack suffix.
flush();
}
is_first_rep = false;
m_log_counter = first_rep_counter;
return needs_update;
}
return false;
};
// Check main logs // Check main logs
while (auto* packet = s_gui_listener.get()) while (auto* packet = s_gui_listener.get())
{ {
@ -671,16 +702,19 @@ void log_frame::UpdateUI()
// Check if we can stack this log message. // Check if we can stack this log message.
if (m_stack_log && m_old_log_level == packet->sev && packet->msg == m_old_log_text) if (m_stack_log && m_old_log_level == packet->sev && packet->msg == m_old_log_text)
{ {
m_log_counter++;
if (is_first_rep) if (is_first_rep)
{ {
flush(); // Keep tracking the old stack suffix count as long as the last known message keeps repeating.
first_rep_counter++;
}
else
{
m_log_counter++;
} }
s_gui_listener.pop(); s_gui_listener.pop();
if (steady_clock::now() >= start + 7ms) if (steady_clock::now() >= log_timeout)
{ {
// Must break eventually // Must break eventually
break; break;
@ -689,10 +723,8 @@ void log_frame::UpdateUI()
continue; continue;
} }
is_first_rep = false; // Add/update counter suffix if needed. Try not to hold too much data at a time so the frame content will be updated frequently.
if (!patch_first_stacked_message() && (m_log_counter > 1 || m_log_text.size() > 0x1000))
// Add counter suffix if needed. Try not to hold too much data at a time so the frame content will be updated frequently.
if (m_log_counter > 1 || m_log_text.size() > 0x1000)
{ {
flush(); flush();
} }
@ -742,12 +774,14 @@ void log_frame::UpdateUI()
s_gui_listener.pop(); s_gui_listener.pop();
// Limit processing time // Limit processing time
if (steady_clock::now() >= start + 7ms) break; if (steady_clock::now() >= log_timeout) break;
} }
is_first_rep = false; if (!patch_first_stacked_message())
{
flush(); flush();
} }
}
void log_frame::closeEvent(QCloseEvent *event) void log_frame::closeEvent(QCloseEvent *event)
{ {

View File

@ -52,8 +52,8 @@ private:
std::string m_old_log_text; std::string m_old_log_text;
QString m_old_tty_text; QString m_old_tty_text;
QString m_log_text; QString m_log_text;
ullong m_log_counter{}; usz m_log_counter{};
ullong m_tty_counter{}; usz m_tty_counter{};
bool m_stack_log{}; bool m_stack_log{};
bool m_stack_tty{}; bool m_stack_tty{};
logs::level m_old_log_level{}; logs::level m_old_log_level{};