诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

73 line
1.9 KiB

  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #include "spdlog/sinks/base_sink.h"
  5. #include "spdlog/details/circular_q.h"
  6. #include "spdlog/details/log_msg_buffer.h"
  7. #include "spdlog/details/null_mutex.h"
  8. #include <mutex>
  9. #include <string>
  10. #include <vector>
  11. namespace spdlog {
  12. namespace sinks {
  13. /*
  14. * Ring buffer sink
  15. */
  16. template<typename Mutex>
  17. class ringbuffer_sink final : public base_sink<Mutex>
  18. {
  19. public:
  20. explicit ringbuffer_sink(size_t n_items)
  21. : q_{n_items}
  22. {}
  23. std::vector<details::log_msg_buffer> last_raw(size_t lim = 0)
  24. {
  25. std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
  26. auto n_items = lim > 0 ? (std::min)(lim, q_.size()) : q_.size();
  27. std::vector<details::log_msg_buffer> ret;
  28. ret.reserve(n_items);
  29. for (size_t i = 0; i < n_items; i++)
  30. {
  31. ret.push_back(q_.at(i));
  32. }
  33. return ret;
  34. }
  35. std::vector<std::string> last_formatted(size_t lim = 0)
  36. {
  37. std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
  38. auto n_items = lim > 0 ? (std::min)(lim, q_.size()) : q_.size();
  39. std::vector<std::string> ret;
  40. ret.reserve(n_items);
  41. for (size_t i = 0; i < n_items; i++)
  42. {
  43. memory_buf_t formatted;
  44. base_sink<Mutex>::formatter_->format(q_.at(i), formatted);
  45. ret.push_back(fmt::to_string(formatted));
  46. }
  47. return ret;
  48. }
  49. protected:
  50. void sink_it_(const details::log_msg &msg) override
  51. {
  52. q_.push_back(details::log_msg_buffer{msg});
  53. }
  54. void flush_() override {}
  55. private:
  56. details::circular_q<details::log_msg_buffer> q_;
  57. };
  58. using ringbuffer_sink_mt = ringbuffer_sink<std::mutex>;
  59. using ringbuffer_sink_st = ringbuffer_sink<details::null_mutex>;
  60. } // namespace sinks
  61. } // namespace spdlog