From a15f5137efca7d29b87e5b82cd449525456414a9 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Thu, 26 Aug 2021 06:35:28 -0700 Subject: [PATCH] Fix udp sink on Windows --- example/example.cpp | 8 ++++---- .../{udp_client_windows.h => udp_client-windows.h} | 11 ++++++++++- include/spdlog/sinks/udp_sink.h | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) rename include/spdlog/details/{udp_client_windows.h => udp_client-windows.h} (85%) diff --git a/example/example.cpp b/example/example.cpp index 7d7f9bf9..c765ecf9 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -200,7 +200,7 @@ void trace_example() // stopwatch example #include "spdlog/stopwatch.h" #include -#include "spdlog/sinks/udp_sink.h" + void stopwatch_example() { spdlog::stopwatch sw; @@ -208,11 +208,11 @@ void stopwatch_example() spdlog::info("Stopwatch: {} seconds", sw); } +// udp sink example +#include "spdlog/sinks/udp_sink.h" void udp_example() { - // using spdlog::details::make_unique; - //auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30); - spdlog::sinks::udp_sink_config cfg("127.0.0.1", 11091); + spdlog::sinks::udp_sink_config cfg("192.168.1.129", 11091); auto my_logger = spdlog::udp_logger_mt("udplog", cfg); my_logger->set_level(spdlog::level::debug); my_logger->info("hello world"); diff --git a/include/spdlog/details/udp_client_windows.h b/include/spdlog/details/udp_client-windows.h similarity index 85% rename from include/spdlog/details/udp_client_windows.h rename to include/spdlog/details/udp_client-windows.h index 82d800a6..4ac37222 100644 --- a/include/spdlog/details/udp_client_windows.h +++ b/include/spdlog/details/udp_client-windows.h @@ -82,6 +82,15 @@ public: addr_.sin_family = AF_INET; addr_.sin_port = htons(port); InetPton(AF_INET, TEXT(host.c_str()), &addr_.sin_addr.s_addr); + + socket_ = socket(PF_INET, SOCK_DGRAM, 0); + if (socket_ == INVALID_SOCKET) + { + int last_error = ::WSAGetLastError(); + WSACleanup(); + throw_winsock_error_("error: Create Socket failed", last_error); + return false; + } return true; } @@ -104,7 +113,7 @@ public: void send(const char *data, size_t n_bytes) { - if ((sendto(socket_, data, n_bytes, 0, (struct sockaddr *)&addr_, sizeof(struct sockaddr))) == -1) + if ((sendto(socket_, data, static_cast(n_bytes), 0, (struct sockaddr *)&addr_, sizeof(struct sockaddr))) == -1) { throw_spdlog_ex("sendto(2) failed", errno); close(); diff --git a/include/spdlog/sinks/udp_sink.h b/include/spdlog/sinks/udp_sink.h index fd57fd73..961d754d 100644 --- a/include/spdlog/sinks/udp_sink.h +++ b/include/spdlog/sinks/udp_sink.h @@ -7,7 +7,7 @@ #include #include #ifdef _WIN32 -# include +# include #else # include #endif