Agora  1.2.0
Agora project
tcp_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 #pragma once
21 
22 // Simple tcp client sink
23 // Connects to remote address and send the formatted log.
24 // Will attempt to reconnect if connection drops.
25 // If more complicated behaviour is needed (i.e get responses), you can inherit it and override the sink_it_ method.
26 
27 namespace spdlog {
28 namespace sinks {
29 
31 {
32  std::string server_host;
34  bool lazy_connect = false; // if true connect on first log call instead of on construction
35 
36  tcp_sink_config(std::string host, int port)
37  : server_host{std::move(host)}
38  , server_port{port}
39  {}
40 };
41 
42 template<typename Mutex>
43 class tcp_sink : public spdlog::sinks::base_sink<Mutex>
44 {
45 public:
46  // connect to tcp host/port or throw if failed
47  // host can be hostname or ip address
48 
49  explicit tcp_sink(tcp_sink_config sink_config)
50  : config_{std::move(sink_config)}
51  {
52  if (!config_.lazy_connect)
53  {
55  }
56  }
57 
58  ~tcp_sink() override = default;
59 
60 protected:
61  void sink_it_(const spdlog::details::log_msg &msg) override
62  {
63  spdlog::memory_buf_t formatted;
64  spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
65  if (!client_.is_connected())
66  {
68  }
69  client_.send(formatted.data(), formatted.size());
70  }
71 
72  void flush_() override {}
75 };
76 
79 
80 } // namespace sinks
81 } // namespace spdlog
spdlog::sinks::tcp_sink::flush_
void flush_() override
Definition: tcp_sink.h:72
spdlog::sinks::tcp_sink::client_
details::tcp_client client_
Definition: tcp_sink.h:74
spdlog::details::tcp_client::send
void send(const char *data, size_t n_bytes)
Definition: tcp_client-windows.h:137
base_sink.h
fmt::v8::detail::buffer::size
constexpr auto size() const -> size_t
Definition: core.h:820
spdlog::sinks::tcp_sink_config::server_port
int server_port
Definition: tcp_sink.h:33
spdlog::sinks::tcp_sink
Definition: tcp_sink.h:43
spdlog::sinks::tcp_sink::config_
tcp_sink_config config_
Definition: tcp_sink.h:73
spdlog::sinks::tcp_sink::~tcp_sink
~tcp_sink() override=default
spdlog::sinks::tcp_sink::tcp_sink
tcp_sink(tcp_sink_config sink_config)
Definition: tcp_sink.h:49
null_mutex.h
fmt::v8::basic_memory_buffer
Definition: format.h:677
spdlog
Definition: async.h:25
tcp_client-windows.h
spdlog::sinks::tcp_sink::sink_it_
void sink_it_(const spdlog::details::log_msg &msg) override
Definition: tcp_sink.h:61
spdlog::sinks::base_sink
Definition: base_sink.h:19
tcp_client.h
spdlog::sinks::tcp_sink_config::tcp_sink_config
tcp_sink_config(std::string host, int port)
Definition: tcp_sink.h:36
fmt::v8::detail::buffer::data
auto data() -> T *
Definition: core.h:826
spdlog::details::tcp_client::connect
void connect(const std::string &host, int port)
Definition: tcp_client-windows.h:76
common.h
spdlog::details::log_msg
Definition: log_msg.h:11
spdlog::details::tcp_client
Definition: tcp_client-windows.h:24
spdlog::sinks::tcp_sink_config::server_host
std::string server_host
Definition: tcp_sink.h:32
spdlog::details::tcp_client::is_connected
bool is_connected() const
Definition: tcp_client-windows.h:59
spdlog::sinks::tcp_sink_config::lazy_connect
bool lazy_connect
Definition: tcp_sink.h:34
spdlog::sinks::tcp_sink_config
Definition: tcp_sink.h:30