From 2ff08d7258c3e313190b2dfb45094fdcba064f0e Mon Sep 17 00:00:00 2001 From: Asuro Date: Thu, 27 Mar 2025 17:36:55 +0100 Subject: [PATCH] show log in game --- src/game/Log.cpp | 19 +++++++++++++++++-- src/game/Log.h | 14 ++++++++++++++ src/game/rendering/Rendering.cpp | 16 ++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/game/Log.cpp b/src/game/Log.cpp index a77b8e9..2603f8d 100644 --- a/src/game/Log.cpp +++ b/src/game/Log.cpp @@ -1,10 +1,13 @@ #include "Log.h" #include "bx/handlealloc.h" #include "bx/hash.h" +#include "bx/timer.h" #include #define WIN32_LEAN_AND_MEAN #include "Windows.h" +#include "Instance.h" + #define ESC "\x1B[" #define YELLOW ESC "33m" #define RED ESC "31m" @@ -12,9 +15,11 @@ namespace { - char LineBuffer[1024]{0}; - char OutBuffer[1024]{0}; + char LineBuffer[LogInternal::MaxLineSize]{0}; + char OutBuffer[LogInternal::MaxLineSize]{0}; bx::HandleHashMapT<1024> OnceMap; + LogHistory History; + constexpr char LogFormat[]{"%s\n"}; constexpr char WarnFormat[]{YELLOW "%s" END "\n"}; constexpr char ErrorFormat[]{RED "%s" END "\n"}; @@ -33,6 +38,11 @@ void Log(ELogType logType, const char* file, uint32_t line, const char* format, bx::vsnprintf(OutBuffer, sizeof(OutBuffer), LineBuffer, args); va_end(args); OutputDebugStringA(OutBuffer); + + bx::strCopy(&History.LogBuffer[History.WriteIdx * LogInternal::MaxLineSize], LogInternal::MaxLineSize, OutBuffer); + History.WriteTime[History.WriteIdx] = bx::getHPCounter(); + ++History.WriteIdx; + if (History.WriteIdx >= LogInternal::LogHistorySize) History.WriteIdx = 0; } bool WasLogged(ELogType logType, const char* file, uint32_t line, const char* format) @@ -48,3 +58,8 @@ bool WasLogged(ELogType logType, const char* file, uint32_t line, const char* fo OnceMap.insert(hash, {}); return false; } + +LogHistory& GetLogHistory() +{ + return History; +} diff --git a/src/game/Log.h b/src/game/Log.h index 0c7332b..cc6daf9 100644 --- a/src/game/Log.h +++ b/src/game/Log.h @@ -27,5 +27,19 @@ enum class ELogType Error, }; +namespace LogInternal +{ + constexpr int32_t MaxLineSize = 1024; + constexpr int32_t LogHistorySize = 1024; +} // namespace LogInternal + +struct LogHistory +{ + char LogBuffer[LogInternal::MaxLineSize * LogInternal::LogHistorySize]{0}; + int32_t WriteIdx = 0; + int64_t WriteTime[LogInternal::LogHistorySize]{0}; +}; + void Log(ELogType logType, const char* file, uint32_t line, const char* format, ...); bool WasLogged(ELogType logType, const char* file, uint32_t line, const char* format); +LogHistory& GetLogHistory(); diff --git a/src/game/rendering/Rendering.cpp b/src/game/rendering/Rendering.cpp index 2957e64..65ca170 100644 --- a/src/game/rendering/Rendering.cpp +++ b/src/game/rendering/Rendering.cpp @@ -474,6 +474,22 @@ namespace Game debug.DebugCardRotation++; if (debug.DebugCardRotation >= 4) debug.DebugCardRotation = 0; } + if (ImGui::Begin("Log")) + { + for (int32_t i = 0; i < bx::min(100, LogInternal::LogHistorySize); ++i) + { + int32_t idx = GetLogHistory().WriteIdx - i - 1; + if (idx < 0) idx += LogInternal::LogHistorySize; + const char* line = &GetLogHistory().LogBuffer[idx * LogInternal::MaxLineSize]; + if (line[0] != 0) + { + int64_t timeOffset = GetLogHistory().WriteTime[idx] - GetInstance().Time.StartTime; + double writeTime = (double)timeOffset / bx::getHPFrequency(); + ImGui::Text("%.04f: %s", writeTime, line); + } + } + } + ImGui::End(); if (ImGui::Begin("Rendering")) { if (LastShaderLoadTime >= 0.0f)