gtsam_points
Loading...
Searching...
No Matches
spdlog_sink.hpp
Go to the documentation of this file.
1#ifndef GUIK_SPDLOG_SINK_HPP
2#define GUIK_SPDLOG_SINK_HPP
3
4#include <deque>
5#include <imgui.h>
6#include <spdlog/spdlog.h>
7
8#if SPDLOG_VERSION >= 10600
9#include <spdlog/pattern_formatter.h>
10#else
11#include <spdlog/details/pattern_formatter.h>
12#endif
13
14namespace guik {
15
23template <typename RingBufferSink>
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);
26 return [=] {
27 if (!(*enabled)) {
28 return;
29 }
30
31 const auto log_messages = sink->last_raw();
32
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();
37
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);
46
47 spdlog::pattern_formatter formatter;
48
49 for (const auto& log : log_messages) {
50 const auto& level = log.level;
51
52 spdlog::memory_buf_t formatted;
53 formatter.format(log, formatted);
54 std::string text = fmt::to_string(formatted);
55
56 const auto& color = colors[static_cast<int>(level)];
57 ImGui::TextColored(color, "%s", text.c_str());
58 }
59
60 if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
61 ImGui::SetScrollHereY(1.0f);
62 }
63 }
64
65 ImGui::EndChild();
66 ImGui::End();
67 };
68}
69
70} // namespace guik
71
72#endif
Definition drawable_container.hpp:9
std::function< void()> create_logger_ui(const std::shared_ptr< RingBufferSink > &sink, double bg_alpha=1.0)
Definition spdlog_sink.hpp:24