| 1 | #pragma once |
|---|
| 2 | |
|---|
| 3 | #include "spdlog/details/fmt_helper.h" |
|---|
| 4 | #include "spdlog/details/log_msg.h" |
|---|
| 5 | #include "spdlog/details/mpmc_blocking_q.h" |
|---|
| 6 | #include "spdlog/details/os.h" |
|---|
| 7 | |
|---|
| 8 | #include <chrono> |
|---|
| 9 | #include <memory> |
|---|
| 10 | #include <thread> |
|---|
| 11 | #include <vector> |
|---|
| 12 | |
|---|
| 13 | namespace spdlog { |
|---|
| 14 | namespace details { |
|---|
| 15 | |
|---|
| 16 | using async_logger_ptr = std::shared_ptr<spdlog::async_logger>; |
|---|
| 17 | |
|---|
| 18 | enum class async_msg_type |
|---|
| 19 | { |
|---|
| 20 | log, |
|---|
| 21 | flush, |
|---|
| 22 | terminate |
|---|
| 23 | }; |
|---|
| 24 | |
|---|
| 25 | // Async msg to move to/from the queue |
|---|
| 26 | // Movable only. should never be copied |
|---|
| 27 | struct async_msg |
|---|
| 28 | { |
|---|
| 29 | async_msg_type msg_type; |
|---|
| 30 | level::level_enum level; |
|---|
| 31 | log_clock::time_point time; |
|---|
| 32 | size_t thread_id; |
|---|
| 33 | fmt::basic_memory_buffer<char, 176> raw; |
|---|
| 34 | |
|---|
| 35 | size_t msg_id; |
|---|
| 36 | source_loc source; |
|---|
| 37 | async_logger_ptr worker_ptr; |
|---|
| 38 | |
|---|
| 39 | async_msg() = default; |
|---|
| 40 | ~async_msg() = default; |
|---|
| 41 | |
|---|
| 42 | // should only be moved in or out of the queue.. |
|---|
| 43 | async_msg(const async_msg &) = delete; |
|---|
| 44 | |
|---|
| 45 | // support for vs2013 move |
|---|
| 46 | #if defined(_MSC_VER) && _MSC_VER <= 1800 |
|---|
| 47 | async_msg(async_msg &&other) SPDLOG_NOEXCEPT : msg_type(other.msg_type), |
|---|
| 48 | level(other.level), |
|---|
| 49 | time(other.time), |
|---|
| 50 | thread_id(other.thread_id), |
|---|
| 51 | raw(move(other.raw)), |
|---|
| 52 | msg_id(other.msg_id), |
|---|
| 53 | source(other.source), |
|---|
| 54 | worker_ptr(std::move(other.worker_ptr)) |
|---|
| 55 | { |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | async_msg &operator=(async_msg &&other) SPDLOG_NOEXCEPT |
|---|
| 59 | { |
|---|
| 60 | msg_type = other.msg_type; |
|---|
| 61 | level = other.level; |
|---|
| 62 | time = other.time; |
|---|
| 63 | thread_id = other.thread_id; |
|---|
| 64 | raw = std::move(other.raw); |
|---|
| 65 | msg_id = other.msg_id; |
|---|
| 66 | source = other.source; |
|---|
| 67 | worker_ptr = std::move(other.worker_ptr); |
|---|
| 68 | return *this; |
|---|
| 69 | } |
|---|
| 70 | #else // (_MSC_VER) && _MSC_VER <= 1800 |
|---|
| 71 | async_msg(async_msg &&) = default; |
|---|
| 72 | async_msg &operator=(async_msg &&) = default; |
|---|
| 73 | #endif |
|---|
| 74 | |
|---|
| 75 | // construct from log_msg with given type |
|---|
| 76 | async_msg(async_logger_ptr &&worker, async_msg_type the_type, details::log_msg &m) |
|---|
| 77 | : msg_type(the_type) |
|---|
| 78 | , level(m.level) |
|---|
| 79 | , time(m.time) |
|---|
| 80 | , thread_id(m.thread_id) |
|---|
| 81 | , msg_id(m.msg_id) |
|---|
| 82 | , source(m.source) |
|---|
| 83 | , worker_ptr(std::move(worker)) |
|---|
| 84 | { |
|---|
| 85 | fmt_helper::append_string_view(m.payload, raw); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | async_msg(async_logger_ptr &&worker, async_msg_type the_type) |
|---|
| 89 | : msg_type(the_type) |
|---|
| 90 | , level(level::off) |
|---|
| 91 | , time() |
|---|
| 92 | , thread_id(0) |
|---|
| 93 | , msg_id(0) |
|---|
| 94 | , source() |
|---|
| 95 | , worker_ptr(std::move(worker)) |
|---|
| 96 | { |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | explicit async_msg(async_msg_type the_type) |
|---|
| 100 | : async_msg(nullptr, the_type) |
|---|
| 101 | { |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | // copy into log_msg |
|---|
| 105 | log_msg to_log_msg() |
|---|
| 106 | { |
|---|
| 107 | log_msg msg(&worker_ptr->name(), level, string_view_t(raw.data(), raw.size())); |
|---|
| 108 | msg.time = time; |
|---|
| 109 | msg.thread_id = thread_id; |
|---|
| 110 | msg.msg_id = msg_id; |
|---|
| 111 | msg.source = source; |
|---|
| 112 | msg.color_range_start = 0; |
|---|
| 113 | msg.color_range_end = 0; |
|---|
| 114 | return msg; |
|---|
| 115 | } |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | class thread_pool |
|---|
| 119 | { |
|---|
| 120 | public: |
|---|
| 121 | using item_type = async_msg; |
|---|
| 122 | using q_type = details::mpmc_blocking_queue<item_type>; |
|---|
| 123 | |
|---|
| 124 | thread_pool(size_t q_max_items, size_t threads_n) |
|---|
| 125 | : q_(q_max_items) |
|---|
| 126 | { |
|---|
| 127 | // std::cout << "thread_pool() q_size_bytes: " << q_size_bytes << |
|---|
| 128 | // "\tthreads_n: " << threads_n << std::endl; |
|---|
| 129 | if (threads_n == 0 || threads_n > 1000) |
|---|
| 130 | { |
|---|
| 131 | throw spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid " |
|---|
| 132 | "range is 1-1000)"); |
|---|
| 133 | } |
|---|
| 134 | for (size_t i = 0; i < threads_n; i++) |
|---|
| 135 | { |
|---|
| 136 | threads_.emplace_back(&thread_pool::worker_loop_, this); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | // message all threads to terminate gracefully join them |
|---|
| 141 | ~thread_pool() |
|---|
| 142 | { |
|---|
| 143 | try |
|---|
| 144 | { |
|---|
| 145 | for (size_t i = 0; i < threads_.size(); i++) |
|---|
| 146 | { |
|---|
| 147 | post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | for (auto &t : threads_) |
|---|
| 151 | { |
|---|
| 152 | t.join(); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | catch (...) |
|---|
| 156 | { |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | thread_pool(const thread_pool &) = delete; |
|---|
| 161 | thread_pool &operator=(thread_pool &&) = delete; |
|---|
| 162 | |
|---|
| 163 | void post_log(async_logger_ptr &&worker_ptr, details::log_msg &msg, async_overflow_policy overflow_policy) |
|---|
| 164 | { |
|---|
| 165 | async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg); |
|---|
| 166 | post_async_msg_(std::move(async_m), overflow_policy); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy) |
|---|
| 170 | { |
|---|
| 171 | post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | size_t overrun_counter() |
|---|
| 175 | { |
|---|
| 176 | return q_.overrun_counter(); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | private: |
|---|
| 180 | q_type q_; |
|---|
| 181 | |
|---|
| 182 | std::vector<std::thread> threads_; |
|---|
| 183 | |
|---|
| 184 | void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy) |
|---|
| 185 | { |
|---|
| 186 | if (overflow_policy == async_overflow_policy::block) |
|---|
| 187 | { |
|---|
| 188 | q_.enqueue(std::move(new_msg)); |
|---|
| 189 | } |
|---|
| 190 | else |
|---|
| 191 | { |
|---|
| 192 | q_.enqueue_nowait(std::move(new_msg)); |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | void worker_loop_() |
|---|
| 197 | { |
|---|
| 198 | while (process_next_msg_()) {}; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | // process next message in the queue |
|---|
| 202 | // return true if this thread should still be active (while no terminate msg |
|---|
| 203 | // was received) |
|---|
| 204 | bool process_next_msg_() |
|---|
| 205 | { |
|---|
| 206 | async_msg incoming_async_msg; |
|---|
| 207 | bool dequeued = q_.dequeue_for(incoming_async_msg, std::chrono::seconds(10)); |
|---|
| 208 | if (!dequeued) |
|---|
| 209 | { |
|---|
| 210 | return true; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | switch (incoming_async_msg.msg_type) |
|---|
| 214 | { |
|---|
| 215 | case async_msg_type::log: |
|---|
| 216 | { |
|---|
| 217 | auto msg = incoming_async_msg.to_log_msg(); |
|---|
| 218 | incoming_async_msg.worker_ptr->backend_log_(msg); |
|---|
| 219 | return true; |
|---|
| 220 | } |
|---|
| 221 | case async_msg_type::flush: |
|---|
| 222 | { |
|---|
| 223 | incoming_async_msg.worker_ptr->backend_flush_(); |
|---|
| 224 | return true; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | case async_msg_type::terminate: |
|---|
| 228 | { |
|---|
| 229 | return false; |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | assert(false && "Unexpected async_msg_type"); |
|---|
| 233 | return true; |
|---|
| 234 | } |
|---|
| 235 | }; |
|---|
| 236 | |
|---|
| 237 | } // namespace details |
|---|
| 238 | } // namespace spdlog |
|---|