Skip to content

Commit 37b98aa

Browse files
log UI content filtering and search (#405)
* working prototype * optimized * remove abstraction * Improving the Imgui layer abstraction (#413) * updated formatting and fixed minors issues * refactored imgui layer * update * update --------- Co-authored-by: Jean Philippe <[email protected]>
1 parent 454c921 commit 37b98aa

File tree

2 files changed

+64
-13
lines changed

2 files changed

+64
-13
lines changed

Tetragrama/Components/LogUIComponent.cpp

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include <pch.h>
2+
#include <Helpers/MemoryOperations.h>
23
#include <LogUIComponent.h>
34
#include <imgui.h>
5+
#include <algorithm>
46

57
using namespace ZEngine::Logging;
8+
using namespace ZEngine::Helpers;
69

710
namespace Tetragrama::Components
811
{
@@ -28,36 +31,69 @@ namespace Tetragrama::Components
2831
{
2932
ImGui::Begin(Name.c_str(), (CanBeClosed ? &CanBeClosed : NULL), ImGuiWindowFlags_NoCollapse);
3033

31-
ImGui::SameLine();
32-
m_is_clear_button_pressed = ImGui::Button("Clear");
34+
static const char* items[] = {"All", "info", "error", "warn", "critical", "trace"};
35+
static int current_item = 0;
3336

3437
ImGui::SameLine();
35-
m_is_copy_button_pressed = ImGui::Button("Copy");
38+
ImGui::SetNextItemWidth(70);
39+
if (ImGui::BeginCombo("##Dropdown", items[current_item]))
40+
{
41+
for (int n = 0; n < IM_ARRAYSIZE(items); n++)
42+
{
43+
if (ImGui::Selectable(items[n], current_item == n))
44+
{
45+
current_item = n;
46+
ImGui::SetItemDefaultFocus();
47+
}
48+
}
49+
ImGui::EndCombo();
50+
}
3651

37-
ImGui::Separator();
52+
ImGui::SameLine();
53+
if (ImGui::Button("Clear"))
54+
{
55+
ClearLog();
56+
m_search_buffer[0] = '\0';
57+
}
3858

39-
if (m_is_copy_button_pressed)
59+
ImGui::SameLine();
60+
if (m_is_copy_button_pressed = ImGui::Button("Copy"))
4061
{
4162
ImGui::LogToClipboard();
4263
}
4364

44-
if (m_is_clear_button_pressed)
65+
ImGui::SameLine();
66+
ImGui::InputTextWithHint("##Search", "Search logs...", m_search_buffer, IM_ARRAYSIZE(m_search_buffer));
67+
ImGui::Separator();
68+
69+
std::string search_term;
70+
if (secure_strlen(m_search_buffer) > 0)
4571
{
46-
ClearLog();
72+
search_term = m_search_buffer;
73+
std::transform(search_term.begin(), search_term.end(), search_term.begin(), ::tolower);
4774
}
4875

4976
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
50-
5177
if (ImGui::BeginTable("log_table", 1, ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY))
5278
{
5379
std::lock_guard<std::mutex> lock(m_mutex);
80+
for (const auto& message : m_log_queue)
5481
{
55-
for (const ZEngine::Logging::LogMessage& message : m_log_queue)
82+
if (current_item != 0)
83+
{
84+
if (GetMessageType(message) != items[current_item])
85+
continue;
86+
}
87+
if (secure_strlen(m_search_buffer) > 0)
5688
{
57-
ImGui::TableNextRow();
58-
ImGui::TableSetColumnIndex(0);
59-
ImGui::TextColored({message.Color[0], message.Color[1], message.Color[2], message.Color[3]}, message.Message.data());
89+
std::string message_lower = message.Message;
90+
std::transform(message_lower.begin(), message_lower.end(), message_lower.begin(), ::tolower);
91+
if (message_lower.find(search_term) == std::string::npos)
92+
continue;
6093
}
94+
ImGui::TableNextRow();
95+
ImGui::TableSetColumnIndex(0);
96+
ImGui::TextColored({message.Color[0], message.Color[1], message.Color[2], message.Color[3]}, message.Message.data());
6197
}
6298
ImGui::EndTable();
6399
}
@@ -92,4 +128,17 @@ namespace Tetragrama::Components
92128
m_currentCount++;
93129
}
94130
}
131+
132+
std::string LogUIComponent::GetMessageType(const ZEngine::Logging::LogMessage& message)
133+
{
134+
if (message.Color[0] == 0.0f && message.Color[1] == 1.0f && message.Color[2] == 0.0f && message.Color[3] == 1.0f)
135+
return "info";
136+
if (message.Color[0] == 1.0f && message.Color[1] == 0.5f && message.Color[2] == 0.0f && message.Color[3] == 1.0f)
137+
return "warn";
138+
if (message.Color[0] == 1.0f && message.Color[1] == 0.0f && message.Color[2] == 0.0f && message.Color[3] == 1.0f)
139+
return "error";
140+
if (message.Color[0] == 1.0f && message.Color[1] == 0.0f && message.Color[2] == 1.0f && message.Color[3] == 0.0f)
141+
return "critical";
142+
return "trace";
143+
}
95144
} // namespace Tetragrama::Components

Tetragrama/Components/LogUIComponent.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ namespace Tetragrama::Components
1818

1919
void ClearLog();
2020

21+
std::string GetMessageType(const ZEngine::Logging::LogMessage& message);
22+
2123
private:
2224
uint32_t m_maxCount{1024};
2325
uint32_t m_currentCount{0};
2426
bool m_auto_scroll{true};
2527
bool m_is_copy_button_pressed{false};
26-
bool m_is_clear_button_pressed{false};
2728
std::vector<ZEngine::Logging::LogMessage> m_log_queue;
2829
std::mutex m_mutex;
30+
char m_search_buffer[256] = "";
2931
};
3032
} // namespace Tetragrama::Components

0 commit comments

Comments
 (0)