MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TextFileWriter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "FileWriter.hpp"
4#include <fstream>
5
6namespace MayaFlux::IO {
7
8/**
9 * @class TextFileWriter
10 * @brief Simple text file writer for logs and text data
11 *
12 * Thread-safe text file writing with optional buffering.
13 * Designed for log files, configuration files, etc.
14 */
15class TextFileWriter : public FileWriter {
16public:
18 ~TextFileWriter() override;
19
20 bool can_write(const std::string& filepath) const override;
21 bool open(const std::string& filepath, FileWriteOptions options) override;
22 void close() override;
23 bool is_open() const override;
24
25 bool write_bytes(const void* data, size_t size) override;
26 bool write_string(std::string_view str) override;
27 bool write_line(std::string_view line) override;
28 bool flush() override;
29
30 size_t get_write_position() const override;
31 std::string get_last_error() const override;
32
33 /**
34 * @brief Set maximum file size before rotation
35 * @param max_bytes Maximum size in bytes (0 = no limit)
36 */
37 void set_max_file_size(size_t max_bytes);
38
39 /**
40 * @brief Get current file size
41 */
42 size_t get_file_size() const;
43
44private:
45 bool should_rotate() const;
46 bool rotate_file();
47
48 mutable std::mutex m_mutex;
49 std::ofstream m_file;
50 std::string m_filepath;
52 size_t m_bytes_written { 0 };
53 size_t m_max_file_size { 0 };
54 mutable std::string m_last_error;
55 bool m_is_open {};
56};
57
58} // namespace MayaFlux::IO
Abstract base class for file writing operations.
bool flush() override
Flush buffered writes to disk.
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.
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.
size_t get_file_size() const
Get current file size.
Simple text file writer for logs and text data.