24std::function<void()>
create_logger_ui(
const std::shared_ptr<RingBufferSink>& sink,
double bg_alpha = 1.0) {
25 auto enabled = std::make_shared<bool>(
true);
31 const auto log_messages = sink->last_raw();
33 ImGui::SetNextWindowSize(ImVec2(660, 400), ImGuiCond_FirstUseEver);
34 ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.0f, 0.0f, 0.0f, bg_alpha));
35 ImGui::Begin(
"logging", enabled.get());
36 ImGui::PopStyleColor();
38 if (ImGui::BeginChild(
"scrolling", ImVec2(0, 0),
false, ImGuiWindowFlags_HorizontalScrollbar)) {
39 std::array<ImVec4, 6> colors;
40 colors[
static_cast<int>(spdlog::level::trace)] = ImVec4(0.7f, 0.7f, 0.7f, 0.5f);
41 colors[
static_cast<int>(spdlog::level::debug)] = ImVec4(0.6f, 0.9f, 1.0f, 0.7f);
42 colors[
static_cast<int>(spdlog::level::info)] = ImVec4(0.9f, 1.0f, 0.9f, 1.0f);
43 colors[
static_cast<int>(spdlog::level::warn)] = ImVec4(1.0f, 1.0f, 0.0f, 1.0f);
44 colors[
static_cast<int>(spdlog::level::err)] = ImVec4(1.0f, 0.5f, 0.0f, 1.0f);
45 colors[
static_cast<int>(spdlog::level::critical)] = ImVec4(1.0f, 0.0f, 0.0f, 1.0f);
47 spdlog::pattern_formatter formatter;
49 for (
const auto& log : log_messages) {
50 const auto& level = log.level;
52 spdlog::memory_buf_t formatted;
53 formatter.format(log, formatted);
54 std::string text = fmt::to_string(formatted);
56 const auto& color = colors[
static_cast<int>(level)];
57 ImGui::TextColored(color,
"%s", text.c_str());
60 if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
61 ImGui::SetScrollHereY(1.0f);