From 346b9ae5a1bfdf038cd5721c734b300cbef40db8 Mon Sep 17 00:00:00 2001 From: "v.reshetnikov" Date: Mon, 10 Feb 2020 14:24:17 +0300 Subject: [PATCH] tcp_sink implementation for fluentbit --- include/spdlog/tcp_sink.h | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 include/spdlog/tcp_sink.h diff --git a/include/spdlog/tcp_sink.h b/include/spdlog/tcp_sink.h new file mode 100644 index 00000000..a52a9991 --- /dev/null +++ b/include/spdlog/tcp_sink.h @@ -0,0 +1,47 @@ + +#include +#include +#include +#include +#include +using namespace std; +template +class tcp_sink : public spdlog::sinks::base_sink +{ +public: + tcp_sink(std::string address,int port) + { + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + printf("\n Socket creation error \n"); + } + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(port); + if(inet_pton(AF_INET, address.c_str(), &serv_addr.sin_addr)<=0) + { + printf("\nInvalid address/ Address not supported \n"); + } + if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) + { + printf("\nConnection Failed \n"); + } + + } +protected: + void sink_it_(const spdlog::details::log_msg& msg) override + { + spdlog::memory_buf_t formatted; + spdlog::sinks::base_sink::formatter_->format(msg, formatted); + std::string message = fmt::to_string(formatted); + send(sock , message.c_str() , message.size() , 0 ); + } + + void flush_() override + { + } +private: + int sock; + struct sockaddr_in serv_addr; +}; +using tcp_sink_mt = tcp_sink; +using tcp_sink_st = tcp_sink;