Files
XCEngine/editor/src/panels/ConsolePanel.cpp

116 lines
3.2 KiB
C++
Raw Normal View History

#include "ConsolePanel.h"
#include "Core/EditorConsoleSink.h"
#include "UI/Core.h"
#include "UI/PanelChrome.h"
#include <XCEngine/Debug/LogCategory.h>
#include <XCEngine/Debug/Logger.h>
#include <imgui.h>
namespace XCEngine {
namespace Editor {
namespace {
constexpr float kConsoleToolbarHeight = 34.0f;
} // namespace
ConsolePanel::ConsolePanel() : Panel("Console") {
}
void ConsolePanel::Render() {
UI::PanelWindowScope panel(m_name.c_str());
if (!panel.IsOpen()) {
return;
}
auto* sink = Debug::EditorConsoleSink::GetInstance();
{
UI::PanelToolbarScope toolbar("ConsoleToolbar", kConsoleToolbarHeight);
if (toolbar.IsOpen()) {
if (UI::ToolbarButton("Clear")) {
sink->Clear();
}
ImGui::SameLine();
ImGui::TextDisabled("Filter");
ImGui::SameLine();
if (UI::ToolbarButton("Info", m_showInfo)) {
m_showInfo = !m_showInfo;
}
ImGui::SameLine();
if (UI::ToolbarButton("Warn", m_showWarning)) {
m_showWarning = !m_showWarning;
}
ImGui::SameLine();
if (UI::ToolbarButton("Error", m_showError)) {
m_showError = !m_showError;
}
}
}
UI::PanelContentScope content("LogScroll", UI::DefaultPanelContentPadding(), ImGuiWindowFlags_HorizontalScrollbar);
if (!content.IsOpen()) {
return;
}
const auto logs = sink->GetLogs();
size_t logIndex = 0;
for (const auto& log : logs) {
bool shouldShow = false;
switch (log.level) {
case ::XCEngine::Debug::LogLevel::Info:
case ::XCEngine::Debug::LogLevel::Verbose:
case ::XCEngine::Debug::LogLevel::Debug:
shouldShow = m_showInfo;
break;
case ::XCEngine::Debug::LogLevel::Warning:
shouldShow = m_showWarning;
break;
case ::XCEngine::Debug::LogLevel::Error:
case ::XCEngine::Debug::LogLevel::Fatal:
shouldShow = m_showError;
break;
}
if (!shouldShow) {
continue;
}
const char* prefix;
switch (log.level) {
case ::XCEngine::Debug::LogLevel::Verbose:
case ::XCEngine::Debug::LogLevel::Debug:
case ::XCEngine::Debug::LogLevel::Info:
prefix = "[INFO] ";
break;
case ::XCEngine::Debug::LogLevel::Warning:
prefix = "[WARN] ";
break;
case ::XCEngine::Debug::LogLevel::Error:
case ::XCEngine::Debug::LogLevel::Fatal:
prefix = "[ERROR] ";
break;
}
ImGui::PushID(static_cast<int>(logIndex));
const char* category = ::XCEngine::Debug::LogCategoryToString(log.category);
std::string fullMessage = std::string(prefix) + "[" + category + "] " + log.message.CStr();
ImGui::TextUnformatted(fullMessage.c_str());
if (ImGui::IsItemClicked()) {
ImGui::SetClipboardText(fullMessage.c_str());
}
ImGui::PopID();
logIndex++;
}
}
}
}