From 32d89d8f77c59785f75806143ad64de3df311ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20W=C3=BCbbers?= Date: Mon, 28 Apr 2025 08:23:16 +0200 Subject: [PATCH] logging improvements --- src/game/Log.cpp | 5 ++++- src/game/Log.h | 1 + src/game/Tools.cpp | 35 ++++++++++++++++++++++++++++------- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/game/Log.cpp b/src/game/Log.cpp index 8ad286d..a96be78 100644 --- a/src/game/Log.cpp +++ b/src/game/Log.cpp @@ -17,6 +17,7 @@ namespace { char LineBuffer[LogInternal::MaxLineSize]{0}; char OutBuffer[LogInternal::MaxLineSize]{0}; + char OutBufferUI[LogInternal::MaxLineSize]{0}; bx::HandleHashMapT<1024> OnceMap; LogHistory History; @@ -36,11 +37,13 @@ void Log(ELogType logType, const char* file, uint32_t line, const char* format, bx::snprintf(LineBuffer, sizeof(LineBuffer), LineFormat, format); bx::vprintf(LineBuffer, args); bx::vsnprintf(OutBuffer, sizeof(OutBuffer), LineBuffer, args); + bx::vsnprintf(OutBufferUI, sizeof(OutBufferUI), format, args); va_end(args); OutputDebugStringA(OutBuffer); - bx::strCopy(&History.LogBuffer[History.WriteIdx * LogInternal::MaxLineSize], LogInternal::MaxLineSize, OutBuffer); + bx::strCopy(&History.LogBuffer[History.WriteIdx * LogInternal::MaxLineSize], LogInternal::MaxLineSize, OutBufferUI); History.WriteTime[History.WriteIdx] = bx::getHPCounter(); + History.WriteType[History.WriteIdx] = logType; bx::strCopy(&History.FileBuffer[History.WriteIdx * LogInternal::MaxLineSize], LogInternal::MaxLineSize, file); History.LineBuffer[History.WriteIdx] = line; diff --git a/src/game/Log.h b/src/game/Log.h index f670ea7..89f2993 100644 --- a/src/game/Log.h +++ b/src/game/Log.h @@ -40,6 +40,7 @@ struct LogHistory uint32_t LineBuffer[LogInternal::LogHistorySize]{0}; int32_t WriteIdx = 0; int64_t WriteTime[LogInternal::LogHistorySize]{0}; + ELogType WriteType[LogInternal::LogHistorySize]{ELogType::Log}; }; void Log(ELogType logType, const char* file, uint32_t line, const char* format, ...); diff --git a/src/game/Tools.cpp b/src/game/Tools.cpp index f4a8f5b..fb89c01 100644 --- a/src/game/Tools.cpp +++ b/src/game/Tools.cpp @@ -2,6 +2,7 @@ #include "Gen.h" #include "Global.h" #include "Instance.h" +#include "Log.h" #include "Mesh.h" #include "Puzzle.h" #include "Tools.h" @@ -153,27 +154,45 @@ namespace Tools if (ImGui::Begin("Log")) { ImGui::Checkbox("Shorten File Names", &debug.ShortenLogFileNames); - ImGui::BeginTable( - "tbl", 4, ImGuiTableFlags_Resizable | ImGuiTableFlags_Hideable | ImGuiTableFlags_SizingFixedFit); + ImGui::BeginTable("tbl", + 4, + ImGuiTableFlags_Resizable | ImGuiTableFlags_Hideable | + ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_RowBg); ImGui::TableSetupColumn("Time", ImGuiTableColumnFlags_NoResize); ImGui::TableSetupColumn("Log"); ImGui::TableSetupColumn("Line", ImGuiTableColumnFlags_NoResize); ImGui::TableSetupColumn("File", ImGuiTableColumnFlags_NoResize); ImGui::TableHeadersRow(); + + auto& logs = GetLogHistory(); for (int32_t i = 0; i < bx::min(100, LogInternal::LogHistorySize); ++i) { - int32_t idx = GetLogHistory().WriteIdx - i - 1; + int32_t idx = logs.WriteIdx - i - 1; if (idx < 0) idx += LogInternal::LogHistorySize; - const char* line = &GetLogHistory().LogBuffer[idx * LogInternal::MaxLineSize]; + const char* line = &logs.LogBuffer[idx * LogInternal::MaxLineSize]; if (line[0] != 0) { - int64_t timeOffset = GetLogHistory().WriteTime[idx] - time.StartTime; + int64_t timeOffset = logs.WriteTime[idx] - time.StartTime; double writeTime = (double)timeOffset / bx::getHPFrequency(); - uint32_t fileLine = GetLogHistory().LineBuffer[idx]; - const char* filePath = &GetLogHistory().FileBuffer[idx * LogInternal::MaxLineSize]; + uint32_t fileLine = logs.LineBuffer[idx]; + const char* filePath = &logs.FileBuffer[idx * LogInternal::MaxLineSize]; const char* filePathRes = debug.ShortenLogFileNames ? bx::FilePath{filePath}.getFileName().getPtr() : filePath; + // ImVec4 texCol = {1.0f, 1.0f, 1.0f, 1.0f}; + // if (logs.WriteType[idx] == ELogType::Warn) + // texCol = {1.0f, 1.0f, 0.8f, 1.0f}; + // else if (logs.WriteType[idx] == ELogType::Error) + // texCol = {1.0f, 0.8f, 0.8f, 1.0f}; + // ImGui::PushStyleColor(ImGuiCol_Text, texCol); + ImVec4 bgCol = {0.0f, 0.0f, 0.0f, 1.0f}; + if (logs.WriteType[idx] == ELogType::Warn) + bgCol = {0.2f, 0.2f, 0.0f, 1.0f}; + else if (logs.WriteType[idx] == ELogType::Error) + bgCol = {0.2f, 0.0f, 0.0f, 1.0f}; + ImGui::PushStyleColor(ImGuiCol_TableRowBg, bgCol); + ImGui::PushStyleColor(ImGuiCol_TableRowBgAlt, bgCol); + ImGui::TableNextRow(); ImGui::TableNextColumn(); ImGui::Text("%.01f", writeTime); @@ -187,6 +206,8 @@ namespace Tools ImGui::TableNextColumn(); ImGui::Text("%s", filePathRes); + + ImGui::PopStyleColor(2); } } ImGui::EndTable();