Photon 1.0.0
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
28{
29 namespace sinks
30 {
31
33 {
34 std::string server_host;
36 bool lazy_connect = false; // if true connect on first log call instead of on construction
37
38 tcp_sink_config(std::string host, int port)
39 : server_host{std::move(host)}, server_port{port}
40 {
41 }
42 };
43
44 template <typename Mutex>
45 class tcp_sink : public spdlog::sinks::base_sink<Mutex>
46 {
47 public:
48 // connect to tcp host/port or throw if failed
49 // host can be hostname or ip address
50
51 explicit tcp_sink(tcp_sink_config sink_config)
52 : config_{std::move(sink_config)}
53 {
55 {
57 }
58 }
59
60 ~tcp_sink() override = default;
61
62 protected:
63 void sink_it_(const spdlog::details::log_msg& msg) override
64 {
65 spdlog::memory_buf_t formatted;
66 spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
67 if (!client_.is_connected())
68 {
70 }
71 client_.send(formatted.data(), formatted.size());
72 }
73
74 void flush_() override
75 {
76 }
79 };
80
83
84 } // namespace sinks
85} // namespace spdlog
Definition tcp_client-windows.h:27
bool is_connected() const
Definition tcp_client-windows.h:61
void connect(const std::string &host, int port)
Definition tcp_client-windows.h:78
void send(const char *data, size_t n_bytes)
Definition tcp_client-windows.h:140
Definition base_sink.h:22
Definition tcp_sink.h:46
tcp_sink(tcp_sink_config sink_config)
Definition tcp_sink.h:51
void sink_it_(const spdlog::details::log_msg &msg) override
Definition tcp_sink.h:63
tcp_sink_config config_
Definition tcp_sink.h:77
void flush_() override
Definition tcp_sink.h:74
~tcp_sink() override=default
details::tcp_client client_
Definition tcp_sink.h:78
tcp_sink< spdlog::details::null_mutex > tcp_sink_st
Definition tcp_sink.h:82
tcp_sink< std::mutex > tcp_sink_mt
Definition tcp_sink.h:81
Definition async.h:26
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:173
Definition uuid.h:926
Definition log_msg.h:14
Definition tcp_sink.h:33
bool lazy_connect
Definition tcp_sink.h:36
int server_port
Definition tcp_sink.h:35
std::string server_host
Definition tcp_sink.h:34
tcp_sink_config(std::string host, int port)
Definition tcp_sink.h:38