From eef981e05f78c55f7fe1e53ebb2389ab538e3d2e Mon Sep 17 00:00:00 2001 From: dominicpoeschko <45942148+dominicpoeschko@users.noreply.github.com> Date: Mon, 10 Feb 2020 17:52:54 +0100 Subject: [PATCH] Handling SPDLOG_PREVENT_CHILD_FD in tcp_sink Adding SOCK_CLOEXEC to socket Fixing bug in sink_it_ (bytes_sent not added to buffer) --- include/spdlog/sinks/tcp_sink.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h index a44e6716..4b3a7457 100644 --- a/include/spdlog/sinks/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -47,7 +47,7 @@ protected: size_t bytes_sent = 0; while (bytes_sent < formatted.size()) { - auto write_result = ::write(sock_, formatted.data(), formatted.size() - bytes_sent); + auto write_result = ::write(sock_, formatted.data() + bytes_sent, formatted.size() - bytes_sent); if (write_result < 0) { SPDLOG_THROW(spdlog::spdlog_ex("write(2) failed", errno)); @@ -88,7 +88,12 @@ private: int last_errno = 0; for (auto *rp = addrinfo_result; rp != nullptr; rp = rp->ai_next) { - socket_rv = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + #ifdef SPDLOG_PREVENT_CHILD_FD + int const flags = SOCK_CLOEXEC; + #else + int const flags = 0; + #endif + socket_rv = ::socket(rp->ai_family, rp->ai_socktype | flags, rp->ai_protocol); if (socket_rv == -1) { last_errno = errno;