#include "Debug/FileLogSink.h" #include namespace XCEngine { namespace Debug { FileLogSink::FileLogSink(const Containers::String& filePath) : m_filePath(filePath) { m_writer.Open(filePath.CStr(), true); } FileLogSink::~FileLogSink() { m_writer.Close(); } void FileLogSink::Log(const LogEntry& entry) { if (!m_writer.IsOpen()) { // File not open, try to reopen bool opened = m_writer.Open(m_filePath.CStr(), true); if (!opened) { // Failed to open - output to stderr as fallback fprintf(stderr, "[FileLogSink] Failed to open log file: %s\n", m_filePath.CStr()); fprintf(stderr, "[%s] [%s] %s\n", LogLevelToString(entry.level), LogCategoryToString(entry.category), entry.message.CStr()); return; } } char timestamp[32]; std::time_t time = static_cast(entry.timestamp); std::strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", std::localtime(&time)); char buffer[1024]; std::snprintf(buffer, sizeof(buffer), "[%s] [%s] [%s] %s\n", timestamp, LogLevelToString(entry.level), LogCategoryToString(entry.category), entry.message.CStr()); bool wrote = m_writer.Write(buffer, std::strlen(buffer)); if (!wrote) { fprintf(stderr, "[FileLogSink] Write failed for: %s\n", buffer); } m_writer.Flush(); } void FileLogSink::Flush() { m_writer.Flush(); } } // namespace Debug } // namespace XCEngine