diff --git a/CMakeLists.txt b/CMakeLists.txt index c345b6a..92c87a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,6 +103,7 @@ if(BUILD_SDL3_APP) add_executable(sdl3_app src/main.cpp src/logging/logger.cpp + src/core/platform.cpp src/app/sdl3_app_core.cpp src/app/audio_player.cpp src/app/sdl3_app_device.cpp @@ -145,6 +146,7 @@ enable_testing() add_executable(script_engine_tests tests/test_cube_script.cpp src/logging/logger.cpp + src/core/platform.cpp src/script/script_engine.cpp src/script/physics_bridge.cpp src/script/scene_manager.cpp diff --git a/scripts/add_traces.sh b/scripts/add_traces.sh new file mode 100755 index 0000000..f182f5d --- /dev/null +++ b/scripts/add_traces.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Script to add logging includes to files that need them + +FILES=( + "src/script/lua_helpers.cpp" + "src/script/mesh_loader.cpp" + "src/app/vulkan_api.cpp" + "src/script/physics_bridge.cpp" + "src/script/scene_manager.cpp" + "src/script/shader_manager.cpp" + "src/script/audio_manager.cpp" + "src/script/gui_manager.cpp" + "src/gui/gui_renderer.cpp" + "src/script/lua_bindings.cpp" +) + +for file in "${FILES[@]}"; do + # Check if file already includes logger.hpp + if ! grep -q "#include.*logging/logger.hpp" "$file"; then + echo "Adding logger include to $file" + # Add include after the first #include line + sed -i '0,/#include/s|#include.*|&\n#include "logging/logger.hpp"|' "$file" + else + echo "Skipping $file - already includes logger" + fi +done + +echo "Done adding logger includes" diff --git a/src/app/audio_player.cpp b/src/app/audio_player.cpp index 4f92539..7149d0b 100644 --- a/src/app/audio_player.cpp +++ b/src/app/audio_player.cpp @@ -1,4 +1,5 @@ #include "app/audio_player.hpp" +#include "logging/logger.hpp" #include #include @@ -22,6 +23,7 @@ struct DecodedAudio { }; DecodedAudio DecodeOgg(const std::filesystem::path& path) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(path.string()); FILE* file = std::fopen(path.string().c_str(), "rb"); if (!file) { throw std::runtime_error("Failed to open audio file: " + path.string()); @@ -82,6 +84,7 @@ AudioPlayer::~AudioPlayer() { } void AudioPlayer::PlayBackground(const std::filesystem::path& path, bool loop) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(path.string(), loop); DecodedAudio clip = DecodeOgg(path); EnsureStream(clip.sampleRate, clip.channels); std::scoped_lock lock(voicesMutex_); @@ -89,6 +92,7 @@ void AudioPlayer::PlayBackground(const std::filesystem::path& path, bool loop) { } void AudioPlayer::PlayEffect(const std::filesystem::path& path, bool loop) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(path.string(), loop); DecodedAudio clip = DecodeOgg(path); EnsureStream(clip.sampleRate, clip.channels); std::scoped_lock lock(voicesMutex_); @@ -101,6 +105,7 @@ void AudioPlayer::AudioStreamCallback(void* userdata, SDL_AudioStream* stream, i } void AudioPlayer::FeedStream(SDL_AudioStream* stream, int totalAmount) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(static_cast(stream), totalAmount); if (totalAmount <= 0 || !stream_) { return; } @@ -142,6 +147,7 @@ void AudioPlayer::FeedStream(SDL_AudioStream* stream, int totalAmount) { } void AudioPlayer::EnsureStream(int sampleRate, int channels) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(sampleRate, channels); if (sampleRate <= 0 || channels <= 0) { throw std::runtime_error("Audio format is invalid"); } @@ -172,6 +178,7 @@ void AudioPlayer::EnsureStream(int sampleRate, int channels) { } void AudioPlayer::AddVoiceSamples(AudioVoice& voice, std::vector& mixBuffer, size_t sampleCount) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(voice.data.size(), mixBuffer.size(), sampleCount); if (voice.data.empty()) { voice.active = false; return; diff --git a/src/app/sdl3_app_buffers.cpp b/src/app/sdl3_app_buffers.cpp index 1053707..c685e64 100644 --- a/src/app/sdl3_app_buffers.cpp +++ b/src/app/sdl3_app_buffers.cpp @@ -9,7 +9,7 @@ namespace sdl3cpp::app { void Sdl3App::LoadSceneData() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__); + sdl3cpp::logging::TraceGuard trace; shaderPathMap_ = scriptEngine_.LoadShaderPathsMap(); if (shaderPathMap_.empty()) { throw std::runtime_error("Lua script did not provide shader paths"); @@ -54,7 +54,7 @@ void Sdl3App::LoadSceneData() { } void Sdl3App::CreateVertexBuffer() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__); + sdl3cpp::logging::TraceGuard trace; if (vertices_.empty()) { throw std::runtime_error("Cannot create vertex buffer: no vertices loaded"); } @@ -75,7 +75,7 @@ void Sdl3App::CreateVertexBuffer() { } void Sdl3App::CreateIndexBuffer() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__); + sdl3cpp::logging::TraceGuard trace; if (indices_.empty()) { throw std::runtime_error("Cannot create index buffer: no indices loaded"); } diff --git a/src/app/sdl3_app_build.cpp b/src/app/sdl3_app_build.cpp index a45f587..9367ad6 100644 --- a/src/app/sdl3_app_build.cpp +++ b/src/app/sdl3_app_build.cpp @@ -6,7 +6,7 @@ namespace sdl3cpp::app { void Sdl3App::CreateFramebuffers() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; swapChainFramebuffers_.resize(swapChainImageViews_.size()); for (size_t i = 0; i < swapChainImageViews_.size(); ++i) { VkImageView attachments[] = {swapChainImageViews_[i]}; @@ -28,7 +28,7 @@ void Sdl3App::CreateFramebuffers() { } void Sdl3App::CreateCommandPool() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; QueueFamilyIndices indices = FindQueueFamilies(physicalDevice_); VkCommandPoolCreateInfo poolInfo{}; @@ -42,7 +42,7 @@ void Sdl3App::CreateCommandPool() { } void Sdl3App::CreateSyncObjects() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; VkSemaphoreCreateInfo semaphoreInfo{}; semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; diff --git a/src/app/sdl3_app_core.cpp b/src/app/sdl3_app_core.cpp index 7448d91..fd000fb 100644 --- a/src/app/sdl3_app_core.cpp +++ b/src/app/sdl3_app_core.cpp @@ -1,6 +1,7 @@ #include "app/audio_player.hpp" #include "app/sdl3_app.hpp" #include "logging/logger.hpp" +#include "core/platform.hpp" #include #include @@ -10,16 +11,12 @@ #include #include -#ifdef _WIN32 -# include -#endif - namespace sdl3cpp::app { extern std::atomic g_signalReceived; std::vector ReadFile(const std::string& path) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; // Validate file exists before attempting to open if (!std::filesystem::exists(path)) { @@ -51,34 +48,6 @@ std::vector ReadFile(const std::string& path) { namespace { -#ifdef _WIN32 -std::string FormatWin32Error(DWORD errorCode) { - if (errorCode == ERROR_SUCCESS) { - return "ERROR_SUCCESS"; - } - LPSTR buffer = nullptr; - DWORD length = FormatMessageA( - FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - nullptr, - errorCode, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - reinterpret_cast(&buffer), - 0, - nullptr); - std::string message; - if (length > 0 && buffer) { - message.assign(buffer, length); - while (!message.empty() && (message.back() == '\r' || message.back() == '\n')) { - message.pop_back(); - } - LocalFree(buffer); - } else { - message = "Unknown Windows error"; - } - return message; -} -#endif - std::string BuildSdlErrorMessage(const char* context) { std::ostringstream oss; oss << context; @@ -88,12 +57,12 @@ std::string BuildSdlErrorMessage(const char* context) { } else { oss << ": (SDL_GetError returned an empty string)"; } -#ifdef _WIN32 - DWORD win32Error = ::GetLastError(); - if (win32Error != ERROR_SUCCESS) { - oss << " [Win32 error " << win32Error << ": " << FormatWin32Error(win32Error) << "]"; + + std::string platformError = sdl3cpp::platform::GetPlatformError(); + if (!platformError.empty() && platformError != "No platform error") { + oss << " [" << platformError << "]"; } -#endif + return oss.str(); } @@ -116,7 +85,7 @@ void ShowErrorDialog(const char* title, const std::string& message) { Sdl3App::Sdl3App(const std::filesystem::path& scriptPath, bool luaDebug) : scriptEngine_(scriptPath, luaDebug), scriptDirectory_(scriptPath.parent_path()) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; sdl3cpp::logging::Logger::GetInstance().TraceVariable("scriptPath", scriptPath); } @@ -125,7 +94,7 @@ bool Sdl3App::ShouldStop() { } void Sdl3App::Run() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; InitSDL(); InitVulkan(); MainLoop(); @@ -133,7 +102,7 @@ void Sdl3App::Run() { } void Sdl3App::InitSDL() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; sdl3cpp::logging::Logger::GetInstance().TraceVariable("kWidth", kWidth); sdl3cpp::logging::Logger::GetInstance().TraceVariable("kHeight", kHeight); @@ -171,7 +140,7 @@ void Sdl3App::InitSDL() { } void Sdl3App::InitVulkan() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; try { CreateInstance(); } catch (const std::exception& e) { @@ -222,7 +191,7 @@ void Sdl3App::InitVulkan() { } void Sdl3App::MainLoop() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; sdl3cpp::logging::Logger::GetInstance().TraceVariable("guiHasCommands_", guiHasCommands_); bool running = true; auto start = std::chrono::steady_clock::now(); @@ -285,7 +254,7 @@ void Sdl3App::MainLoop() { } void Sdl3App::Cleanup() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; CleanupSwapChain(); vkDestroyBuffer(device_, vertexBuffer_, nullptr); diff --git a/src/app/sdl3_app_device.cpp b/src/app/sdl3_app_device.cpp index 640f923..59d75ad 100644 --- a/src/app/sdl3_app_device.cpp +++ b/src/app/sdl3_app_device.cpp @@ -8,7 +8,7 @@ namespace sdl3cpp::app { void Sdl3App::CreateInstance() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; // Early validation: Check if Vulkan is available uint32_t apiVersion = 0; @@ -104,14 +104,14 @@ void Sdl3App::CreateInstance() { } void Sdl3App::CreateSurface() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; if (!SDL_Vulkan_CreateSurface(window_, instance_, nullptr, &surface_)) { throw std::runtime_error("Failed to create Vulkan surface"); } } void Sdl3App::PickPhysicalDevice() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; uint32_t deviceCount = 0; VkResult enumResult = vkEnumeratePhysicalDevices(instance_, &deviceCount, nullptr); if (enumResult != VK_SUCCESS) { @@ -172,7 +172,7 @@ void Sdl3App::PickPhysicalDevice() { } void Sdl3App::CreateLogicalDevice() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; QueueFamilyIndices indices = FindQueueFamilies(physicalDevice_); std::vector queueCreateInfos; @@ -209,7 +209,7 @@ void Sdl3App::CreateLogicalDevice() { } QueueFamilyIndices Sdl3App::FindQueueFamilies(VkPhysicalDevice device) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; QueueFamilyIndices indices; uint32_t queueFamilyCount = 0; @@ -241,7 +241,7 @@ QueueFamilyIndices Sdl3App::FindQueueFamilies(VkPhysicalDevice device) { } bool Sdl3App::CheckDeviceExtensionSupport(VkPhysicalDevice device) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; uint32_t extensionCount = 0; vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr); @@ -266,7 +266,7 @@ bool Sdl3App::CheckDeviceExtensionSupport(VkPhysicalDevice device) { } SwapChainSupportDetails Sdl3App::QuerySwapChainSupport(VkPhysicalDevice device) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; SwapChainSupportDetails details; vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface_, &details.capabilities); @@ -289,7 +289,7 @@ SwapChainSupportDetails Sdl3App::QuerySwapChainSupport(VkPhysicalDevice device) } bool Sdl3App::IsDeviceSuitable(VkPhysicalDevice device) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; QueueFamilyIndices indices = FindQueueFamilies(device); bool extensionsSupported = CheckDeviceExtensionSupport(device); diff --git a/src/app/sdl3_app_pipeline.cpp b/src/app/sdl3_app_pipeline.cpp index 2b6b7ac..1b0df18 100644 --- a/src/app/sdl3_app_pipeline.cpp +++ b/src/app/sdl3_app_pipeline.cpp @@ -7,7 +7,7 @@ namespace sdl3cpp::app { VkShaderModule Sdl3App::CreateShaderModule(const std::vector& code) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; VkShaderModuleCreateInfo createInfo{}; createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; createInfo.codeSize = code.size(); @@ -21,7 +21,7 @@ VkShaderModule Sdl3App::CreateShaderModule(const std::vector& code) { } void Sdl3App::CreateGraphicsPipeline() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; if (shaderPathMap_.empty()) { throw std::runtime_error("No shader paths were loaded before pipeline creation"); } diff --git a/src/app/sdl3_app_render.cpp b/src/app/sdl3_app_render.cpp index 611b3cd..6d1ff2d 100644 --- a/src/app/sdl3_app_render.cpp +++ b/src/app/sdl3_app_render.cpp @@ -132,7 +132,7 @@ void Sdl3App::PrintGpuDiagnostics(const std::string& errorContext) { } void Sdl3App::CreateCommandBuffers() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; commandBuffers_.resize(swapChainFramebuffers_.size()); VkCommandBufferAllocateInfo allocInfo{}; @@ -148,7 +148,7 @@ void Sdl3App::CreateCommandBuffers() { void Sdl3App::RecordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex, float time, const std::array& viewProj) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; sdl3cpp::logging::Logger::GetInstance().TraceVariable("imageIndex", imageIndex); VkCommandBufferBeginInfo beginInfo{}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; @@ -202,7 +202,7 @@ void Sdl3App::RecordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageI } void Sdl3App::ProcessGuiEvent(const SDL_Event& event) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; switch (event.type) { case SDL_EVENT_MOUSE_MOTION: guiInputSnapshot_.mouseX = static_cast(event.motion.x); @@ -235,7 +235,7 @@ void Sdl3App::ProcessGuiEvent(const SDL_Event& event) { } void Sdl3App::SetupGuiRenderer() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; guiHasCommands_ = scriptEngine_.HasGuiCommands(); if (!guiHasCommands_) { guiRenderer_.reset(); @@ -250,7 +250,7 @@ void Sdl3App::SetupGuiRenderer() { } void Sdl3App::DrawFrame(float time) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; sdl3cpp::logging::Logger::GetInstance().Debug("Drawing frame at time " + std::to_string(time)); // Use reasonable timeout instead of infinite wait (5 seconds) diff --git a/src/app/sdl3_app_swapchain.cpp b/src/app/sdl3_app_swapchain.cpp index ed88499..abbc57d 100644 --- a/src/app/sdl3_app_swapchain.cpp +++ b/src/app/sdl3_app_swapchain.cpp @@ -7,7 +7,7 @@ namespace sdl3cpp::app { void Sdl3App::CreateSwapChain() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; SwapChainSupportDetails support = QuerySwapChainSupport(physicalDevice_); // Validate swap chain support @@ -85,7 +85,7 @@ void Sdl3App::CreateSwapChain() { } void Sdl3App::CreateImageViews() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; swapChainImageViews_.resize(swapChainImages_.size()); for (size_t i = 0; i < swapChainImages_.size(); ++i) { VkImageViewCreateInfo viewInfo{}; @@ -108,7 +108,7 @@ void Sdl3App::CreateImageViews() { } void Sdl3App::CreateRenderPass() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; VkAttachmentDescription colorAttachment{}; colorAttachment.format = swapChainImageFormat_; colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT; @@ -151,7 +151,7 @@ void Sdl3App::CreateRenderPass() { } void Sdl3App::CleanupSwapChain() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; for (auto framebuffer : swapChainFramebuffers_) { vkDestroyFramebuffer(device_, framebuffer, nullptr); } @@ -173,7 +173,7 @@ void Sdl3App::CleanupSwapChain() { } void Sdl3App::RecreateSwapChain() { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; int width = 0; int height = 0; @@ -223,7 +223,7 @@ void Sdl3App::RecreateSwapChain() { } VkSurfaceFormatKHR Sdl3App::ChooseSwapSurfaceFormat(const std::vector& availableFormats) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; for (const auto& availableFormat : availableFormats) { if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { @@ -234,7 +234,7 @@ VkSurfaceFormatKHR Sdl3App::ChooseSwapSurfaceFormat(const std::vector& availablePresentModes) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; for (const auto& availablePresentMode : availablePresentModes) { if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) { return availablePresentMode; diff --git a/src/core/platform.cpp b/src/core/platform.cpp new file mode 100644 index 0000000..452e091 --- /dev/null +++ b/src/core/platform.cpp @@ -0,0 +1,76 @@ +#include "core/platform.hpp" +#include "logging/logger.hpp" + +#include + +#ifdef _WIN32 +#include +#endif + +namespace sdl3cpp::platform { + +std::optional GetUserConfigDirectory() { + sdl3cpp::logging::TraceGuard trace; +#ifdef _WIN32 + if (const char* appData = std::getenv("APPDATA")) { + return std::filesystem::path(appData) / "sdl3cpp"; + } +#else + if (const char* xdgConfig = std::getenv("XDG_CONFIG_HOME")) { + return std::filesystem::path(xdgConfig) / "sdl3cpp"; + } + if (const char* home = std::getenv("HOME")) { + return std::filesystem::path(home) / ".config" / "sdl3cpp"; + } +#endif + return std::nullopt; +} + +#ifdef _WIN32 +namespace { +std::string FormatWin32Error(DWORD errorCode) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(errorCode); + if (errorCode == ERROR_SUCCESS) { + return "ERROR_SUCCESS"; + } + LPSTR buffer = nullptr; + DWORD length = FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, + errorCode, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast(&buffer), + 0, + nullptr + ); + + std::string message; + if (length > 0 && buffer != nullptr) { + message = std::string(buffer, length); + // Remove trailing newlines + while (!message.empty() && (message.back() == '\n' || message.back() == '\r')) { + message.pop_back(); + } + LocalFree(buffer); + } else { + message = "(FormatMessage failed)"; + } + return message; +} +} // anonymous namespace +#endif + +std::string GetPlatformError() { + sdl3cpp::logging::TraceGuard trace; +#ifdef _WIN32 + DWORD win32Error = ::GetLastError(); + if (win32Error != ERROR_SUCCESS) { + return "Win32 error " + std::to_string(win32Error) + ": " + FormatWin32Error(win32Error); + } + return "No platform error"; +#else + return "No platform error"; +#endif +} + +} // namespace sdl3cpp::platform diff --git a/src/core/platform.hpp b/src/core/platform.hpp new file mode 100644 index 0000000..3c12e6c --- /dev/null +++ b/src/core/platform.hpp @@ -0,0 +1,27 @@ +#ifndef SDL3CPP_CORE_PLATFORM_HPP +#define SDL3CPP_CORE_PLATFORM_HPP + +#include +#include +#include + +namespace sdl3cpp::platform { + +// Platform detection +#ifdef _WIN32 + constexpr bool IsWindows = true; + constexpr bool IsUnix = false; +#else + constexpr bool IsWindows = false; + constexpr bool IsUnix = true; +#endif + +// Get user configuration directory (platform-specific) +std::optional GetUserConfigDirectory(); + +// Format platform-specific error messages +std::string GetPlatformError(); + +} // namespace sdl3cpp::platform + +#endif // SDL3CPP_CORE_PLATFORM_HPP diff --git a/src/gui/gui_renderer.cpp b/src/gui/gui_renderer.cpp index 38ef1b8..e07b536 100644 --- a/src/gui/gui_renderer.cpp +++ b/src/gui/gui_renderer.cpp @@ -358,7 +358,7 @@ GuiRenderer::GuiRenderer(VkDevice device, VkPhysicalDevice physicalDevice, VkFor swapchainFormat_(swapchainFormat), scriptDirectory_(scriptDirectory), canvas_(std::make_unique()) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__); + sdl3cpp::logging::TraceGuard trace; } GuiRenderer::~GuiRenderer() { diff --git a/src/logging/logger.hpp b/src/logging/logger.hpp index e803244..4709a31 100644 --- a/src/logging/logger.hpp +++ b/src/logging/logger.hpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace sdl3cpp::logging { @@ -57,10 +58,10 @@ public: } template - void TraceFunctionWithArgs(const std::string& funcName, const Args&... args) { + void TraceFunctionWithArgs(const Args&... args, const std::source_location& location = std::source_location::current()) { if (GetLevel() <= LogLevel::TRACE) { std::ostringstream oss; - oss << "Entering " << funcName << " with args: "; + oss << "Entering " << location.function_name() << " with args: "; ((oss << args << " "), ...); Trace(oss.str()); } @@ -86,7 +87,8 @@ private: class TraceGuard { public: - TraceGuard(const std::string& funcName) : funcName_(funcName) { + explicit TraceGuard(const std::source_location& location = std::source_location::current()) + : funcName_(location.function_name()) { Logger::GetInstance().Trace("Entering " + funcName_); } ~TraceGuard() { diff --git a/src/main.cpp b/src/main.cpp index c376e59..b88c142 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,7 @@ #include "app/sdl3_app.hpp" #include #include "logging/logger.hpp" +#include "core/platform.hpp" namespace sdl3cpp::app { std::atomic g_signalReceived{false}; @@ -71,7 +72,7 @@ RuntimeConfig GenerateDefaultRuntimeConfig(const char* argv0) { } RuntimeConfig LoadRuntimeConfigFromJson(const std::filesystem::path& configPath, bool dumpConfig) { - sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(__PRETTY_FUNCTION__, configPath.string(), dumpConfig); + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(configPath.string(), dumpConfig); std::ifstream configStream(configPath); if (!configStream) { throw std::runtime_error("Failed to open config file: " + configPath.string()); @@ -158,24 +159,8 @@ RuntimeConfig LoadRuntimeConfigFromJson(const std::filesystem::path& configPath, return config; } -std::optional GetUserConfigDirectory() { -#ifdef _WIN32 - if (const char* appData = std::getenv("APPDATA")) { - return std::filesystem::path(appData) / "sdl3cpp"; - } -#else - if (const char* xdgConfig = std::getenv("XDG_CONFIG_HOME")) { - return std::filesystem::path(xdgConfig) / "sdl3cpp"; - } - if (const char* home = std::getenv("HOME")) { - return std::filesystem::path(home) / ".config" / "sdl3cpp"; - } -#endif - return std::nullopt; -} - std::optional GetDefaultConfigPath() { - if (auto dir = GetUserConfigDirectory()) { + if (auto dir = sdl3cpp::platform::GetUserConfigDirectory()) { return *dir / "default_runtime.json"; } return std::nullopt; diff --git a/src/script/audio_manager.cpp b/src/script/audio_manager.cpp index 2134ca7..18bb8d7 100644 --- a/src/script/audio_manager.cpp +++ b/src/script/audio_manager.cpp @@ -11,7 +11,7 @@ namespace sdl3cpp::script { AudioManager::AudioManager(const std::filesystem::path& scriptDirectory) : scriptDirectory_(scriptDirectory) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__); + sdl3cpp::logging::TraceGuard trace; } void AudioManager::SetAudioPlayer(app::AudioPlayer* audioPlayer) { diff --git a/src/script/gui_manager.cpp b/src/script/gui_manager.cpp index 1a71640..fb0b807 100644 --- a/src/script/gui_manager.cpp +++ b/src/script/gui_manager.cpp @@ -11,7 +11,7 @@ namespace sdl3cpp::script { GuiManager::GuiManager(lua_State* L) : L_(L) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__); + sdl3cpp::logging::TraceGuard trace; lua_getglobal(L_, "gui_input"); if (!lua_isnil(L_, -1)) { guiInputRef_ = luaL_ref(L_, LUA_REGISTRYINDEX); diff --git a/src/script/lua_bindings.cpp b/src/script/lua_bindings.cpp index 50c2267..5a2785d 100644 --- a/src/script/lua_bindings.cpp +++ b/src/script/lua_bindings.cpp @@ -10,7 +10,7 @@ namespace sdl3cpp::script { void LuaBindings::RegisterBindings(lua_State* L, ScriptEngine* engine) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; lua_pushlightuserdata(L, engine); lua_pushcclosure(L, &LoadMeshFromFile, 1); lua_setglobal(L, "load_mesh_from_file"); @@ -41,7 +41,7 @@ void LuaBindings::RegisterBindings(lua_State* L, ScriptEngine* engine) { } int LuaBindings::LoadMeshFromFile(lua_State* L) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; auto* engine = static_cast(lua_touserdata(L, lua_upvalueindex(1))); const char* path = luaL_checkstring(L, 1); sdl3cpp::logging::Logger::GetInstance().TraceVariable("path", path); @@ -60,7 +60,7 @@ int LuaBindings::LoadMeshFromFile(lua_State* L) { } int LuaBindings::PhysicsCreateBox(lua_State* L) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; auto* engine = static_cast(lua_touserdata(L, lua_upvalueindex(1))); const char* name = luaL_checkstring(L, 1); sdl3cpp::logging::Logger::GetInstance().TraceVariable("name", name); diff --git a/src/script/lua_helpers.cpp b/src/script/lua_helpers.cpp index 8f25574..d31ff80 100644 --- a/src/script/lua_helpers.cpp +++ b/src/script/lua_helpers.cpp @@ -1,4 +1,5 @@ #include "script/lua_helpers.hpp" +#include "logging/logger.hpp" #include #include @@ -10,6 +11,7 @@ namespace sdl3cpp::script { std::array ReadVector3(lua_State* L, int index) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(static_cast(L), index); std::array result{}; int absIndex = lua_absindex(L, index); size_t len = lua_rawlen(L, absIndex); @@ -29,6 +31,7 @@ std::array ReadVector3(lua_State* L, int index) { } std::array ReadQuaternion(lua_State* L, int index) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(static_cast(L), index); std::array result{}; int absIndex = lua_absindex(L, index); size_t len = lua_rawlen(L, absIndex); @@ -48,6 +51,7 @@ std::array ReadQuaternion(lua_State* L, int index) { } std::array ReadMatrix(lua_State* L, int index) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(static_cast(L), index); std::array result{}; int absIndex = lua_absindex(L, index); size_t len = lua_rawlen(L, absIndex); @@ -67,11 +71,13 @@ std::array ReadMatrix(lua_State* L, int index) { } std::string GetLuaError(lua_State* L) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(static_cast(L)); const char* message = lua_tostring(L, -1); return message ? message : "unknown lua error"; } std::array IdentityMatrix() { + sdl3cpp::logging::TraceGuard trace; return {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, @@ -79,14 +85,17 @@ std::array IdentityMatrix() { } glm::vec3 ToVec3(const std::array& value) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(value[0], value[1], value[2]); return glm::vec3(value[0], value[1], value[2]); } glm::quat ToQuat(const std::array& value) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(value[0], value[1], value[2], value[3]); return glm::quat(value[3], value[0], value[1], value[2]); } void PushMatrix(lua_State* L, const glm::mat4& matrix) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(static_cast(L)); lua_newtable(L); const float* ptr = glm::value_ptr(matrix); for (int i = 0; i < 16; ++i) { @@ -96,6 +105,7 @@ void PushMatrix(lua_State* L, const glm::mat4& matrix) { } int LuaGlmMatrixFromTransform(lua_State* L) { + sdl3cpp::logging::Logger::GetInstance().TraceFunctionWithArgs(static_cast(L)); std::array translation = ReadVector3(L, 1); std::array rotation = ReadQuaternion(L, 2); glm::vec3 pos = ToVec3(translation); diff --git a/src/script/mesh_loader.cpp b/src/script/mesh_loader.cpp index d25325c..18fa8f0 100644 --- a/src/script/mesh_loader.cpp +++ b/src/script/mesh_loader.cpp @@ -1,4 +1,5 @@ #include "script/mesh_loader.hpp" +#include "logging/logger.hpp" #include "script/script_engine.hpp" #include diff --git a/src/script/physics_bridge.cpp b/src/script/physics_bridge.cpp index d132653..85e0965 100644 --- a/src/script/physics_bridge.cpp +++ b/src/script/physics_bridge.cpp @@ -15,7 +15,7 @@ PhysicsBridge::PhysicsBridge() broadphase_.get(), solver_.get(), collisionConfig_.get())) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__); + sdl3cpp::logging::TraceGuard trace; world_->setGravity(btVector3(0.0f, -9.81f, 0.0f)); } diff --git a/src/script/scene_manager.cpp b/src/script/scene_manager.cpp index d4b47e7..66c3c7a 100644 --- a/src/script/scene_manager.cpp +++ b/src/script/scene_manager.cpp @@ -10,7 +10,7 @@ namespace sdl3cpp::script { SceneManager::SceneManager(lua_State* L) : L_(L) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__); + sdl3cpp::logging::TraceGuard trace; } std::vector SceneManager::LoadSceneObjects() { diff --git a/src/script/script_engine.cpp b/src/script/script_engine.cpp index 7caf15a..b6ac867 100644 --- a/src/script/script_engine.cpp +++ b/src/script/script_engine.cpp @@ -25,7 +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_)) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__);; + sdl3cpp::logging::TraceGuard trace;; if (!L_) { sdl3cpp::logging::Logger::GetInstance().Error("Failed to create Lua state"); throw std::runtime_error("Failed to create Lua state"); diff --git a/src/script/shader_manager.cpp b/src/script/shader_manager.cpp index 0d880c9..c696cf6 100644 --- a/src/script/shader_manager.cpp +++ b/src/script/shader_manager.cpp @@ -10,7 +10,7 @@ namespace sdl3cpp::script { ShaderManager::ShaderManager(lua_State* L) : L_(L) { - sdl3cpp::logging::TraceGuard trace(__PRETTY_FUNCTION__); + sdl3cpp::logging::TraceGuard trace; } std::unordered_map ShaderManager::LoadShaderPathsMap() {