From 346b9ae5a1bfdf038cd5721c734b300cbef40db8 Mon Sep 17 00:00:00 2001 From: "v.reshetnikov" Date: Mon, 10 Feb 2020 14:24:17 +0300 Subject: [PATCH 1/5] 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; From ccad4ae04f23c871fdbad8fae61dd97333e58e8e Mon Sep 17 00:00:00 2001 From: Vyacheslav Date: Mon, 10 Feb 2020 14:46:39 +0300 Subject: [PATCH 2/5] Resolve modification requests --- include/spdlog/tcp_sink.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/include/spdlog/tcp_sink.h b/include/spdlog/tcp_sink.h index a52a9991..36ec4324 100644 --- a/include/spdlog/tcp_sink.h +++ b/include/spdlog/tcp_sink.h @@ -13,17 +13,17 @@ public: { if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - printf("\n Socket creation error \n"); + SPDLOG_THROW(spdlog_ex("Socket creation error", errno)); } 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"); + SPDLOG_THROW(spdlog_ex("Invalid address/ Address not supported", errno)); } if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { - printf("\nConnection Failed \n"); + SPDLOG_THROW(spdlog_ex("Connection Failed", errno)); } } @@ -32,8 +32,7 @@ protected: { 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 ); + send(sock , formatted.data() , formatted.size() , 0 ); } void flush_() override From 3aa94a099778714256179f0f652c15e539a37bce Mon Sep 17 00:00:00 2001 From: Vyacheslav Date: Mon, 10 Feb 2020 14:58:54 +0300 Subject: [PATCH 3/5] Added send function verification + licence --- include/spdlog/tcp_sink.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/spdlog/tcp_sink.h b/include/spdlog/tcp_sink.h index 36ec4324..1c44d644 100644 --- a/include/spdlog/tcp_sink.h +++ b/include/spdlog/tcp_sink.h @@ -1,3 +1,5 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) #include #include @@ -32,7 +34,9 @@ protected: { spdlog::memory_buf_t formatted; spdlog::sinks::base_sink::formatter_->format(msg, formatted); - send(sock , formatted.data() , formatted.size() , 0 ); + int res = send(sock , formatted.data() , formatted.size() , 0 ); + if(res < 0) + SPDLOG_THROW(spdlog_ex("Message Send Failed", errno)); } void flush_() override From 4bb623a0a3fda0b987c5e9454f20837cfa674c85 Mon Sep 17 00:00:00 2001 From: Vyacheslav Date: Mon, 10 Feb 2020 15:08:51 +0300 Subject: [PATCH 4/5] removed unneccessary namespace std --- include/spdlog/tcp_sink.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/include/spdlog/tcp_sink.h b/include/spdlog/tcp_sink.h index 1c44d644..dff51853 100644 --- a/include/spdlog/tcp_sink.h +++ b/include/spdlog/tcp_sink.h @@ -3,10 +3,11 @@ #include #include +#include #include #include #include -using namespace std; + template class tcp_sink : public spdlog::sinks::base_sink { @@ -15,17 +16,17 @@ public: { if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - SPDLOG_THROW(spdlog_ex("Socket creation error", errno)); + SPDLOG_THROW(spdlog::spdlog_ex("Socket creation error", errno)); } 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) { - SPDLOG_THROW(spdlog_ex("Invalid address/ Address not supported", errno)); + SPDLOG_THROW(spdlog::spdlog_ex("Invalid address/ Address not supported", errno)); } if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { - SPDLOG_THROW(spdlog_ex("Connection Failed", errno)); + SPDLOG_THROW(spdlog::spdlog_ex("Connection Failed", errno)); } } @@ -35,8 +36,8 @@ protected: spdlog::memory_buf_t formatted; spdlog::sinks::base_sink::formatter_->format(msg, formatted); int res = send(sock , formatted.data() , formatted.size() , 0 ); - if(res < 0) - SPDLOG_THROW(spdlog_ex("Message Send Failed", errno)); + if(res < 0) + SPDLOG_THROW(spdlog::spdlog_ex("Message Send Failed", errno)); } void flush_() override From d96d8c49ac646dee98c9d97ddeb4e0f88d1f534b Mon Sep 17 00:00:00 2001 From: Vyacheslav Date: Mon, 10 Feb 2020 15:13:39 +0300 Subject: [PATCH 5/5] Code Style naming --- include/spdlog/tcp_sink.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/spdlog/tcp_sink.h b/include/spdlog/tcp_sink.h index dff51853..f581b71d 100644 --- a/include/spdlog/tcp_sink.h +++ b/include/spdlog/tcp_sink.h @@ -14,17 +14,17 @@ 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) + if ((sock_ = socket(AF_INET, SOCK_STREAM, 0)) < 0) { SPDLOG_THROW(spdlog::spdlog_ex("Socket creation error", errno)); } - 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) + 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) { SPDLOG_THROW(spdlog::spdlog_ex("Invalid address/ Address not supported", errno)); } - if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) + if (connect(sock_, (struct sockaddr *)&serv_addr_, sizeof(serv_addr_)) < 0) { SPDLOG_THROW(spdlog::spdlog_ex("Connection Failed", errno)); } @@ -35,7 +35,7 @@ protected: { spdlog::memory_buf_t formatted; spdlog::sinks::base_sink::formatter_->format(msg, formatted); - int res = send(sock , formatted.data() , formatted.size() , 0 ); + int res = send(sock_ , formatted.data() , formatted.size() , 0 ); if(res < 0) SPDLOG_THROW(spdlog::spdlog_ex("Message Send Failed", errno)); } @@ -44,8 +44,8 @@ protected: { } private: - int sock; - struct sockaddr_in serv_addr; + int sock_; + struct sockaddr_in serv_addr_; }; using tcp_sink_mt = tcp_sink; using tcp_sink_st = tcp_sink;