feat: Replace trace logging with new logging system and remove trace.hpp

This commit is contained in:
2026-01-04 00:39:49 +00:00
parent deea1cc19e
commit 80609fcf22
12 changed files with 30 additions and 64 deletions

View File

@@ -1,5 +1,5 @@
#include "app/sdl3_app.hpp"
#include "app/trace.hpp"
#include "logging/logger.hpp"
#include <cstring>
#include <stdexcept>

View File

@@ -1,5 +1,5 @@
#include "app/sdl3_app.hpp"
#include "app/trace.hpp"
#include "logging/logger.hpp"
#include <stdexcept>

View File

@@ -1,6 +1,5 @@
#include "app/audio_player.hpp"
#include "app/sdl3_app.hpp"
#include "app/trace.hpp"
#include "logging/logger.hpp"
#include <atomic>

View File

@@ -1,5 +1,4 @@
#include "app/sdl3_app.hpp"
#include "app/trace.hpp"
#include "logging/logger.hpp"
#include <set>

View File

@@ -1,5 +1,5 @@
#include "app/sdl3_app.hpp"
#include "app/trace.hpp"
#include "logging/logger.hpp"
#include <array>
#include <stdexcept>

View File

@@ -1,5 +1,4 @@
#include "app/sdl3_app.hpp"
#include "app/trace.hpp"
#include "logging/logger.hpp"
#include <limits>

View File

@@ -1,5 +1,4 @@
#include "app/sdl3_app.hpp"
#include "app/trace.hpp"
#include "app/vulkan_api.hpp"
#include "logging/logger.hpp"

View File

@@ -1,55 +0,0 @@
#ifndef SDL3CPP_APP_TRACE_HPP
#define SDL3CPP_APP_TRACE_HPP
#include <atomic>
#include <iostream>
#include <sstream>
namespace sdl3cpp::app {
class TraceLogger {
public:
static void SetEnabled(bool enabled) noexcept {
enabled_.store(enabled, std::memory_order_relaxed);
}
static bool Enabled() noexcept {
return enabled_.load(std::memory_order_relaxed);
}
static void Log(const char* message) {
if (Enabled()) {
std::cout << "[TRACE] " << message << '\n';
}
}
template <typename T>
static void LogVariable(const char* name, const T& value) {
if (!Enabled()) {
return;
}
std::ostringstream oss;
oss << "[TRACE] " << name << " = " << value;
std::cout << oss.str() << '\n';
}
private:
static inline std::atomic_bool enabled_{false};
};
class TraceScope {
public:
explicit TraceScope(const char* name) : name_(name) {
TraceLogger::Log(name_);
}
private:
const char* name_;
};
} // namespace sdl3cpp::app
#define TRACE_FUNCTION() sdl3cpp::app::TraceScope traceScope##__COUNTER__{__func__}
#define TRACE_VAR(var) sdl3cpp::app::TraceLogger::LogVariable(#var, var)
#endif // SDL3CPP_APP_TRACE_HPP

View File

@@ -40,6 +40,22 @@ public:
void Warn(const std::string& message) { Log(LogLevel::WARN, message); }
void Error(const std::string& message) { Log(LogLevel::ERROR, message); }
// Tracing methods
void TraceFunction(const std::string& funcName) {
if (GetLevel() <= LogLevel::TRACE) {
Trace(std::string("Entering ") + funcName);
}
}
template <typename T>
void TraceVariable(const std::string& name, const T& value) {
if (GetLevel() <= LogLevel::TRACE) {
std::ostringstream oss;
oss << name << " = " << value;
Trace(oss.str());
}
}
private:
Logger();
~Logger();
@@ -64,6 +80,9 @@ private:
#define LOG_WARN(msg) sdl3cpp::logging::Logger::GetInstance().Warn(msg)
#define LOG_ERROR(msg) sdl3cpp::logging::Logger::GetInstance().Error(msg)
#define TRACE_FUNCTION() sdl3cpp::logging::Logger::GetInstance().TraceFunction(__func__)
#define TRACE_VAR(var) sdl3cpp::logging::Logger::GetInstance().TraceVariable(#var, var)
} // namespace sdl3cpp::logging
#endif // SDL3CPP_LOGGING_LOGGER_HPP

View File

@@ -18,7 +18,6 @@
#include <string>
#include <utility>
#include "app/trace.hpp"
#include "app/sdl3_app.hpp"
#include <SDL3/SDL_main.h>
#include "logging/logger.hpp"
@@ -315,7 +314,6 @@ int main(int argc, char** argv) {
SetupSignalHandlers();
try {
AppOptions options = ParseCommandLine(argc, argv);
sdl3cpp::app::TraceLogger::SetEnabled(options.traceEnabled);
// Initialize logger
auto& logger = sdl3cpp::logging::Logger::GetInstance();
if (options.traceEnabled) {
@@ -324,6 +322,7 @@ int main(int argc, char** argv) {
logger.SetLevel(sdl3cpp::logging::LogLevel::INFO);
}
logger.EnableConsoleOutput(true);
logger.SetOutputFile("sdl3_app.log");
LOG_INFO("Application starting");
LogRuntimeConfig(options.runtimeConfig);
if (options.seedOutput) {

View File

@@ -2,6 +2,7 @@
#include "script/script_engine.hpp"
#include "script/lua_helpers.hpp"
#include "script/mesh_loader.hpp"
#include "logging/logger.hpp"
#include <btBulletDynamicsCommon.h>
#include <lua.hpp>
@@ -9,6 +10,7 @@
namespace sdl3cpp::script {
void LuaBindings::RegisterBindings(lua_State* L, ScriptEngine* engine) {
TRACE_FUNCTION();
lua_pushlightuserdata(L, engine);
lua_pushcclosure(L, &LoadMeshFromFile, 1);
lua_setglobal(L, "load_mesh_from_file");
@@ -39,8 +41,10 @@ void LuaBindings::RegisterBindings(lua_State* L, ScriptEngine* engine) {
}
int LuaBindings::LoadMeshFromFile(lua_State* L) {
TRACE_FUNCTION();
auto* engine = static_cast<ScriptEngine*>(lua_touserdata(L, lua_upvalueindex(1)));
const char* path = luaL_checkstring(L, 1);
TRACE_VAR(path);
MeshPayload payload;
std::string error;
@@ -56,8 +60,10 @@ int LuaBindings::LoadMeshFromFile(lua_State* L) {
}
int LuaBindings::PhysicsCreateBox(lua_State* L) {
TRACE_FUNCTION();
auto* engine = static_cast<ScriptEngine*>(lua_touserdata(L, lua_upvalueindex(1)));
const char* name = luaL_checkstring(L, 1);
TRACE_VAR(name);
if (!lua_istable(L, 2) || !lua_istable(L, 4) || !lua_istable(L, 5)) {
luaL_error(L, "physics_create_box expects vector tables for half extents, origin, and rotation");

View File

@@ -25,6 +25,7 @@ ScriptEngine::ScriptEngine(const std::filesystem::path& scriptPath, bool debugEn
shaderManager_(std::make_unique<ShaderManager>(L_)),
guiManager_(std::make_unique<GuiManager>(L_)),
audioManager_(std::make_unique<AudioManager>(scriptDirectory_)) {
TRACE_FUNCTION();
if (!L_) {
LOG_ERROR("Failed to create Lua state");
throw std::runtime_error("Failed to create Lua state");