Agora  1.2.0
Agora project
udp_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 
6 #include <spdlog/common.h>
9 #ifdef _WIN32
11 #else
13 #endif
14 
15 #include <mutex>
16 #include <string>
17 #include <chrono>
18 #include <functional>
19 
20 // Simple udp client sink
21 // Sends formatted log via udp
22 
23 namespace spdlog {
24 namespace sinks {
25 
27 {
28  std::string server_host;
29  uint16_t server_port;
30 
31  udp_sink_config(std::string host, uint16_t port)
32  : server_host{std::move(host)}
33  , server_port{port}
34  {}
35 };
36 
37 template<typename Mutex>
38 class udp_sink : public spdlog::sinks::base_sink<Mutex>
39 {
40 public:
41  // host can be hostname or ip address
42  explicit udp_sink(udp_sink_config sink_config)
43  : client_{sink_config.server_host, sink_config.server_port}
44  {}
45 
46  ~udp_sink() override = default;
47 
48 protected:
49  void sink_it_(const spdlog::details::log_msg &msg) override
50  {
51  spdlog::memory_buf_t formatted;
52  spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
53  client_.send(formatted.data(), formatted.size());
54  }
55 
56  void flush_() override {}
58 };
59 
62 
63 } // namespace sinks
64 
65 //
66 // factory functions
67 //
68 template<typename Factory = spdlog::synchronous_factory>
69 inline std::shared_ptr<logger> udp_logger_mt(const std::string &logger_name, sinks::udp_sink_config skin_config)
70 {
71  return Factory::template create<sinks::udp_sink_mt>(logger_name, skin_config);
72 }
73 
74 } // namespace spdlog
spdlog::sinks::udp_sink_config
Definition: udp_sink.h:26
spdlog::sinks::udp_sink::~udp_sink
~udp_sink() override=default
spdlog::sinks::udp_sink::flush_
void flush_() override
Definition: udp_sink.h:56
base_sink.h
spdlog::udp_logger_mt
std::shared_ptr< logger > udp_logger_mt(const std::string &logger_name, sinks::udp_sink_config skin_config)
Definition: udp_sink.h:69
spdlog::sinks::udp_sink_config::server_port
uint16_t server_port
Definition: udp_sink.h:29
fmt::v8::detail::buffer::size
constexpr auto size() const -> size_t
Definition: core.h:820
spdlog::sinks::udp_sink::sink_it_
void sink_it_(const spdlog::details::log_msg &msg) override
Definition: udp_sink.h:49
spdlog::sinks::udp_sink::udp_sink
udp_sink(udp_sink_config sink_config)
Definition: udp_sink.h:42
spdlog::details::udp_client
Definition: udp_client-windows.h:24
null_mutex.h
fmt::v8::basic_memory_buffer
Definition: format.h:677
udp_client.h
spdlog
Definition: async.h:25
spdlog::sinks::base_sink
Definition: base_sink.h:19
udp_client-windows.h
spdlog::details::udp_client::send
void send(const char *data, size_t n_bytes)
Definition: udp_client-windows.h:101
spdlog::sinks::udp_sink_config::server_host
std::string server_host
Definition: udp_sink.h:28
fmt::v8::detail::buffer::data
auto data() -> T *
Definition: core.h:826
common.h
spdlog::details::log_msg
Definition: log_msg.h:11
spdlog::sinks::udp_sink
Definition: udp_sink.h:38
spdlog::sinks::udp_sink_config::udp_sink_config
udp_sink_config(std::string host, uint16_t port)
Definition: udp_sink.h:31
spdlog::sinks::udp_sink::client_
details::udp_client client_
Definition: udp_sink.h:57