Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // Copyright(c) 2015 Gabi Melman.
  3. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  4. //
  5. // async log helper :
  6. // Process logs asynchronously using a back thread.
  7. //
  8. // If the internal queue of log messages reaches its max size,
  9. // then the client call will block until there is more room.
  10. //
  11. // If the back thread throws during logging, a spdlog::spdlog_ex exception
  12. // will be thrown in client's thread when tries to log the next message
  13. #pragma once
  14. #include <spdlog/common.h>
  15. #include <spdlog/sinks/sink.h>
  16. #include <spdlog/details/mpmc_bounded_q.h>
  17. #include <spdlog/details/log_msg.h>
  18. #include <spdlog/details/os.h>
  19. #include <spdlog/formatter.h>
  20. #include <chrono>
  21. #include <exception>
  22. #include <functional>
  23. #include <memory>
  24. #include <string>
  25. #include <thread>
  26. #include <utility>
  27. #include <vector>
  28. namespace spdlog
  29. {
  30. namespace details
  31. {
  32. class async_log_helper
  33. {
  34. // Async msg to move to/from the queue
  35. // Movable only. should never be copied
  36. enum class async_msg_type
  37. {
  38. log,
  39. flush,
  40. terminate
  41. };
  42. struct async_msg
  43. {
  44. std::string logger_name;
  45. level::level_enum level;
  46. log_clock::time_point time;
  47. size_t thread_id;
  48. std::string txt;
  49. async_msg_type msg_type;
  50. async_msg() = default;
  51. ~async_msg() = default;
  52. async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
  53. logger_name(std::move(other.logger_name)),
  54. level(std::move(other.level)),
  55. time(std::move(other.time)),
  56. txt(std::move(other.txt)),
  57. msg_type(std::move(other.msg_type))
  58. {}
  59. async_msg(async_msg_type m_type) :msg_type(m_type)
  60. {};
  61. async_msg& operator=(async_msg&& other) SPDLOG_NOEXCEPT
  62. {
  63. logger_name = std::move(other.logger_name);
  64. level = other.level;
  65. time = std::move(other.time);
  66. thread_id = other.thread_id;
  67. txt = std::move(other.txt);
  68. msg_type = other.msg_type;
  69. return *this;
  70. }
  71. // never copy or assign. should only be moved..
  72. async_msg(const async_msg&) = delete;
  73. async_msg& operator=(async_msg& other) = delete;
  74. // construct from log_msg
  75. async_msg(const details::log_msg& m) :
  76. level(m.level),
  77. time(m.time),
  78. thread_id(m.thread_id),
  79. txt(m.raw.data(), m.raw.size()),
  80. msg_type(async_msg_type::log)
  81. {
  82. #ifndef SPDLOG_NO_NAME
  83. logger_name = *m.logger_name;
  84. #endif
  85. }
  86. // copy into log_msg
  87. void fill_log_msg(log_msg &msg)
  88. {
  89. msg.logger_name = &logger_name;
  90. msg.level = level;
  91. msg.time = time;
  92. msg.thread_id = thread_id;
  93. msg.raw << txt;
  94. }
  95. };
  96. public:
  97. using item_type = async_msg;
  98. using q_type = details::mpmc_bounded_queue<item_type>;
  99. using clock = std::chrono::steady_clock;
  100. async_log_helper(formatter_ptr formatter,
  101. const std::vector<sink_ptr>& sinks,
  102. size_t queue_size,
  103. const log_err_handler err_handler,
  104. const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
  105. const std::function<void()>& worker_warmup_cb = nullptr,
  106. const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
  107. const std::function<void()>& worker_teardown_cb = nullptr);
  108. void log(const details::log_msg& msg);
  109. // stop logging and join the back thread
  110. ~async_log_helper();
  111. void set_formatter(formatter_ptr);
  112. void flush(bool wait_for_q);
  113. private:
  114. formatter_ptr _formatter;
  115. std::vector<std::shared_ptr<sinks::sink>> _sinks;
  116. // queue of messages to log
  117. q_type _q;
  118. log_err_handler _err_handler;
  119. bool _flush_requested;
  120. bool _terminate_requested;
  121. // overflow policy
  122. const async_overflow_policy _overflow_policy;
  123. // worker thread warmup callback - one can set thread priority, affinity, etc
  124. const std::function<void()> _worker_warmup_cb;
  125. // auto periodic sink flush parameter
  126. const std::chrono::milliseconds _flush_interval_ms;
  127. // worker thread teardown callback
  128. const std::function<void()> _worker_teardown_cb;
  129. // worker thread
  130. std::thread _worker_thread;
  131. void push_msg(async_msg&& new_msg);
  132. // worker thread main loop
  133. void worker_loop();
  134. // pop next message from the queue and process it. will set the last_pop to the pop time
  135. // return false if termination of the queue is required
  136. bool process_next_msg(log_clock::time_point& last_pop, log_clock::time_point& last_flush);
  137. void handle_flush_interval(log_clock::time_point& now, log_clock::time_point& last_flush);
  138. // sleep,yield or return immediatly using the time passed since last message as a hint
  139. static void sleep_or_yield(const spdlog::log_clock::time_point& now, const log_clock::time_point& last_op_time);
  140. // wait until the queue is empty
  141. void wait_empty_q();
  142. };
  143. }
  144. }
  145. ///////////////////////////////////////////////////////////////////////////////
  146. // async_sink class implementation
  147. ///////////////////////////////////////////////////////////////////////////////
  148. inline spdlog::details::async_log_helper::async_log_helper(
  149. formatter_ptr formatter,
  150. const std::vector<sink_ptr>& sinks,
  151. size_t queue_size,
  152. log_err_handler err_handler,
  153. const async_overflow_policy overflow_policy,
  154. const std::function<void()>& worker_warmup_cb,
  155. const std::chrono::milliseconds& flush_interval_ms,
  156. const std::function<void()>& worker_teardown_cb):
  157. _formatter(formatter),
  158. _sinks(sinks),
  159. _q(queue_size),
  160. _err_handler(err_handler),
  161. _flush_requested(false),
  162. _terminate_requested(false),
  163. _overflow_policy(overflow_policy),
  164. _worker_warmup_cb(worker_warmup_cb),
  165. _flush_interval_ms(flush_interval_ms),
  166. _worker_teardown_cb(worker_teardown_cb),
  167. _worker_thread(&async_log_helper::worker_loop, this)
  168. {}
  169. // Send to the worker thread termination message(level=off)
  170. // and wait for it to finish gracefully
  171. inline spdlog::details::async_log_helper::~async_log_helper()
  172. {
  173. try
  174. {
  175. push_msg(async_msg(async_msg_type::terminate));
  176. _worker_thread.join();
  177. }
  178. catch (...) // don't crash in destructor
  179. {}
  180. }
  181. //Try to push and block until succeeded (if the policy is not to discard when the queue is full)
  182. inline void spdlog::details::async_log_helper::log(const details::log_msg& msg)
  183. {
  184. push_msg(async_msg(msg));
  185. }
  186. inline void spdlog::details::async_log_helper::push_msg(details::async_log_helper::async_msg&& new_msg)
  187. {
  188. if (!_q.enqueue(std::move(new_msg)) && _overflow_policy != async_overflow_policy::discard_log_msg)
  189. {
  190. auto last_op_time = details::os::now();
  191. auto now = last_op_time;
  192. do
  193. {
  194. now = details::os::now();
  195. sleep_or_yield(now, last_op_time);
  196. }
  197. while (!_q.enqueue(std::move(new_msg)));
  198. }
  199. }
  200. // optionally wait for the queue be empty and request flush from the sinks
  201. inline void spdlog::details::async_log_helper::flush(bool wait_for_q)
  202. {
  203. push_msg(async_msg(async_msg_type::flush));
  204. if(wait_for_q)
  205. wait_empty_q(); //return only make after the above flush message was processed
  206. }
  207. inline void spdlog::details::async_log_helper::worker_loop()
  208. {
  209. try
  210. {
  211. if (_worker_warmup_cb) _worker_warmup_cb();
  212. auto last_pop = details::os::now();
  213. auto last_flush = last_pop;
  214. while(process_next_msg(last_pop, last_flush));
  215. if (_worker_teardown_cb) _worker_teardown_cb();
  216. }
  217. catch (const std::exception &ex)
  218. {
  219. _err_handler(ex.what());
  220. }
  221. catch (...)
  222. {
  223. _err_handler("Unknown exception");
  224. }
  225. }
  226. // process next message in the queue
  227. // return true if this thread should still be active (while no terminate msg was received)
  228. inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_point& last_pop, log_clock::time_point& last_flush)
  229. {
  230. async_msg incoming_async_msg;
  231. if (_q.dequeue(incoming_async_msg))
  232. {
  233. last_pop = details::os::now();
  234. switch (incoming_async_msg.msg_type)
  235. {
  236. case async_msg_type::flush:
  237. _flush_requested = true;
  238. break;
  239. case async_msg_type::terminate:
  240. _flush_requested = true;
  241. _terminate_requested = true;
  242. break;
  243. default:
  244. log_msg incoming_log_msg;
  245. incoming_async_msg.fill_log_msg(incoming_log_msg);
  246. _formatter->format(incoming_log_msg);
  247. for (auto &s : _sinks)
  248. {
  249. if(s->should_log( incoming_log_msg.level))
  250. {
  251. s->log(incoming_log_msg);
  252. }
  253. }
  254. }
  255. return true;
  256. }
  257. // Handle empty queue..
  258. // This is the only place where the queue can terminate or flush to avoid losing messages already in the queue
  259. else
  260. {
  261. auto now = details::os::now();
  262. handle_flush_interval(now, last_flush);
  263. sleep_or_yield(now, last_pop);
  264. return !_terminate_requested;
  265. }
  266. }
  267. // flush all sinks if _flush_interval_ms has expired
  268. inline void spdlog::details::async_log_helper::handle_flush_interval(log_clock::time_point& now, log_clock::time_point& last_flush)
  269. {
  270. auto should_flush = _flush_requested || (_flush_interval_ms != std::chrono::milliseconds::zero() && now - last_flush >= _flush_interval_ms);
  271. if (should_flush)
  272. {
  273. for (auto &s : _sinks)
  274. s->flush();
  275. now = last_flush = details::os::now();
  276. _flush_requested = false;
  277. }
  278. }
  279. inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_formatter)
  280. {
  281. _formatter = msg_formatter;
  282. }
  283. // spin, yield or sleep. use the time passed since last message as a hint
  284. inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_clock::time_point& now, const spdlog::log_clock::time_point& last_op_time)
  285. {
  286. using namespace std::this_thread;
  287. using std::chrono::milliseconds;
  288. using std::chrono::microseconds;
  289. auto time_since_op = now - last_op_time;
  290. // spin upto 50 micros
  291. if (time_since_op <= microseconds(50))
  292. return;
  293. // yield upto 150 micros
  294. if (time_since_op <= microseconds(100))
  295. return yield();
  296. // sleep for 20 ms upto 200 ms
  297. if (time_since_op <= milliseconds(200))
  298. return sleep_for(milliseconds(20));
  299. // sleep for 200 ms
  300. return sleep_for(milliseconds(200));
  301. }
  302. // wait for the queue to be empty
  303. inline void spdlog::details::async_log_helper::wait_empty_q()
  304. {
  305. auto last_op = details::os::now();
  306. while (_q.approx_size() > 0)
  307. {
  308. sleep_or_yield(details::os::now(), last_op);
  309. }
  310. }