37 std::ios_base::openmode mode = std::ios_base::out;
40 mode |= std::ios_base::app;
44 mode |= std::ios_base::trunc;
48 std::filesystem::path path(filepath);
49 if (
auto parent = path.parent_path(); !parent.empty()) {
50 std::filesystem::create_directories(parent);
52 }
catch (
const std::filesystem::filesystem_error& e) {
53 m_last_error = std::string(
"Failed to create directories: ") + e.what();
57 m_file.open(filepath, mode);
186 auto now = std::chrono::system_clock::now();
187 auto time_t = std::chrono::system_clock::to_time_t(now);
189 std::ostringstream oss;
191 << std::put_time(std::localtime(&time_t),
"%Y%m%d_%H%M%S");
193 std::string backup_path = oss.str();
196 std::filesystem::rename(
m_filepath, backup_path);
197 }
catch (
const std::filesystem::filesystem_error& e) {
198 m_last_error = std::string(
"Failed to rotate file: ") + e.what();
202 std::ios_base::openmode mode = std::ios_base::out | std::ios_base::trunc;
bool flush() override
Flush buffered writes to disk.
FileWriteOptions m_options
bool write_string(std::string_view str) override
Write a string.
void set_max_file_size(size_t max_bytes)
Set maximum file size before rotation.
bool open(const std::string &filepath, FileWriteOptions options) override
Open a file for writing.
~TextFileWriter() override
bool can_write(const std::string &filepath) const override
Check if this writer can handle the given file path.
std::string get_last_error() const override
Get last error message.
bool write_line(std::string_view line) override
Write a line (appends newline)
void close() override
Close the currently open file.
size_t get_write_position() const override
Get current write position (bytes written)
bool write_bytes(const void *data, size_t size) override
Write raw bytes.
bool is_open() const override
Check if a file is currently open for writing.
bool should_rotate() const
size_t get_file_size() const
Get current file size.