2023-01-29 08:21:22 +00:00
|
|
|
#include <ctime>
|
2023-02-22 09:09:33 +00:00
|
|
|
#include <imgui/imgui.h>
|
2023-01-23 08:44:16 +00:00
|
|
|
|
2023-01-29 10:08:27 +00:00
|
|
|
#include "../misc.h"
|
2023-01-23 08:44:16 +00:00
|
|
|
#include "../log.h"
|
|
|
|
#include "logs.h"
|
|
|
|
|
2023-02-11 02:28:10 +00:00
|
|
|
static inline void render_table_item_context_menu(const LogEntry& log_entry) {
|
2023-02-09 08:27:39 +00:00
|
|
|
struct tm tm;
|
|
|
|
char time_as_str[128] = {0};
|
|
|
|
strftime(time_as_str, 127 * sizeof(char), "%c", localtime_r(&log_entry.time, &tm));
|
2023-02-11 02:28:10 +00:00
|
|
|
|
|
|
|
ImGui::TextDisabled("[%s] %s", time_as_str, log_entry.message.c_str());
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::PushFont(nullptr);
|
|
|
|
|
|
|
|
if (ImGui::Selectable("Copy")) ImGui::SetClipboardText(to_string(log_entry).c_str());
|
|
|
|
if (ImGui::Selectable("Copy Time")) ImGui::SetClipboardText(time_as_str);
|
|
|
|
if (ImGui::Selectable("Copy Message")) ImGui::SetClipboardText(log_entry.message.c_str());
|
|
|
|
|
|
|
|
ImGui::PopFont();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void render_table_item(const LogEntry& log_entry, size_t log_entry_index, size_t* copying_entry_index) {
|
|
|
|
auto table_item_popup = [&]() {
|
|
|
|
bool popup_was_open = ImGui::IsPopupOpen("copy_popup");
|
|
|
|
ImGui::OpenPopupOnItemClick("copy_popup");
|
|
|
|
if (!popup_was_open) {
|
|
|
|
*copying_entry_index = log_entry_index;
|
2023-02-09 08:27:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ImGui::TableNextRow();
|
2023-02-11 02:28:10 +00:00
|
|
|
if (ImGui::TableSetColumnIndex(0)) {
|
|
|
|
struct tm tm;
|
|
|
|
char time_as_str[128] = {0};
|
|
|
|
strftime(time_as_str, 127 * sizeof(char), "%c", localtime_r(&log_entry.time, &tm));
|
|
|
|
|
|
|
|
ImGui::TextUnformatted(time_as_str);
|
|
|
|
table_item_popup();
|
|
|
|
}
|
|
|
|
if (ImGui::TableSetColumnIndex(1)) { ImGui::TextUnformatted(log_entry.message); table_item_popup(); }
|
2023-02-09 08:27:39 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 08:21:22 +00:00
|
|
|
static inline void render_table(ImFont* monospace_font, bool* autoscrolling) {
|
2023-02-11 02:28:10 +00:00
|
|
|
static size_t copying_entry_index;
|
|
|
|
|
2023-01-29 08:21:22 +00:00
|
|
|
ImGui::TableSetupScrollFreeze(0, 1);
|
|
|
|
ImGui::TableSetupColumn("Time", ImGuiTableColumnFlags_None);
|
|
|
|
ImGui::TableSetupColumn("Message", ImGuiTableColumnFlags_None);
|
|
|
|
ImGui::TableHeadersRow();
|
|
|
|
|
|
|
|
ImGui::PushFont(monospace_font);
|
|
|
|
|
2023-01-23 08:44:16 +00:00
|
|
|
ImGuiListClipper clipper;
|
2023-01-29 08:21:22 +00:00
|
|
|
clipper.Begin(static_cast<int>(log_entries.size()));
|
2023-01-23 08:44:16 +00:00
|
|
|
while (clipper.Step()) {
|
|
|
|
for (int i_u = clipper.DisplayStart; i_u < clipper.DisplayEnd; i_u++) {
|
|
|
|
// what'd we do if we log the error about an error failing to show logs
|
|
|
|
assert(i_u >= 0);
|
|
|
|
size_t i = static_cast<size_t>(i_u);
|
|
|
|
|
2023-02-11 02:28:10 +00:00
|
|
|
render_table_item(log_entries[i], i, ©ing_entry_index);
|
2023-01-23 08:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
clipper.End();
|
|
|
|
|
2023-02-11 02:28:10 +00:00
|
|
|
if (ImGui::BeginPopup("copy_popup")) {
|
|
|
|
render_table_item_context_menu(log_entries[copying_entry_index]);
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
|
2023-01-23 08:44:16 +00:00
|
|
|
ImGui::PopFont();
|
|
|
|
|
|
|
|
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
|
|
|
|
*autoscrolling = true;
|
|
|
|
ImGui::SetScrollHereY(1.0f);
|
|
|
|
}
|
2023-01-29 08:21:22 +00:00
|
|
|
|
|
|
|
ImGui::EndTable();
|
2023-01-23 08:44:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 15:57:46 +00:00
|
|
|
void logs_window(ImFont* monospace_font, bool* __restrict autoscrolling, bool* __restrict p_open) {
|
2023-02-04 07:31:50 +00:00
|
|
|
if (!ImGui::BeginWithCloseShortcut("LogMeow Logs", p_open)) {
|
2023-01-23 08:44:16 +00:00
|
|
|
ImGui::End();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::Button("Clear")) {
|
|
|
|
log_entries.clear();
|
|
|
|
}
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::Button("Copy")) {
|
2023-01-29 08:21:22 +00:00
|
|
|
std::string text;
|
|
|
|
for (const LogEntry& entry : log_entries) {
|
|
|
|
if (!text.empty()) {
|
|
|
|
text += '\n';
|
|
|
|
}
|
2023-02-09 07:44:34 +00:00
|
|
|
text += to_string(entry);
|
2023-01-29 08:21:22 +00:00
|
|
|
}
|
|
|
|
ImGui::SetClipboardText(text.c_str());
|
2023-01-23 08:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
// copied from imgui/imgui_demo.cpp: [SECTION] Example App: Debug Console / ShowExampleAppConsole()
|
|
|
|
// and [SECTION] Example App: Long Text / ShowExampleAppLongText()
|
|
|
|
// and [SECTION] Example App: Debug Log / ShowExampleAppLog()
|
2023-01-29 08:21:22 +00:00
|
|
|
// and Tables/Vertical scrolling, with clipping
|
|
|
|
const constexpr ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_ScrollX | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable;
|
|
|
|
if (ImGui::BeginTable("logs", 2, flags)) {
|
|
|
|
render_table(monospace_font, autoscrolling);
|
2023-01-23 08:44:16 +00:00
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|