41 lines
976 B
C++
41 lines
976 B
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()) {
|
||
|
|
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));
|
||
|
|
}
|
||
|
|
|
||
|
|
void FileLogSink::Flush() {
|
||
|
|
m_writer.Flush();
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace Debug
|
||
|
|
} // namespace XCEngine
|