- LogLevel: 日志级别枚举 (Verbose, Debug, Info, Warning, Error, Fatal) - LogCategory: 日志分类 (General, Rendering, Physics, Memory, Threading等) - ILogSink: 日志输出接口 - ConsoleLogSink: 控制台输出, 支持Windows颜色 - FileLogSink: 文件日志输出 - FileWriter: 文件写入器 - Logger: 日志管理器, 支持多sink, 分类控制 - Profiler: 性能分析器 - 单元测试覆盖
51 lines
1012 B
C++
51 lines
1012 B
C++
#include "Core/FileWriter.h"
|
|
#include <cstring>
|
|
|
|
namespace XCEngine {
|
|
namespace Core {
|
|
|
|
FileWriter::FileWriter() = default;
|
|
|
|
FileWriter::FileWriter(const char* filePath, bool append) {
|
|
Open(filePath, append);
|
|
}
|
|
|
|
FileWriter::~FileWriter() {
|
|
Close();
|
|
}
|
|
|
|
bool FileWriter::Open(const char* filePath, bool append) {
|
|
Close();
|
|
const char* mode = append ? "ab" : "wb";
|
|
m_file = std::fopen(filePath, mode);
|
|
return m_file != nullptr;
|
|
}
|
|
|
|
void FileWriter::Close() {
|
|
if (m_file) {
|
|
std::fclose(m_file);
|
|
m_file = nullptr;
|
|
}
|
|
}
|
|
|
|
bool FileWriter::Write(const char* data, size_t length) {
|
|
if (!m_file || !data || length == 0) {
|
|
return false;
|
|
}
|
|
return std::fwrite(data, 1, length, m_file) == length;
|
|
}
|
|
|
|
bool FileWriter::Write(const Containers::String& str) {
|
|
return Write(str.CStr(), str.Length());
|
|
}
|
|
|
|
bool FileWriter::Flush() {
|
|
if (!m_file) {
|
|
return false;
|
|
}
|
|
return std::fflush(m_file) == 0;
|
|
}
|
|
|
|
} // namespace Core
|
|
} // namespace XCEngine
|