Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
standalone_executor.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
31#pragma once
32
35
36#if LOGGER_HAS_IEXECUTOR
37
38#if __has_include(<kcenon/common/interfaces/executor_interface.h>)
39#include <kcenon/common/interfaces/executor_interface.h>
40#include <kcenon/common/patterns/result.h>
41#elif __has_include(<common/interfaces/executor_interface.h>)
42#include <common/interfaces/executor_interface.h>
43#include <common/patterns/result.h>
44#endif
45
46#include <atomic>
47#include <chrono>
48#include <condition_variable>
49#include <functional>
50#include <future>
51#include <memory>
52#include <mutex>
53#include <queue>
54#include <string>
55#include <thread>
56
58
64class function_job : public common::interfaces::IJob {
65public:
66 explicit function_job(std::function<void()> func, std::string name = "logger_task")
67 : func_(std::move(func)), name_(std::move(name)) {}
68
69 common::VoidResult execute() override {
70 try {
71 func_();
72 return common::ok();
73 } catch (const std::exception& ex) {
75 -1, ex.what(), "standalone_executor"
76 });
77 } catch (...) {
79 -1, "Unknown exception during job execution", "standalone_executor"
80 });
81 }
82 }
83
84 std::string get_name() const override { return name_; }
85
86private:
87 std::function<void()> func_;
88 std::string name_;
89};
90
118class LOGGER_SYSTEM_API standalone_executor : public common::interfaces::IExecutor {
119public:
125 explicit standalone_executor(std::size_t queue_size = 8192,
126 std::string name = "standalone_executor");
127
131 ~standalone_executor() override;
132
133 // Non-copyable, non-movable
134 standalone_executor(const standalone_executor&) = delete;
135 standalone_executor& operator=(const standalone_executor&) = delete;
136 standalone_executor(standalone_executor&&) = delete;
137 standalone_executor& operator=(standalone_executor&&) = delete;
138
144 void start();
145
146 // ===== IExecutor Interface Implementation =====
147
154 std::unique_ptr<common::interfaces::IJob>&& job) override;
155
162 common::Result<std::future<void>> execute_delayed(
163 std::unique_ptr<common::interfaces::IJob>&& job,
164 std::chrono::milliseconds delay) override;
165
170 size_t worker_count() const override;
171
176 bool is_running() const override;
177
182 size_t pending_tasks() const override;
183
188 void shutdown(bool wait_for_completion = true) override;
189
190 // ===== Additional Methods =====
191
196 [[nodiscard]] std::string get_name() const noexcept { return name_; }
197
202 [[nodiscard]] std::uint64_t dropped_count() const noexcept;
203
204private:
208 struct pending_task {
209 std::unique_ptr<common::interfaces::IJob> job;
210 std::shared_ptr<std::promise<void>> completion_promise;
211 std::chrono::steady_clock::time_point execute_after;
212 };
213
217 void worker_loop();
218
222 void drain_queue();
223
229 bool enqueue_task(pending_task&& task);
230
231private:
232 const std::string name_;
233 const std::size_t queue_size_;
234
235 std::queue<pending_task> queue_;
236 mutable std::mutex queue_mutex_;
237 std::condition_variable queue_cv_;
238
239 std::thread worker_thread_;
240 std::atomic<bool> running_{false};
241 std::atomic<bool> stop_requested_{false};
242 std::atomic<std::uint64_t> dropped_count_{0};
243};
244
248class LOGGER_SYSTEM_API standalone_executor_factory {
249public:
256 static std::shared_ptr<common::interfaces::IExecutor> create(
257 std::size_t queue_size = 8192,
258 const std::string& name = "standalone_executor");
259};
260
261} // namespace kcenon::logger::integration
262
263#endif // LOGGER_HAS_IEXECUTOR
DLL export/import macros for logger_system shared library support.
#define LOGGER_SYSTEM_API
VoidResult ok()
Conditionally enables thread_system integration when available.