Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
kcenon::logger::log_collector_jthread_worker Class Reference

Worker thread for log processing with jthread compatibility. More...

Collaboration diagram for kcenon::logger::log_collector_jthread_worker:
Collaboration graph

Public Member Functions

 log_collector_jthread_worker (std::shared_ptr< log_collector_shared_state > state)
 
 ~log_collector_jthread_worker ()
 
void start ()
 
void stop ()
 
void notify_work ()
 
bool is_running () const noexcept
 

Static Private Member Functions

static void worker_loop (std::shared_ptr< log_collector_shared_state > state, async::simple_stop_source &stop)
 
static void write_to_all (const std::shared_ptr< log_collector_shared_state > &state, const log_entry &entry)
 

Private Attributes

std::shared_ptr< log_collector_shared_statestate_
 
async::compat_jthread thread_
 
std::atomic< bool > running_
 

Detailed Description

Worker thread for log processing with jthread compatibility.

Uses std::jthread where available, falls back to std::thread with manual stop mechanism otherwise.

Definition at line 69 of file log_collector.cpp.

Constructor & Destructor Documentation

◆ log_collector_jthread_worker()

kcenon::logger::log_collector_jthread_worker::log_collector_jthread_worker ( std::shared_ptr< log_collector_shared_state > state)
inlineexplicit

Definition at line 71 of file log_collector.cpp.

72 : state_(std::move(state))
73 , running_(false)
74 {}
std::shared_ptr< log_collector_shared_state > state_

◆ ~log_collector_jthread_worker()

kcenon::logger::log_collector_jthread_worker::~log_collector_jthread_worker ( )
inline

Definition at line 76 of file log_collector.cpp.

References stop().

Here is the call graph for this function:

Member Function Documentation

◆ is_running()

bool kcenon::logger::log_collector_jthread_worker::is_running ( ) const
inlinenodiscardnoexcept

Definition at line 125 of file log_collector.cpp.

125 {
126 return running_.load(std::memory_order_acquire);
127 }

References running_.

◆ notify_work()

void kcenon::logger::log_collector_jthread_worker::notify_work ( )
inline

Definition at line 119 of file log_collector.cpp.

119 {
120 if (state_) {
121 state_->queue_cv.notify_one();
122 }
123 }

References state_.

◆ start()

void kcenon::logger::log_collector_jthread_worker::start ( )
inline

Definition at line 80 of file log_collector.cpp.

80 {
81 if (running_.exchange(true, std::memory_order_acq_rel)) {
82 return; // Already started
83 }
84
85#if LOGGER_HAS_JTHREAD
86 // Capture state by shared_ptr - survives worker destruction
87 auto state = state_;
88 thread_ = async::compat_jthread([state](std::stop_token stop_token) {
89 worker_loop(state, stop_token);
90 });
91#else
92 // Create worker thread with manual stop source
93 // Note: We use the stop_source created by compat_jthread to ensure
94 // request_stop() correctly signals the worker loop
95 auto state = state_;
96 thread_ = async::compat_jthread([state](async::simple_stop_source& stop) {
97 worker_loop(state, stop);
98 });
99#endif
100 }
static void worker_loop(std::shared_ptr< log_collector_shared_state > state, async::simple_stop_source &stop)

References running_, state_, stop(), thread_, and worker_loop().

Here is the call graph for this function:

◆ stop()

void kcenon::logger::log_collector_jthread_worker::stop ( )
inline

Definition at line 102 of file log_collector.cpp.

102 {
103 if (!running_.exchange(false, std::memory_order_acq_rel)) {
104 return; // Already stopped
105 }
106
107 // Request stop
109
110 // Wake up the worker in case it's waiting
111 if (state_) {
112 state_->queue_cv.notify_all();
113 }
114
115 // Wait for thread to finish
116 thread_.join();
117 }
void request_stop()
Request the thread to stop.
void join()
Wait for thread to complete.

References kcenon::logger::async::compat_jthread::join(), kcenon::logger::async::compat_jthread::request_stop(), running_, state_, and thread_.

Referenced by start(), worker_loop(), and ~log_collector_jthread_worker().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ worker_loop()

static void kcenon::logger::log_collector_jthread_worker::worker_loop ( std::shared_ptr< log_collector_shared_state > state,
async::simple_stop_source & stop )
inlinestaticprivate

Definition at line 173 of file log_collector.cpp.

174 {
175 if (!state) {
176 return;
177 }
178
179 while (!stop.stop_requested()) {
180 std::vector<log_entry> batch;
181
182 {
183 std::unique_lock<std::mutex> lock(state->queue_mutex);
184
185 // Wait for work or stop signal
186 state->queue_cv.wait(lock, [&state, &stop]() {
187 return stop.stop_requested() || !state->queue.empty();
188 });
189
190 // Check if stop was requested
191 if (stop.stop_requested()) {
192 break;
193 }
194
195 // Check if we actually have work
196 if (state->queue.empty()) {
197 continue;
198 }
199
200 // Extract batch from queue
201 batch.reserve(std::min(state->batch_size, state->queue.size()));
202 while (!state->queue.empty() && batch.size() < state->batch_size) {
203 batch.push_back(std::move(state->queue.front()));
204 state->queue.pop();
205 }
206 }
207
208 // Process batch outside the lock
209 for (const auto& entry : batch) {
210 write_to_all(state, entry);
211 }
212 }
213 }
static void write_to_all(const std::shared_ptr< log_collector_shared_state > &state, const log_entry &entry)

References stop(), and write_to_all().

Referenced by start().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_to_all()

static void kcenon::logger::log_collector_jthread_worker::write_to_all ( const std::shared_ptr< log_collector_shared_state > & state,
const log_entry & entry )
inlinestaticprivate

Definition at line 216 of file log_collector.cpp.

217 {
218 if (!state) {
219 return;
220 }
221
222 // Snapshot writers under lock
223 std::vector<std::shared_ptr<log_writer_interface>> writers_snapshot;
224 {
225 std::lock_guard<std::mutex> lock(state->writers_mutex);
226 writers_snapshot.reserve(state->writers.size());
227 for (auto& weak_writer : state->writers) {
228 if (auto writer = weak_writer.lock()) {
229 writers_snapshot.push_back(writer);
230 }
231 }
232 }
233
234 // Write to all writers without holding mutex
235 for (auto& writer : writers_snapshot) {
236 try {
237 writer->write(entry);
238 } catch (...) {
239 // Swallow exceptions to prevent thread termination
240 }
241 }
242 }

Referenced by worker_loop().

Here is the caller graph for this function:

Member Data Documentation

◆ running_

std::atomic<bool> kcenon::logger::log_collector_jthread_worker::running_
private

Definition at line 247 of file log_collector.cpp.

Referenced by is_running(), start(), and stop().

◆ state_

std::shared_ptr<log_collector_shared_state> kcenon::logger::log_collector_jthread_worker::state_
private

Definition at line 245 of file log_collector.cpp.

Referenced by notify_work(), start(), and stop().

◆ thread_

async::compat_jthread kcenon::logger::log_collector_jthread_worker::thread_
private

Definition at line 246 of file log_collector.cpp.

Referenced by start(), and stop().


The documentation for this class was generated from the following file: