From e2be23ddf0c034a90f39077e1822a1a4f6afa4ea Mon Sep 17 00:00:00 2001 From: johndoe6345789 Date: Sun, 4 Jan 2026 15:03:09 +0000 Subject: [PATCH] refactor: Remove logger implementation and update CMakeLists to reflect changes --- CMakeLists.txt | 2 - src/logging/logger.cpp | 201 ------------------------ src/logging/logger.hpp | 80 ---------- src/services/impl/sdl_audio_service.hpp | 5 + 4 files changed, 5 insertions(+), 283 deletions(-) delete mode 100644 src/logging/logger.cpp delete mode 100644 src/logging/logger.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a55a503..78ad7c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,7 +118,6 @@ find_package(glm CONFIG REQUIRED) if(BUILD_SDL3_APP) add_executable(sdl3_app src/main.cpp - src/logging/logger.cpp src/logging/string_utils.cpp src/core/platform.cpp src/di/service_registry.cpp @@ -176,7 +175,6 @@ enable_testing() add_executable(script_engine_tests tests/test_cube_script.cpp - src/logging/logger.cpp src/logging/string_utils.cpp src/core/platform.cpp src/script/script_engine.cpp diff --git a/src/logging/logger.cpp b/src/logging/logger.cpp deleted file mode 100644 index 7e72f78..0000000 --- a/src/logging/logger.cpp +++ /dev/null @@ -1,201 +0,0 @@ -#include "logging/logger.hpp" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace sdl3cpp::logging { - -// Implementation class that holds all the C++ magic -class LoggerImpl { -public: - std::atomic level_; - bool consoleEnabled_; - std::unique_ptr fileStream_; - std::mutex mutex_; - - LoggerImpl() : level_(LogLevel::INFO), consoleEnabled_(true) {} - - ~LoggerImpl() { - if (fileStream_) { - fileStream_->close(); - } - } -}; - -Logger& Logger::GetInstance() { - static Logger instance; - return instance; -} - -Logger::Logger() : impl_(new LoggerImpl()) {} - -Logger::~Logger() { - delete impl_; -} - -void Logger::SetLevel(LogLevel level) { - impl_->level_.store(level, std::memory_order_relaxed); -} - -LogLevel Logger::GetLevel() const { - return impl_->level_.load(std::memory_order_relaxed); -} - -void Logger::SetOutputFile(const std::string& filename) { - std::lock_guard lock(impl_->mutex_); - if (impl_->fileStream_) { - impl_->fileStream_->close(); - } - impl_->fileStream_ = std::make_unique(filename, std::ios::app); - if (!impl_->fileStream_->is_open()) { - // Fallback to console if file can't be opened - std::cerr << "Failed to open log file: " << filename << std::endl; - impl_->fileStream_.reset(); - } -} - -void Logger::EnableConsoleOutput(bool enable) { - impl_->consoleEnabled_ = enable; -} - -void Logger::Log(LogLevel level, const std::string& message) { - if (level < GetLevel()) { - return; - } - - std::lock_guard lock(impl_->mutex_); - std::string formattedMessage = FormatMessage(level, message); - - if (impl_->consoleEnabled_) { - WriteToConsole(level, formattedMessage); - } - - if (impl_->fileStream_) { - WriteToFile(formattedMessage); - } -} - -void Logger::Log(LogLevel level, const char* message) { - Log(level, std::string(message)); -} - -std::string Logger::LevelToString(LogLevel level) const { - switch (level) { - case LogLevel::TRACE: return "TRACE"; - case LogLevel::DEBUG: return "DEBUG"; - case LogLevel::INFO: return "INFO"; - case LogLevel::WARN: return "WARN"; - case LogLevel::ERROR: return "ERROR"; - default: return "UNKNOWN"; - } -} - -void Logger::WriteToConsole(LogLevel level, const std::string& message) { - if (level >= LogLevel::ERROR) { - std::cerr << message << std::endl; - } else { - std::cout << message << std::endl; - } -} - -void Logger::WriteToFile(const std::string& message) { - if (impl_->fileStream_) { - *impl_->fileStream_ << message << std::endl; - impl_->fileStream_->flush(); - } -} - -std::string Logger::FormatMessage(LogLevel level, const std::string& message) { - auto now = std::chrono::system_clock::now(); - auto time = std::chrono::system_clock::to_time_t(now); - auto ms = std::chrono::duration_cast( - now.time_since_epoch()) % 1000; - - std::ostringstream oss; - oss << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S") - << '.' << std::setfill('0') << std::setw(3) << ms.count() - << " [" << LevelToString(level) << "] " - << message; - return oss.str(); -} - -void Logger::Trace(const std::string& message) { - Log(LogLevel::TRACE, message); -} - -void Logger::Debug(const std::string& message) { - Log(LogLevel::DEBUG, message); -} - -void Logger::Info(const std::string& message) { - Log(LogLevel::INFO, message); -} - -void Logger::Warn(const std::string& message) { - Log(LogLevel::WARN, message); -} - -void Logger::Error(const std::string& message) { - Log(LogLevel::ERROR, message); -} - -void Logger::TraceFunction(const std::string& funcName) { - if (GetLevel() <= LogLevel::TRACE) { - Trace("Entering " + funcName); - } -} - -void Logger::TraceVariable(const std::string& name, const std::string& value) { - if (GetLevel() <= LogLevel::TRACE) { - Trace(name + " = " + value); - } -} - -void Logger::TraceVariable(const std::string& name, int value) { - TraceVariable(name, std::to_string(value)); -} - -void Logger::TraceVariable(const std::string& name, size_t value) { - TraceVariable(name, std::to_string(value)); -} - -void Logger::TraceVariable(const std::string& name, bool value) { - TraceVariable(name, value ? "true" : "false"); -} - -void Logger::TraceVariable(const std::string& name, float value) { - TraceVariable(name, std::to_string(value)); -} - -void Logger::TraceVariable(const std::string& name, double value) { - TraceVariable(name, std::to_string(value)); -} - -void Logger::TraceFunctionWithArgs(const std::string& description, const std::string& args) { - if (GetLevel() <= LogLevel::TRACE) { - Trace(description + ": " + args); - } -} - -TraceGuard::TraceGuard(const std::string& funcName) - : funcName_(funcName), ended_(false) { - if (!funcName_.empty()) { - Logger::GetInstance().Trace("Entering " + funcName_); - } -} - -void TraceGuard::End() { - if (!ended_ && !funcName_.empty()) { - Logger::GetInstance().Trace("Exiting " + funcName_); - ended_ = true; - } -} - -} // namespace sdl3cpp::logging \ No newline at end of file diff --git a/src/logging/logger.hpp b/src/logging/logger.hpp deleted file mode 100644 index 400fc73..0000000 --- a/src/logging/logger.hpp +++ /dev/null @@ -1,80 +0,0 @@ -#ifndef SDL3CPP_LOGGING_LOGGER_HPP -#define SDL3CPP_LOGGING_LOGGER_HPP - -#include - -namespace sdl3cpp::logging { - -enum class LogLevel { - TRACE = 0, - DEBUG = 1, - INFO = 2, - WARN = 3, - ERROR = 4, - OFF = 5 -}; - -// Forward declaration to hide implementation details -class LoggerImpl; - -class Logger { -public: - static Logger& GetInstance(); - - void SetLevel(LogLevel level); - LogLevel GetLevel() const; - - void SetOutputFile(const std::string& filename); - void EnableConsoleOutput(bool enable); - - void Log(LogLevel level, const std::string& message); - void Log(LogLevel level, const char* message); - - // Convenience methods - void Trace(const std::string& message); - void Debug(const std::string& message); - void Info(const std::string& message); - void Warn(const std::string& message); - void Error(const std::string& message); - - // Tracing methods - void TraceFunction(const std::string& funcName); - - // TraceVariable overloads for common types - void TraceVariable(const std::string& name, const std::string& value); - void TraceVariable(const std::string& name, int value); - void TraceVariable(const std::string& name, size_t value); - void TraceVariable(const std::string& name, bool value); - void TraceVariable(const std::string& name, float value); - void TraceVariable(const std::string& name, double value); - - void TraceFunctionWithArgs(const std::string& description, const std::string& args); - -private: - Logger(); - ~Logger(); - - // Non-copyable (Java final class pattern) - Logger(const Logger&); - Logger& operator=(const Logger&); - - std::string LevelToString(LogLevel level) const; - std::string FormatMessage(LogLevel level, const std::string& message); - void WriteToConsole(LogLevel level, const std::string& message); - void WriteToFile(const std::string& message); - - LoggerImpl* impl_; -}; - -class TraceGuard { -public: - explicit TraceGuard(const std::string& funcName = ""); - void End(); -private: - std::string funcName_; - bool ended_; -}; - -} // namespace sdl3cpp::logging - -#endif // SDL3CPP_LOGGING_LOGGER_HPP \ No newline at end of file diff --git a/src/services/impl/sdl_audio_service.hpp b/src/services/impl/sdl_audio_service.hpp index 778935e..6b9cf2d 100644 --- a/src/services/impl/sdl_audio_service.hpp +++ b/src/services/impl/sdl_audio_service.hpp @@ -5,6 +5,11 @@ #include "../../di/lifecycle.hpp" #include #include +#include +#include +#include +#include +#include namespace sdl3cpp::services::impl {