Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add udp_sink #2090

Merged
merged 11 commits into from
Sep 5, 2021
Merged

Add udp_sink #2090

merged 11 commits into from
Sep 5, 2021

Conversation

CJLove
Copy link
Contributor

@CJLove CJLove commented Sep 3, 2021

This PR updates the prior udp_sink PR (#1746), cleaning up some code and adding initial Windows support. Winsock seems more prone to drop UDP packets if sent too quickly, so this sink seems more usable on Linux. I'm not a Windows developer so I'm not sure if I'm missing something.

for (int i = 0; i < 10; i++) {
my_logger->info("hello world {}", i);
#ifdef _WIN32
// sendto() on winsock will drop packets if sent too quickly
Copy link
Owner

@gabime gabime Sep 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drops depends on lots of variables. Since this is just a sample I would keep it simple and remove the loop and the ifdef.

#pragma once

// Simple udp client sink
// Connects to remote address and send the formatted log.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no connection in udp. Please remove this comment


#pragma once

#define WIN32_LEAN_AND_MEAN
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please surround with #ifndef WIN32_LEAN_AND_CLEAN

if (sendto(socket_, data, static_cast<int>(n_bytes), 0, (struct sockaddr *)&addr_, tolen) == -1)
{
throw_spdlog_ex("sendto(2) failed", errno);
close();
Copy link
Owner

@gabime gabime Sep 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close() will never be reached. please remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved close() before the exception is thrown

if (( toslen = sendto(socket_, data, n_bytes, 0, (struct sockaddr *)&sockAddr_, tolen)) == -1)
{
throw_spdlog_ex("sendto(2) failed", errno);
close();
Copy link
Owner

@gabime gabime Sep 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close() will never be reached. please remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved close() before the exception is thrown.

@gabime
Copy link
Owner

gabime commented Sep 4, 2021

Also, since the socket creation and initialization might be an expensive operation, I think it is better done once in the constructor and never called again (i.e. no need to check is_init() on each log call). This will make this sink a simple RAII class that closes the socket on destruction only.

@CJLove
Copy link
Contributor Author

CJLove commented Sep 4, 2021

Agreed that the socket creation and initialization can/should be moved to the constructor. I'm inclined to leave the is_init() check in place as this would allow the sink to re-initialize the socket if an error had occurred and the previous socket was closed.

That being said it's your call and I can remove the is_init() check if you'd prefer.

@gabime
Copy link
Owner

gabime commented Sep 4, 2021

to re-initialize the socket if an error had occurred and the previous socket was closed.

Problem is that there is good chance it would fail again and again in each log call. Lets keep it simple and remove it. If we find out in the future such a need, we will re add it, but in present time seems unneeded.

@CJLove
Copy link
Contributor Author

CJLove commented Sep 5, 2021

Done. Thanks!

@gabime gabime merged commit 5906ce8 into gabime:v1.x Sep 5, 2021
@gabime
Copy link
Owner

gabime commented Sep 5, 2021

Thanks @CJLove

@gabime
Copy link
Owner

gabime commented Sep 5, 2021

@CJLove fyi I committed some changes (9bb66c0, 9bb66c0, 9bb66c0) to make it RAII and to better handle the WSAStartup()/WSACleanup() pair.

This was referenced Sep 6, 2021
@tt4g tt4g mentioned this pull request May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants