show log in game

This commit is contained in:
Asuro
2025-03-27 17:36:55 +01:00
parent 24a724a021
commit 2ff08d7258
3 changed files with 47 additions and 2 deletions

View File

@@ -1,10 +1,13 @@
#include "Log.h"
#include "bx/handlealloc.h"
#include "bx/hash.h"
#include "bx/timer.h"
#include <bx/string.h>
#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;
}

View File

@@ -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();

View File

@@ -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)