1
0
mirror of synced 2024-12-15 01:01:15 +01:00
ImHex/plugins/builtin/source/content/views/view_logs.cpp

74 lines
2.9 KiB
C++
Raw Normal View History

2023-07-23 23:37:47 +02:00
#include "content/views/view_logs.hpp"
2023-11-10 20:47:08 +01:00
#include <hex/api/content_registry.hpp>
2023-07-23 23:37:47 +02:00
#include <hex/helpers/logger.hpp>
#include <fonts/codicons_font.h>
2023-07-23 23:37:47 +02:00
namespace hex::plugin::builtin {
ViewLogs::ViewLogs() : View::Floating("hex.builtin.view.logs.name") {
2024-01-08 21:51:48 +01:00
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.extras", "hex.builtin.view.logs.name" }, ICON_VS_BRACKET_ERROR, 2500, Shortcut::None, [&, this] {
2023-07-23 23:37:47 +02:00
this->getWindowOpenState() = true;
});
}
static ImColor getColor(std::string_view level) {
if (level.contains("DEBUG"))
return ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerDebug);
2023-07-23 23:37:47 +02:00
else if (level.contains("INFO"))
return ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerInfo);
2023-07-23 23:37:47 +02:00
else if (level.contains("WARN"))
return ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerWarning);
2023-07-23 23:37:47 +02:00
else if (level.contains("ERROR"))
return ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerError);
2023-07-23 23:37:47 +02:00
else if (level.contains("FATAL"))
return ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerFatal);
2023-07-23 23:37:47 +02:00
return ImGui::GetStyleColorVec4(ImGuiCol_Text);
}
static bool shouldDisplay(std::string_view messageLevel, int currLevel) {
if (messageLevel.contains("[DEBUG]"))
return currLevel <= 0;
else if (messageLevel.contains("[INFO]"))
return currLevel <= 1;
else if (messageLevel.contains("[WARN]"))
return currLevel <= 2;
else if (messageLevel.contains("[ERROR]"))
return currLevel <= 3;
else if (messageLevel.contains("[FATAL]"))
return currLevel <= 4;
else
return false;
2023-07-23 23:37:47 +02:00
}
void ViewLogs::drawContent() {
2023-12-19 13:10:25 +01:00
ImGui::Combo("hex.builtin.view.logs.log_level"_lang, &m_logLevel, "DEBUG\0INFO\0WARNING\0ERROR\0FATAL\0");
2023-07-23 23:37:47 +02:00
if (ImGui::BeginTable("##logs", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_ScrollY)) {
ImGui::TableSetupColumn("hex.builtin.view.logs.component"_lang, ImGuiTableColumnFlags_WidthFixed, 100_scaled);
ImGui::TableSetupColumn("hex.builtin.view.logs.message"_lang);
ImGui::TableSetupScrollFreeze(0, 1);
2023-07-23 23:37:47 +02:00
ImGui::TableHeadersRow();
2023-07-23 23:37:47 +02:00
const auto &logs = log::impl::getLogEntries();
for (const auto &log : logs | std::views::reverse) {
if (!shouldDisplay(log.level, m_logLevel)) {
continue;
}
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextUnformatted(log.project.c_str());
ImGui::TableNextColumn();
ImGui::PushStyleColor(ImGuiCol_Text, getColor(log.level).Value);
ImGui::TextUnformatted(log.message.c_str());
ImGui::PopStyleColor();
2023-07-23 23:37:47 +02:00
}
ImGui::EndTable();
}
2023-07-23 23:37:47 +02:00
}
}