Fix udp sink on Windows

This commit is contained in:
Chris Love 2021-08-26 06:35:28 -07:00
parent 410e641dff
commit a15f5137ef
3 changed files with 15 additions and 6 deletions

View File

@ -200,7 +200,7 @@ void trace_example()
// stopwatch example // stopwatch example
#include "spdlog/stopwatch.h" #include "spdlog/stopwatch.h"
#include <thread> #include <thread>
#include "spdlog/sinks/udp_sink.h"
void stopwatch_example() void stopwatch_example()
{ {
spdlog::stopwatch sw; spdlog::stopwatch sw;
@ -208,11 +208,11 @@ void stopwatch_example()
spdlog::info("Stopwatch: {} seconds", sw); spdlog::info("Stopwatch: {} seconds", sw);
} }
// udp sink example
#include "spdlog/sinks/udp_sink.h"
void udp_example() void udp_example()
{ {
// using spdlog::details::make_unique; spdlog::sinks::udp_sink_config cfg("192.168.1.129", 11091);
//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);
auto my_logger = spdlog::udp_logger_mt("udplog", cfg); auto my_logger = spdlog::udp_logger_mt("udplog", cfg);
my_logger->set_level(spdlog::level::debug); my_logger->set_level(spdlog::level::debug);
my_logger->info("hello world"); my_logger->info("hello world");

View File

@ -82,6 +82,15 @@ public:
addr_.sin_family = AF_INET; addr_.sin_family = AF_INET;
addr_.sin_port = htons(port); addr_.sin_port = htons(port);
InetPton(AF_INET, TEXT(host.c_str()), &addr_.sin_addr.s_addr); 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; return true;
} }
@ -104,7 +113,7 @@ public:
void send(const char *data, size_t n_bytes) 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<int>(n_bytes), 0, (struct sockaddr *)&addr_, sizeof(struct sockaddr))) == -1)
{ {
throw_spdlog_ex("sendto(2) failed", errno); throw_spdlog_ex("sendto(2) failed", errno);
close(); close();

View File

@ -7,7 +7,7 @@
#include <spdlog/sinks/base_sink.h> #include <spdlog/sinks/base_sink.h>
#include <spdlog/details/null_mutex.h> #include <spdlog/details/null_mutex.h>
#ifdef _WIN32 #ifdef _WIN32
# include <spdlog/details/udp_client_windows.h> # include <spdlog/details/udp_client-windows.h>
#else #else
# include <spdlog/details/udp_client.h> # include <spdlog/details/udp_client.h>
#endif #endif