|
| 1 | +#include "LogWindow.h" |
| 2 | +#include "imgui_internal.h" |
| 3 | +#include "Utilities/Logger.h" |
| 4 | + |
| 5 | + |
| 6 | +using namespace std; |
| 7 | +using namespace SPH; |
| 8 | +using namespace Utilities; |
| 9 | + |
| 10 | + |
| 11 | +LogWindow::LogWindow() |
| 12 | +{ |
| 13 | + m_lastSize = 0; |
| 14 | + m_selectedFilter = 1; |
| 15 | +} |
| 16 | + |
| 17 | +LogWindow::~LogWindow(void) |
| 18 | +{ |
| 19 | +} |
| 20 | + |
| 21 | +void LogWindow::drawWindow(ImFont* textFont) |
| 22 | +{ |
| 23 | + if (m_bufferSink != 0) |
| 24 | + { |
| 25 | + std::vector<std::pair<Utilities::LogLevel, std::string>>& buffer = m_bufferSink->getBuffer(); |
| 26 | + |
| 27 | + float alpha = 0.8f; |
| 28 | + if (ImGui::IsWindowDocked()) |
| 29 | + alpha = 1.0f; |
| 30 | + ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.1f, 0.1f, 0.1f, alpha)); |
| 31 | + |
| 32 | + ImGui::Begin("Log"); |
| 33 | + |
| 34 | + if (ImGui::Button("Clear")) |
| 35 | + m_bufferSink->clearBuffer(); |
| 36 | + ImGui::SameLine(); |
| 37 | + bool copy = ImGui::Button("Copy"); |
| 38 | + ImGui::SameLine(); |
| 39 | + |
| 40 | + const char* items[] = { "Debug", "Info", "Warning", "Error" }; |
| 41 | + const char* currentItem = items[m_selectedFilter]; |
| 42 | + ImGui::PushItemWidth(200.0f); |
| 43 | + if (ImGui::BeginCombo("Filter", currentItem)) |
| 44 | + { |
| 45 | + for (int n = 0; n < IM_ARRAYSIZE(items); n++) |
| 46 | + { |
| 47 | + const bool is_selected = (m_selectedFilter == n); |
| 48 | + if (ImGui::Selectable(items[n], is_selected)) |
| 49 | + { |
| 50 | + if (n != m_selectedFilter) |
| 51 | + m_selectedFilter = n; |
| 52 | + } |
| 53 | + if (is_selected) |
| 54 | + ImGui::SetItemDefaultFocus(); |
| 55 | + } |
| 56 | + ImGui::EndCombo(); |
| 57 | + } |
| 58 | + ImGui::PopItemWidth(); |
| 59 | + |
| 60 | + |
| 61 | + ImGui::Separator(); |
| 62 | + ImGui::BeginChild("Scrolling"); |
| 63 | + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 1)); |
| 64 | + if (copy) |
| 65 | + ImGui::LogToClipboard(); |
| 66 | + |
| 67 | + ImGui::PushFont(textFont); |
| 68 | + for (size_t i = 0; i < buffer.size(); i++) |
| 69 | + { |
| 70 | + if ((m_selectedFilter == 0) && (buffer[i].first == LogLevel::DEBUG)) |
| 71 | + ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.4f, 1.0f), buffer[i].second.c_str()); |
| 72 | + else if ((m_selectedFilter <= 1) && (buffer[i].first == LogLevel::INFO)) |
| 73 | + ImGui::TextColored(ImVec4(1.0f, 1.0f, 1.0f, 1.0f), buffer[i].second.c_str()); |
| 74 | + else if ((m_selectedFilter <= 2) && (buffer[i].first == LogLevel::WARN)) |
| 75 | + ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), buffer[i].second.c_str()); |
| 76 | + else if ((m_selectedFilter <= 3) && (buffer[i].first == LogLevel::ERR)) |
| 77 | + ImGui::TextColored(ImVec4(1.0f, 0.5f, 0.5f, 1.0f), buffer[i].second.c_str()); |
| 78 | + } |
| 79 | + ImGui::PopFont(); |
| 80 | + // if there are new lines, scroll to bottom |
| 81 | + if (buffer.size() > m_lastSize) |
| 82 | + { |
| 83 | + m_lastSize = buffer.size(); |
| 84 | + ImGui::SetScrollHereY(1.0f); |
| 85 | + } |
| 86 | + ImGui::PopStyleVar(); |
| 87 | + ImGui::EndChild(); |
| 88 | + ImGui::PopStyleColor(1); |
| 89 | + ImGui::End(); |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + auto& sinks = Utilities::logger.getSinks(); |
| 94 | + for (auto it = sinks.begin(); it != sinks.end(); it++) |
| 95 | + { |
| 96 | + m_bufferSink = dynamic_pointer_cast<BufferSink>(*it); |
| 97 | + if (m_bufferSink != 0) |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments