diff --git a/src/app/sdl3_app_buffers.cpp b/src/app/sdl3_app_buffers.cpp index 277d3b4..8424b07 100644 --- a/src/app/sdl3_app_buffers.cpp +++ b/src/app/sdl3_app_buffers.cpp @@ -1,5 +1,5 @@ #include "app/sdl3_app.hpp" -#include "app/trace.hpp" +#include "logging/logger.hpp" #include #include diff --git a/src/app/sdl3_app_build.cpp b/src/app/sdl3_app_build.cpp index a626d4e..7326467 100644 --- a/src/app/sdl3_app_build.cpp +++ b/src/app/sdl3_app_build.cpp @@ -1,5 +1,5 @@ #include "app/sdl3_app.hpp" -#include "app/trace.hpp" +#include "logging/logger.hpp" #include diff --git a/src/app/sdl3_app_core.cpp b/src/app/sdl3_app_core.cpp index d231436..a766f28 100644 --- a/src/app/sdl3_app_core.cpp +++ b/src/app/sdl3_app_core.cpp @@ -1,6 +1,5 @@ #include "app/audio_player.hpp" #include "app/sdl3_app.hpp" -#include "app/trace.hpp" #include "logging/logger.hpp" #include diff --git a/src/app/sdl3_app_device.cpp b/src/app/sdl3_app_device.cpp index 6d7deca..1b34b7c 100644 --- a/src/app/sdl3_app_device.cpp +++ b/src/app/sdl3_app_device.cpp @@ -1,5 +1,4 @@ #include "app/sdl3_app.hpp" -#include "app/trace.hpp" #include "logging/logger.hpp" #include diff --git a/src/app/sdl3_app_pipeline.cpp b/src/app/sdl3_app_pipeline.cpp index 0c00456..c046af7 100644 --- a/src/app/sdl3_app_pipeline.cpp +++ b/src/app/sdl3_app_pipeline.cpp @@ -1,5 +1,5 @@ #include "app/sdl3_app.hpp" -#include "app/trace.hpp" +#include "logging/logger.hpp" #include #include diff --git a/src/app/sdl3_app_render.cpp b/src/app/sdl3_app_render.cpp index 1195382..a4e00f6 100644 --- a/src/app/sdl3_app_render.cpp +++ b/src/app/sdl3_app_render.cpp @@ -1,5 +1,4 @@ #include "app/sdl3_app.hpp" -#include "app/trace.hpp" #include "logging/logger.hpp" #include diff --git a/src/app/sdl3_app_swapchain.cpp b/src/app/sdl3_app_swapchain.cpp index 3d3e182..fcd4711 100644 --- a/src/app/sdl3_app_swapchain.cpp +++ b/src/app/sdl3_app_swapchain.cpp @@ -1,5 +1,4 @@ #include "app/sdl3_app.hpp" -#include "app/trace.hpp" #include "app/vulkan_api.hpp" #include "logging/logger.hpp" diff --git a/src/app/trace.hpp b/src/app/trace.hpp deleted file mode 100644 index a66b7f0..0000000 --- a/src/app/trace.hpp +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef SDL3CPP_APP_TRACE_HPP -#define SDL3CPP_APP_TRACE_HPP - -#include -#include -#include - -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 - 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 diff --git a/src/logging/logger.hpp b/src/logging/logger.hpp index 4c56abc..48aa44b 100644 --- a/src/logging/logger.hpp +++ b/src/logging/logger.hpp @@ -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 + 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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 072333a..95ab345 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,6 @@ #include #include -#include "app/trace.hpp" #include "app/sdl3_app.hpp" #include #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) { diff --git a/src/script/lua_bindings.cpp b/src/script/lua_bindings.cpp index 1cbcefb..4e15f2b 100644 --- a/src/script/lua_bindings.cpp +++ b/src/script/lua_bindings.cpp @@ -2,6 +2,7 @@ #include "script/script_engine.hpp" #include "script/lua_helpers.hpp" #include "script/mesh_loader.hpp" +#include "logging/logger.hpp" #include #include @@ -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(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(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"); diff --git a/src/script/script_engine.cpp b/src/script/script_engine.cpp index e630a4f..e108806 100644 --- a/src/script/script_engine.cpp +++ b/src/script/script_engine.cpp @@ -25,6 +25,7 @@ ScriptEngine::ScriptEngine(const std::filesystem::path& scriptPath, bool debugEn shaderManager_(std::make_unique(L_)), guiManager_(std::make_unique(L_)), audioManager_(std::make_unique(scriptDirectory_)) { + TRACE_FUNCTION(); if (!L_) { LOG_ERROR("Failed to create Lua state"); throw std::runtime_error("Failed to create Lua state");