Optimize log window

This commit is contained in:
blankie 2023-01-20 22:22:21 +07:00
parent 3219ebf65e
commit 8a4eaea7f9
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
4 changed files with 33 additions and 8 deletions

View File

@ -19,10 +19,7 @@ static inline void check_for_logcat_items(LogcatThread& logcat_thread) {
while ((logcat_thread_item = logcat_thread.atomic_ring_buffer.get())) {
if (std::holds_alternative<std::string>(*logcat_thread_item)) {
if (!log_entries.empty()) {
log_entries += '\n';
}
log_entries += std::move(std::get<std::string>(*logcat_thread_item));
log_raw(std::move(std::get<std::string>(*logcat_thread_item)), false);
} else if (std::holds_alternative<LogcatEntry>(*logcat_thread_item)) {
LogcatEntry logcat_entry = std::move(std::get<LogcatEntry>(*logcat_thread_item));
log("Received new logcat entry");
@ -74,6 +71,7 @@ static inline void logs_window(ImFont* monospace_font, bool* autoscrolling, bool
if (ImGui::Button("Clear")) {
log_entries.clear();
log_entry_line_offsets = {0};
}
ImGui::SameLine();
if (ImGui::Button("Copy")) {
@ -82,9 +80,30 @@ static inline void logs_window(ImFont* monospace_font, bool* autoscrolling, bool
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()
if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar)) {
ImGui::PushFont(monospace_font);
ImGui::TextUnformatted(log_entries.data(), &log_entries[log_entries.size()]);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
ImGuiListClipper clipper;
clipper.Begin(static_cast<int>(log_entry_line_offsets.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 char* start_offset = &log_entries[log_entry_line_offsets[i]];
const char* end_offset = log_entry_line_offsets.size() > i + 1
? &log_entries[log_entry_line_offsets[i + 1] - 1]
: &log_entries[log_entries.size()];
ImGui::TextUnformatted(start_offset, end_offset);
}
}
clipper.End();
ImGui::PopStyleVar();
ImGui::PopFont();
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
*autoscrolling = true;

View File

@ -6,6 +6,7 @@
#include "log.h"
std::string log_entries;
std::vector<size_t> log_entry_line_offsets = {0};
std::string format_log(std::string entry, time_t time) {
size_t newline_pos;
@ -23,10 +24,13 @@ std::string format_log(std::string entry) {
return format_log(std::move(entry), time(nullptr));
}
void log_raw(std::string line) {
printf("%s\n", line.c_str());
void log_raw(std::string line, bool print) {
if (print) {
printf("%s\n", line.c_str());
}
if (!log_entries.empty()) {
log_entries += '\n';
log_entry_line_offsets.push_back(log_entries.size());
}
log_entries += std::move(line);
}

3
log.h
View File

@ -5,9 +5,10 @@
#include <exception>
extern std::string log_entries;
extern std::vector<size_t> log_entry_line_offsets;
std::string format_log(std::string entry, time_t time);
std::string format_log(std::string entry);
void log_raw(std::string line);
void log_raw(std::string line, bool print = true);
void log(std::string entry, time_t time);
void log(std::string entry);

View File

@ -3,6 +3,7 @@
#include "log.h"
#define IM_ASSERT_USER_ERROR(expr,msg) do { if (!(expr)) { log(msg); } } while (0);
#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS
#ifndef NDEBUG
#define IMGUI_DEBUG_PARANOID