Merge pull request #2090 from CJLove/v1.x

Add udp_sink
This commit is contained in:
Gabi Melman 2021-09-05 10:25:09 +03:00 committed by GitHub
commit 5906ce844a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 315 additions and 0 deletions

View File

@ -5,6 +5,7 @@
// spdlog usage example
#include <cstdio>
#include <chrono>
void load_levels_example();
void stdout_logger_example();
@ -19,6 +20,7 @@ void multi_sink_example();
void user_defined_example();
void err_handler_example();
void syslog_example();
void udp_example();
void custom_flags_example();
#include "spdlog/spdlog.h"
@ -74,6 +76,7 @@ int main(int, char *[])
err_handler_example();
trace_example();
stopwatch_example();
udp_example();
custom_flags_example();
// Flush all *registered* loggers using a worker thread every 3 seconds.
@ -198,6 +201,7 @@ void trace_example()
// stopwatch example
#include "spdlog/stopwatch.h"
#include <thread>
#include "spdlog/sinks/udp_sink.h"
void stopwatch_example()
{
spdlog::stopwatch sw;
@ -205,6 +209,14 @@ void stopwatch_example()
spdlog::info("Stopwatch: {} seconds", sw);
}
void udp_example()
{
spdlog::sinks::udp_sink_config cfg("127.0.0.1", 11091);
auto my_logger = spdlog::udp_logger_mt("udplog", cfg);
my_logger->set_level(spdlog::level::debug);
my_logger->info("hello world");
}
// A logger with multiple sinks (stdout and file) - each with a different format and log level.
void multi_sink_example()
{

View File

@ -0,0 +1,137 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif // WIN32_LEAN_AND_MEAN
// udp client helper
#include <spdlog/common.h>
#include <spdlog/details/os.h>
#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Mswsock.lib")
#pragma comment(lib, "AdvApi32.lib")
namespace spdlog {
namespace details {
class udp_client
{
const int TX_BUFFER_SIZE = 10240;
SOCKET socket_ = INVALID_SOCKET;
sockaddr_in addr_ = { 0 };
static bool winsock_initialized_()
{
SOCKET s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s == INVALID_SOCKET)
{
return false;
}
else
{
closesocket(s);
return true;
}
}
static void init_winsock_()
{
WSADATA wsaData;
auto rv = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (rv != 0)
{
throw_winsock_error_("WSAStartup failed", ::WSAGetLastError());
}
}
static void throw_winsock_error_(const std::string &msg, int last_error)
{
char buf[512];
::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, (sizeof(buf) / sizeof(char)), NULL);
throw_spdlog_ex(fmt::format("udp_sink - {}: {}", msg, buf));
}
public:
bool is_init() const
{
return socket_ != INVALID_SOCKET;
}
bool init(const std::string &host, uint16_t port)
{
// initialize winsock if needed
if (!winsock_initialized_())
{
init_winsock_();
}
addr_.sin_family = PF_INET;
addr_.sin_port = htons(port);
addr_.sin_addr.s_addr = INADDR_ANY;
InetPton(PF_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);
}
int option_value = TX_BUFFER_SIZE;
if (setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, (char*)&option_value, sizeof(option_value)) < 0)
{
int last_error = ::WSAGetLastError();
close();
throw_winsock_error_("error: setsockopt(SO_SNDBUF) Failed!", last_error);
}
return true;
}
void close()
{
if (socket_ != -1)
{
::closesocket(socket_);
}
socket_ = INVALID_SOCKET;
WSACleanup();
}
SOCKET fd() const
{
return socket_;
}
~udp_client()
{
close();
}
void send(const char *data, size_t n_bytes)
{
socklen_t tolen = sizeof(struct sockaddr);
if (sendto(socket_, data, static_cast<int>(n_bytes), 0, (struct sockaddr *)&addr_, tolen) == -1)
{
close();
throw_spdlog_ex("sendto(2) failed", errno);
}
}
};
} // namespace details
} // namespace spdlog
#endif // _WIN32

View File

@ -0,0 +1,88 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#ifdef _WIN32
#error include udp_client-windows.h instead
#endif
// udp client helper
#include <spdlog/common.h>
#include <spdlog/details/os.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/udp.h>
#include <string>
namespace spdlog {
namespace details {
class udp_client
{
const int TX_BUFFER_SIZE = 10240;
int socket_ = -1;
struct sockaddr_in sockAddr_;
public:
bool init(const std::string &host, uint16_t port)
{
socket_ = socket(PF_INET, SOCK_DGRAM, 0);
if (socket_ < 0)
{
throw_spdlog_ex("error: Create Socket Failed!");
}
int option_value = TX_BUFFER_SIZE;
if (setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, (char*)&option_value, sizeof(option_value)) < 0)
{
close();
throw_spdlog_ex("error: setsockopt(SO_SNDBUF) Failed!");
}
sockAddr_.sin_family = AF_INET;
sockAddr_.sin_port = htons(port);
inet_aton(host.c_str(), &sockAddr_.sin_addr);
memset(sockAddr_.sin_zero, 0x00, sizeof(sockAddr_.sin_zero));
return true;
}
void close()
{
if (socket_ != -1)
{
::close(socket_);
socket_ = -1;
}
}
int fd() const
{
return socket_;
}
~udp_client()
{
close();
}
// Send exactly n_bytes of the given data.
// On error close the connection and throw.
void send(const char *data, size_t n_bytes)
{
ssize_t toslen = 0;
socklen_t tolen = sizeof(struct sockaddr);
if (( toslen = sendto(socket_, data, n_bytes, 0, (struct sockaddr *)&sockAddr_, tolen)) == -1)
{
close();
throw_spdlog_ex("sendto(2) failed", errno);
}
}
};
} // namespace details
} // namespace spdlog

View File

@ -0,0 +1,78 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/common.h>
#include <spdlog/sinks/base_sink.h>
#include <spdlog/details/null_mutex.h>
#ifdef _WIN32
# include <spdlog/details/udp_client-windows.h>
#else
# include <spdlog/details/udp_client.h>
#endif
#include <mutex>
#include <string>
#include <chrono>
#include <functional>
// Simple udp client sink
// Sends formatted log via udp
namespace spdlog {
namespace sinks {
struct udp_sink_config
{
std::string server_host;
uint16_t server_port;
udp_sink_config(std::string host, uint16_t port)
: server_host{std::move(host)}
, server_port{port}
{}
};
template<typename Mutex>
class udp_sink : public spdlog::sinks::base_sink<Mutex>
{
public:
// host can be hostname or ip address
explicit udp_sink(udp_sink_config sink_config)
: config_{std::move(sink_config)}
{
client_.init(config_.server_host, config_.server_port);
}
~udp_sink() override = default;
protected:
void sink_it_(const spdlog::details::log_msg &msg) override
{
spdlog::memory_buf_t formatted;
spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
client_.send(formatted.data(), formatted.size());
}
void flush_() override {}
udp_sink_config config_;
details::udp_client client_;
};
using udp_sink_mt = udp_sink<std::mutex>;
using udp_sink_st = udp_sink<spdlog::details::null_mutex>;
} // namespace sinks
//
// factory functions
//
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> udp_logger_mt(const std::string &logger_name, sinks::udp_sink_config skin_config)
{
return Factory::template create<sinks::udp_sink_mt>(logger_name, skin_config);
}
} // namespace spdlog