Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
executor_integration.cpp
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
13
14#if LOGGER_HAS_IEXECUTOR
15
17
18// Static member initialization
19std::atomic<executor_type> executor_integration::current_type_{executor_type::none};
20std::shared_ptr<common::interfaces::IExecutor> executor_integration::executor_{nullptr};
21std::mutex executor_integration::executor_mutex_{};
22
23void executor_integration::enable(std::shared_ptr<common::interfaces::IExecutor> executor) {
24 std::lock_guard<std::mutex> lock(executor_mutex_);
25
26 if (executor) {
27 executor_ = std::move(executor);
28 current_type_.store(executor_type::external, std::memory_order_release);
29 } else if (!executor_) {
30 // Create default standalone executor if none exists
31 executor_ = create_default_executor();
32 if (executor_) {
33 current_type_.store(executor_type::standalone, std::memory_order_release);
34 }
35 } else {
36 // Re-enable existing executor
37 if (dynamic_cast<standalone_executor*>(executor_.get()) != nullptr) {
38 current_type_.store(executor_type::standalone, std::memory_order_release);
39 } else {
40 current_type_.store(executor_type::external, std::memory_order_release);
41 }
42 }
43}
44
46 std::lock_guard<std::mutex> lock(executor_mutex_);
47
48 current_type_.store(executor_type::none, std::memory_order_release);
49 // Note: We don't shut down the executor here - let it be managed by its owner
50 // Just release our reference
51 executor_.reset();
52}
53
55 return current_type_.load(std::memory_order_acquire) != executor_type::none;
56}
57
58executor_type executor_integration::get_executor_type() noexcept {
59 return current_type_.load(std::memory_order_acquire);
60}
61
62void executor_integration::set_executor(std::shared_ptr<common::interfaces::IExecutor> executor) {
63 std::lock_guard<std::mutex> lock(executor_mutex_);
64
65 if (executor) {
66 executor_ = std::move(executor);
67 if (dynamic_cast<standalone_executor*>(executor_.get()) != nullptr) {
68 current_type_.store(executor_type::standalone, std::memory_order_release);
69 } else {
70 current_type_.store(executor_type::external, std::memory_order_release);
71 }
72 } else {
73 executor_.reset();
74 current_type_.store(executor_type::none, std::memory_order_release);
75 }
76}
77
78std::shared_ptr<common::interfaces::IExecutor> executor_integration::get_executor() noexcept {
79 std::lock_guard<std::mutex> lock(executor_mutex_);
80 return executor_;
81}
82
83bool executor_integration::submit_task(std::function<void()> task) {
84 if (!is_enabled()) {
85 return false;
86 }
87
88 std::shared_ptr<common::interfaces::IExecutor> executor;
89 {
90 std::lock_guard<std::mutex> lock(executor_mutex_);
91 executor = executor_;
92 }
93
94 if (!executor || !executor->is_running()) {
95 return false;
96 }
97
98 auto job = std::make_unique<function_job>(std::move(task));
99 auto result = executor->execute(std::move(job));
100
101 return result.is_ok();
102}
103
104bool executor_integration::submit_task_delayed(
105 std::function<void()> task,
106 std::chrono::milliseconds delay_ms) {
107
108 if (!is_enabled()) {
109 return false;
110 }
111
112 std::shared_ptr<common::interfaces::IExecutor> executor;
113 {
114 std::lock_guard<std::mutex> lock(executor_mutex_);
115 executor = executor_;
116 }
117
118 if (!executor || !executor->is_running()) {
119 return false;
120 }
121
122 auto job = std::make_unique<function_job>(std::move(task));
123 auto result = executor->execute_delayed(std::move(job), delay_ms);
124
125 return result.is_ok();
126}
127
128std::string executor_integration::get_executor_name() noexcept {
129 switch (current_type_.load(std::memory_order_acquire)) {
130 case executor_type::standalone:
131 return "standalone";
132 case executor_type::external:
133 return "external";
134 case executor_type::none:
135 default:
136 return "none";
137 }
138}
139
140size_t executor_integration::pending_tasks() noexcept {
141 std::lock_guard<std::mutex> lock(executor_mutex_);
142 if (executor_) {
143 return executor_->pending_tasks();
144 }
145 return 0;
146}
147
148size_t executor_integration::worker_count() noexcept {
149 std::lock_guard<std::mutex> lock(executor_mutex_);
150 if (executor_) {
151 return executor_->worker_count();
152 }
153 return 0;
154}
155
156std::shared_ptr<common::interfaces::IExecutor> executor_integration::create_default_executor() {
157 return standalone_executor_factory::create(8192, "logger_async_executor");
158}
159
160} // namespace kcenon::logger::integration
161
162#endif // LOGGER_HAS_IEXECUTOR
static bool submit_task(std::function< void()>) noexcept
IExecutor-based async integration for logger_system.