spdlog
Loading...
Searching...
No Matches
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
27namespace spdlog {
28namespace sinks {
29
31{
34 bool lazy_connect = false; // if true connect on first log call instead of on construction
35
37 : server_host{std::move(host)}
38 , server_port{port}
39 {}
40};
41
42template<typename Mutex>
44{
45public:
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 {
53 {
55 }
56 }
57
58 ~tcp_sink() override = default;
59
60protected:
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
void connect(const std::string &host, int port)
void send(const char *data, size_t n_bytes)
tcp_sink(tcp_sink_config sink_config)
Definition tcp_sink.h:49
void sink_it_(const spdlog::details::log_msg &msg) override
Definition tcp_sink.h:61
tcp_sink_config config_
Definition tcp_sink.h:73
void flush_() override
Definition tcp_sink.h:72
~tcp_sink() override=default
details::tcp_client client_
Definition tcp_sink.h:74
Definition async.h:25
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:116
tcp_sink_config(std::string host, int port)
Definition tcp_sink.h:36