47 lines
1.2 KiB
C++
47 lines
1.2 KiB
C++
#include "Debug/FileLogSink.h"
|
|
#include <ctime>
|
|
|
|
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
|
|
m_writer.Open(m_filePath.CStr(), true);
|
|
if (!m_writer.IsOpen()) {
|
|
// Still not open - output to debug
|
|
return;
|
|
}
|
|
}
|
|
|
|
char timestamp[32];
|
|
std::time_t time = static_cast<std::time_t>(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());
|
|
|
|
m_writer.Write(buffer, std::strlen(buffer));
|
|
m_writer.Flush();
|
|
}
|
|
|
|
void FileLogSink::Flush() {
|
|
m_writer.Flush();
|
|
}
|
|
|
|
} // namespace Debug
|
|
} // namespace XCEngine
|