mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 10:01:33 +08:00
Fixed tcp_sink to accept hostnames
This commit is contained in:
parent
66e8652862
commit
7f8169f0da
@ -8,6 +8,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
@ -22,12 +23,16 @@ public:
|
||||
{
|
||||
SPDLOG_THROW(spdlog::spdlog_ex("Socket creation error", errno));
|
||||
}
|
||||
serv_addr_.sin_family = AF_INET;
|
||||
serv_addr_.sin_port = ::htons(static_cast<uint16_t>(port));
|
||||
if (inet_pton(AF_INET, address.c_str(), &serv_addr_.sin_addr) <= 0)
|
||||
struct hostent *he = gethostbyname(address.c_str());
|
||||
if (he == nullptr)
|
||||
{
|
||||
SPDLOG_THROW(spdlog::spdlog_ex("Invalid address/ Address not supported", errno));
|
||||
SPDLOG_THROW(spdlog::spdlog_ex("gethostbyname failed", errno));
|
||||
}
|
||||
|
||||
serv_addr_.sin_family = AF_INET;
|
||||
|
||||
serv_addr_.sin_addr = *(struct in_addr *)(he->h_addr);
|
||||
serv_addr_.sin_port = ::htons(static_cast<uint16_t>(port));
|
||||
if (connect(sock_, (struct sockaddr *)&serv_addr_, sizeof(serv_addr_)) < 0)
|
||||
{
|
||||
SPDLOG_THROW(spdlog::spdlog_ex("Connection Failed", errno));
|
||||
@ -48,6 +53,8 @@ protected:
|
||||
|
||||
void flush_() override {}
|
||||
|
||||
private:
|
||||
|
||||
private:
|
||||
int sock_;
|
||||
struct sockaddr_in serv_addr_;
|
||||
|
Loading…
Reference in New Issue
Block a user