20 std::chrono::milliseconds flush_interval)
22 , max_entries_(max_entries)
23 , flush_interval_(flush_interval)
24 , last_flush_time_(std::chrono::steady_clock::now()) {
26 throw std::invalid_argument(
"buffered_writer: max_entries must be greater than 0");
33 std::lock_guard<std::mutex> lock(
mutex_);
40 std::lock_guard<std::mutex> lock(
mutex_);
47 bool flush_needed =
false;
54 }
else if (flush_by_time) {
67 std::lock_guard<std::mutex> lock(
mutex_);
78 for (
const auto& entry :
buffer_) {
104 auto now = std::chrono::steady_clock::now();
105 auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now -
last_flush_time_);
115 entry.
location->function.to_string(),
122 std::lock_guard<std::mutex> lock(
mutex_);
147 std::unique_ptr<log_writer_interface> writer,
149 std::chrono::milliseconds flush_interval) {
150 return std::make_unique<buffered_writer>(std::move(writer), max_entries, flush_interval);
Decorator that provides buffering for wrapped log writers.
std::vector< log_entry > buffer_
std::chrono::milliseconds get_flush_interval() const noexcept
Get the configured flush interval.
size_t get_buffer_count() const
Get the current number of buffered entries.
common::VoidResult flush_buffer_unsafe()
Flush buffer without acquiring mutex (caller must hold lock)
size_t get_max_entries() const noexcept
Get the maximum buffer size.
~buffered_writer() override
Destructor - flushes any remaining buffered entries.
common::VoidResult flush() override
Flush all buffered entries to the wrapped writer.
common::VoidResult write(const log_entry &entry) override
Write a log entry to the buffer.
const stats & get_stats() const noexcept
Get buffered writer statistics.
void reset_stats()
Reset statistics counters.
bool should_flush_by_time() const
Check if time-based flush is needed.
buffered_writer(std::unique_ptr< log_writer_interface > wrapped, size_t max_entries=DEFAULT_BUFFER_SIZE, std::chrono::milliseconds flush_interval=DEFAULT_FLUSH_INTERVAL)
Construct a buffered writer.
std::chrono::steady_clock::time_point last_flush_time_
static log_entry copy_entry(const log_entry &entry)
Copy a log entry for buffer storage.
std::chrono::milliseconds flush_interval_
Abstract base class for decorator pattern log writers.
log_writer_interface & wrapped() noexcept
Access the wrapped writer (non-const)
virtual common::VoidResult flush()=0
Flush any buffered data.
virtual common::VoidResult write(const log_entry &entry)=0
Write a log entry.
std::string to_string() const
Convert to std::string.
std::unique_ptr< buffered_writer > make_buffered_writer(std::unique_ptr< log_writer_interface > writer, size_t max_entries=buffered_writer::DEFAULT_BUFFER_SIZE, std::chrono::milliseconds flush_interval=buffered_writer::DEFAULT_FLUSH_INTERVAL)
Factory function to create a buffered writer.
Statistics for the buffered writer.
std::atomic< uint64_t > total_entries_written
std::atomic< uint64_t > flush_on_interval
std::atomic< uint64_t > manual_flushes
std::atomic< uint64_t > total_flushes
std::atomic< uint64_t > flush_on_full
Represents a single log entry with all associated metadata.
std::optional< source_location > location
Optional source code location information.
log_level level
Severity level of the log message.
small_string_256 message
The actual log message.
std::chrono::system_clock::time_point timestamp
Timestamp when the log entry was created.