80 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
			
		
		
	
	
			80 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
| #include <ctime>
 | |
| #include <imgui.h>
 | |
| 
 | |
| #include "../misc.h"
 | |
| #include "../log.h"
 | |
| #include "logs.h"
 | |
| 
 | |
| static inline void render_table(ImFont* monospace_font, bool* autoscrolling) {
 | |
|     ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
 | |
| 
 | |
|     ImGui::TableSetupScrollFreeze(0, 1);
 | |
|     ImGui::TableSetupColumn("Time", ImGuiTableColumnFlags_None);
 | |
|     ImGui::TableSetupColumn("Message", ImGuiTableColumnFlags_None);
 | |
|     ImGui::TableHeadersRow();
 | |
| 
 | |
|     ImGui::PushFont(monospace_font);
 | |
| 
 | |
|     ImGuiListClipper clipper;
 | |
|     clipper.Begin(static_cast<int>(log_entries.size()));
 | |
|     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);
 | |
| 
 | |
|             const LogEntry* log_entry = &log_entries[i];
 | |
|             char time_as_str[128] = {0};
 | |
|             strftime(time_as_str, 127 * sizeof(char), "%c", localtime(&log_entry->time));
 | |
| 
 | |
|             ImGui::TableNextRow();
 | |
|             if (ImGui::TableSetColumnIndex(0)) ImGui::TextUnformatted(time_as_str);
 | |
|             if (ImGui::TableSetColumnIndex(1)) ImGui::TextUnformatted(log_entry->message);
 | |
|         }
 | |
|     }
 | |
|     clipper.End();
 | |
| 
 | |
|     ImGui::PopFont();
 | |
|     ImGui::PopStyleVar();
 | |
| 
 | |
|     if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
 | |
|         *autoscrolling = true;
 | |
|         ImGui::SetScrollHereY(1.0f);
 | |
|     }
 | |
| 
 | |
|     ImGui::EndTable();
 | |
| }
 | |
| 
 | |
| void logs_window(ImFont* monospace_font, bool* autoscrolling, bool* p_open) {
 | |
|     if (!ImGui::Begin("LogMeow Logs", p_open)) {
 | |
|         ImGui::End();
 | |
|         return;
 | |
|     }
 | |
| 
 | |
|     if (ImGui::Button("Clear")) {
 | |
|         log_entries.clear();
 | |
|     }
 | |
|     ImGui::SameLine();
 | |
|     if (ImGui::Button("Copy")) {
 | |
|         std::string text;
 | |
|         for (const LogEntry& entry : log_entries) {
 | |
|             if (!text.empty()) {
 | |
|                 text += '\n';
 | |
|             }
 | |
|             text += format_log(entry);
 | |
|         }
 | |
|         ImGui::SetClipboardText(text.c_str());
 | |
|     }
 | |
| 
 | |
|     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()
 | |
|     // 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);
 | |
|     }
 | |
|     ImGui::End();
 | |
| }
 |