Agora  1.2.0
Agora project
ringbuffer_sink.h
Go to the documentation of this file.
1 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
3 
4 #pragma once
5 
10 
11 #include <mutex>
12 #include <string>
13 #include <vector>
14 
15 namespace spdlog {
16 namespace sinks {
17 /*
18  * Ring buffer sink
19  */
20 template<typename Mutex>
21 class ringbuffer_sink final : public base_sink<Mutex>
22 {
23 public:
24  explicit ringbuffer_sink(size_t n_items)
25  : q_{n_items}
26  {}
27 
28  std::vector<details::log_msg_buffer> last_raw(size_t lim = 0)
29  {
30  std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
31  auto items_available = q_.size();
32  auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available;
33  std::vector<details::log_msg_buffer> ret;
34  ret.reserve(n_items);
35  for (size_t i = (items_available - n_items); i < items_available; i++)
36  {
37  ret.push_back(q_.at(i));
38  }
39  return ret;
40  }
41 
42  std::vector<std::string> last_formatted(size_t lim = 0)
43  {
44  std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
45  auto items_available = q_.size();
46  auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available;
47  std::vector<std::string> ret;
48  ret.reserve(n_items);
49  for (size_t i = (items_available - n_items); i < items_available; i++)
50  {
51  memory_buf_t formatted;
52  base_sink<Mutex>::formatter_->format(q_.at(i), formatted);
53 #ifdef SPDLOG_USE_STD_FORMAT
54  ret.push_back(std::move(formatted));
55 #else
56  ret.push_back(fmt::to_string(formatted));
57 #endif
58  }
59  return ret;
60  }
61 
62 protected:
63  void sink_it_(const details::log_msg &msg) override
64  {
65  q_.push_back(details::log_msg_buffer{msg});
66  }
67  void flush_() override {}
68 
69 private:
71 };
72 
75 
76 } // namespace sinks
77 
78 } // namespace spdlog
circular_q.h
spdlog::sinks::ringbuffer_sink::sink_it_
void sink_it_(const details::log_msg &msg) override
Definition: ringbuffer_sink.h:63
base_sink.h
spdlog::sinks::ringbuffer_sink::ringbuffer_sink
ringbuffer_sink(size_t n_items)
Definition: ringbuffer_sink.h:24
spdlog::sinks::ringbuffer_sink::q_
details::circular_q< details::log_msg_buffer > q_
Definition: ringbuffer_sink.h:70
spdlog::sinks::ringbuffer_sink::flush_
void flush_() override
Definition: ringbuffer_sink.h:67
null_mutex.h
fmt::v8::basic_memory_buffer
Definition: format.h:677
spdlog
Definition: async.h:25
spdlog::sinks::base_sink
Definition: base_sink.h:19
i
for i
Definition: generate_data.m:107
fmt::v8::to_string
auto to_string(const T &value) -> std::string
Definition: format.h:2882
spdlog::details::log_msg_buffer
Definition: log_msg_buffer.h:14
spdlog::sinks::ringbuffer_sink::last_formatted
std::vector< std::string > last_formatted(size_t lim=0)
Definition: ringbuffer_sink.h:42
spdlog::details::log_msg
Definition: log_msg.h:11
spdlog::sinks::ringbuffer_sink::last_raw
std::vector< details::log_msg_buffer > last_raw(size_t lim=0)
Definition: ringbuffer_sink.h:28
log_msg_buffer.h
spdlog::sinks::ringbuffer_sink
Definition: ringbuffer_sink.h:21
spdlog::details::circular_q
Definition: circular_q.h:13