mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-16 00:45:48 +08:00
Fix tcp_sink
This commit is contained in:
parent
05cbdbc1ef
commit
66e8652862
@ -1,29 +1,30 @@
|
|||||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
#include <spdlog/spdlog.h>
|
#pragma once
|
||||||
|
|
||||||
|
#include <spdlog/common.h>
|
||||||
#include <spdlog/sinks/base_sink.h>
|
#include <spdlog/sinks/base_sink.h>
|
||||||
#include <spdlog/common-inl.h>
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
|
|
||||||
template<typename Mutex>
|
template<typename Mutex>
|
||||||
class tcp_sink : public spdlog::sinks::base_sink <Mutex>
|
class tcp_sink : public spdlog::sinks::base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
tcp_sink(std::string address,int port)
|
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));
|
SPDLOG_THROW(spdlog::spdlog_ex("Socket creation error", errno));
|
||||||
}
|
}
|
||||||
serv_addr_.sin_family = AF_INET;
|
serv_addr_.sin_family = AF_INET;
|
||||||
serv_addr_.sin_port = htons(port);
|
serv_addr_.sin_port = ::htons(static_cast<uint16_t>(port));
|
||||||
if(inet_pton(AF_INET, address.c_str(), &serv_addr_.sin_addr)<=0)
|
if (inet_pton(AF_INET, address.c_str(), &serv_addr_.sin_addr) <= 0)
|
||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog::spdlog_ex("Invalid address/ Address not supported", errno));
|
SPDLOG_THROW(spdlog::spdlog_ex("Invalid address/ Address not supported", errno));
|
||||||
}
|
}
|
||||||
@ -31,25 +32,29 @@ public:
|
|||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog::spdlog_ex("Connection Failed", errno));
|
SPDLOG_THROW(spdlog::spdlog_ex("Connection Failed", errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void sink_it_(const spdlog::details::log_msg& msg) override
|
void sink_it_(const spdlog::details::log_msg &msg) override
|
||||||
{
|
{
|
||||||
spdlog::memory_buf_t formatted;
|
spdlog::memory_buf_t formatted;
|
||||||
spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
|
spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||||
int res = send(sock_ , formatted.data() , formatted.size() , 0 );
|
auto res = ::send(sock_, formatted.data(), formatted.size(), 0);
|
||||||
if(res < 0)
|
if (res < 0)
|
||||||
|
{
|
||||||
SPDLOG_THROW(spdlog::spdlog_ex("Message Send Failed", errno));
|
SPDLOG_THROW(spdlog::spdlog_ex("Message Send Failed", errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
void flush_() override
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void flush_() override {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int sock_;
|
int sock_;
|
||||||
struct sockaddr_in serv_addr_;
|
struct sockaddr_in serv_addr_;
|
||||||
};
|
};
|
||||||
|
|
||||||
using tcp_sink_mt = tcp_sink<std::mutex>;
|
using tcp_sink_mt = tcp_sink<std::mutex>;
|
||||||
using tcp_sink_st = tcp_sink<spdlog::details::null_mutex>;
|
using tcp_sink_st = tcp_sink<spdlog::details::null_mutex>;
|
||||||
}}
|
|
||||||
|
} // namespace sinks
|
||||||
|
} // namespace spdlog
|
||||||
|
Loading…
Reference in New Issue
Block a user