refactor: Remove logger implementation and update CMakeLists to reflect changes

This commit is contained in:
2026-01-04 15:03:09 +00:00
parent 4a8e454d60
commit e2be23ddf0
4 changed files with 5 additions and 283 deletions

View File

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

View File

@@ -1,201 +0,0 @@
#include "logging/logger.hpp"
#include <atomic>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <memory>
#include <mutex>
#include <sstream>
#include <thread>
namespace sdl3cpp::logging {
// Implementation class that holds all the C++ magic
class LoggerImpl {
public:
std::atomic<LogLevel> level_;
bool consoleEnabled_;
std::unique_ptr<std::ofstream> 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<std::mutex> lock(impl_->mutex_);
if (impl_->fileStream_) {
impl_->fileStream_->close();
}
impl_->fileStream_ = std::make_unique<std::ofstream>(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<std::mutex> 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<std::chrono::milliseconds>(
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

View File

@@ -1,80 +0,0 @@
#ifndef SDL3CPP_LOGGING_LOGGER_HPP
#define SDL3CPP_LOGGING_LOGGER_HPP
#include <string>
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

View File

@@ -5,6 +5,11 @@
#include "../../di/lifecycle.hpp"
#include <memory>
#include <SDL3/SDL.h>
#include <vorbis/vorbisfile.h>
#include <filesystem>
#include <vector>
#include <atomic>
#include <mutex>
namespace sdl3cpp::services::impl {