Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
critical_writer.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
44#include "base_writer.h"
46
48
49#include <atomic>
50#include <memory>
51#include <mutex>
52#include <fstream>
53#include <functional>
54
55namespace kcenon::logger {
56
64
67
68 // Signal handling is managed by logger and signal_manager (DI pattern).
69 // Use logger_context for signal handling instead.
70
72 bool write_ahead_log = false;
73
75 std::string wal_path = "logs/.wal";
76
78 bool sync_on_critical = true;
79
82};
83
104public:
111 explicit critical_writer(
112 log_writer_ptr wrapped_writer,
113 critical_writer_config config = {}
114 );
115
119 ~critical_writer() override;
120
137 common::VoidResult write(const log_entry& entry) override;
138
143 common::VoidResult flush() override;
144
149 bool is_healthy() const override;
150
155 std::string get_name() const override;
156
161 void set_use_color(bool use_color) override;
162
167 void set_force_flush_on_critical(bool enable);
168
173 const critical_writer_config& get_config() const { return config_; }
174
179 std::atomic<uint64_t> total_critical_writes{0};
180 std::atomic<uint64_t> total_flushes{0};
181 std::atomic<uint64_t> wal_writes{0};
182 std::atomic<uint64_t> sync_calls{0};
183 };
184
185 const critical_stats& get_stats() const { return stats_; }
186
187private:
193 bool is_critical_level(common::interfaces::log_level level) const;
194
199 void write_to_wal(const log_entry& entry);
200
204 void sync_file_descriptor();
205
206
209
212
214 mutable std::mutex critical_mutex_;
215
217 std::unique_ptr<std::ofstream> wal_stream_;
218
221
223 std::atomic<bool> shutting_down_{false};
224};
225
248public:
249 explicit hybrid_writer(
250 log_writer_ptr wrapped_writer,
251 critical_writer_config critical_config = {},
252 std::size_t async_queue_size = 10000
253 );
254
255 ~hybrid_writer() override;
256
263 common::VoidResult write(const log_entry& entry) override;
264
265 common::VoidResult flush() override;
266 bool is_healthy() const override;
267 std::string get_name() const override;
268 void set_use_color(bool use_color) override;
269
270private:
272 std::size_t async_queue_size_;
273};
274
275} // namespace kcenon::logger
Abstract base class for all log output writers kcenon.
Abstract base class for all log output writers.
Definition base_writer.h:95
bool use_color() const
Get current color output setting.
Synchronous wrapper ensuring critical logs are never lost.
std::mutex critical_mutex_
Mutex for critical section.
critical_writer_config config_
Configuration.
critical_stats stats_
Statistics.
const critical_stats & get_stats() const
const critical_writer_config & get_config() const
Get current configuration.
std::unique_ptr< std::ofstream > wal_stream_
Write-ahead log stream.
log_writer_ptr wrapped_writer_
Wrapped writer.
Combines async writer for normal logs with critical_writer for safety.
std::string get_name() const override
hybrid_writer(log_writer_ptr wrapped_writer, critical_writer_config critical_config={}, std::size_t async_queue_size=10000)
bool is_healthy() const override
Check if the writer is healthy and operational.
common::VoidResult write(const log_entry &entry) override
Write a log entry.
common::VoidResult flush() override
Flush any buffered data.
void set_use_color(bool use_color) override
Set whether to use color output (if supported)
DLL export/import macros for logger_system shared library support.
#define LOGGER_SYSTEM_API
std::unique_ptr< log_writer_interface > log_writer_ptr
Type alias for writer unique pointer.
Tag interface for composite writers.
Get statistics about critical writes.
Configuration for critical log writer.
bool force_flush_on_error
Force immediate flush for error messages (default: false)
bool force_flush_on_critical
Force immediate flush for critical/fatal messages (default: true)
std::string wal_path
Path for write-ahead log file (only if write_ahead_log is true)
bool sync_on_critical
Sync file descriptor after each critical write (default: true)
bool write_ahead_log
Enable write-ahead logging for maximum durability (default: false)
uint32_t critical_write_timeout_ms
Timeout for critical write operations in milliseconds (0 = no timeout)
Tag interface for decorator writers.
Represents a single log entry with all associated metadata.
Definition log_entry.h:155
Writer category interfaces and type traits kcenon.