Skip to content

Commit f5a0445

Browse files
authored
Use std random and modernize some code (#2)
* Use std::srand and std::rand instead of direct call to srandom and random * Modernize the way errors are handled
1 parent 6f61fe3 commit f5a0445

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

src/StatsdClient.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef STATSD_CLIENT_HPP
22
#define STATSD_CLIENT_HPP
33

4+
#include <cstdlib>
45
#include <experimental/optional>
56
#include <string>
67
#include "UDPSender.hpp"
@@ -33,9 +34,6 @@ class StatsdClient
3334
const std::string& prefix,
3435
const std::experimental::optional<uint64_t> batchsize = std::experimental::nullopt) noexcept;
3536

36-
//! Default destructor
37-
~StatsdClient() = default;
38-
3937
//!@}
4038

4139
//!@name Methods
@@ -96,7 +94,7 @@ StatsdClient(
9694
, m_sender(host, port, batchsize)
9795
{
9896
// Initialize the randorm generator to be used for sampling
99-
srandom(time(NULL));
97+
std::srand(time(nullptr));
10098
}
10199

102100
void
@@ -162,7 +160,7 @@ send(const std::string& key, const int value, const std::string& type, const flo
162160
// Test if one should send or not, according to the frequency rate
163161
if (!isFrequencyOne(frequency))
164162
{
165-
if (frequency < (float)random() / RAND_MAX)
163+
if (frequency < static_cast<float>(std::rand()) / RAND_MAX)
166164
{
167165
return;
168166
}

src/UDPSender.hpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ bool
232232
UDPSender::
233233
initialize() noexcept
234234
{
235+
using namespace std::string_literals;
236+
235237
if (m_isInitialized)
236238
{
237239
return true;
@@ -241,9 +243,7 @@ initialize() noexcept
241243
m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
242244
if (m_socket == -1)
243245
{
244-
char buffer[256];
245-
snprintf(buffer, sizeof(buffer), "Could not create socket, err=%m");
246-
m_errorMessage = std::string(buffer);
246+
m_errorMessage = "Could not create socket, err="s + std::strerror(errno);
247247
return false;
248248
}
249249

@@ -263,17 +263,13 @@ initialize() noexcept
263263

264264
// Get the address info using the hints
265265
struct addrinfo* results = nullptr;
266-
const int ret = getaddrinfo(m_host.c_str(), nullptr, &hints, &results);
266+
const int ret{ getaddrinfo(m_host.c_str(), nullptr, &hints, &results) };
267267
if (ret != 0)
268268
{
269269
// An error code has been returned by getaddrinfo
270270
close(m_socket);
271271
m_socket = -1;
272-
273-
char buffer[256];
274-
snprintf(buffer, sizeof(buffer), "getaddrinfo failed: error=%d, msg=%s", ret, gai_strerror(ret));
275-
m_errorMessage = std::string(buffer);
276-
272+
m_errorMessage = "getaddrinfo failed: error="s + std::to_string(ret) + ", msg=" + gai_strerror(ret);
277273
return false;
278274
}
279275

@@ -286,7 +282,6 @@ initialize() noexcept
286282
}
287283

288284
m_isInitialized = true;
289-
290285
return true;
291286
}
292287

@@ -301,12 +296,11 @@ sendToDaemon(const std::string& message) noexcept
301296
}
302297

303298
// Try sending the message
304-
const int ret = sendto(m_socket, message.data(), message.size(), 0, (struct sockaddr *)&m_server, sizeof(m_server));
299+
const long int ret{ sendto(m_socket, message.data(), message.size(), 0, (struct sockaddr *)&m_server, sizeof(m_server)) };
305300
if (ret == -1)
306301
{
307-
char buffer[256];
308-
snprintf(buffer, sizeof(buffer), "sendto server failed: host=%s:%d, err=%m", m_host.c_str(), m_port);
309-
m_errorMessage = std::string(buffer);
302+
using namespace std::string_literals;
303+
m_errorMessage = "sendto server failed: host="s + m_host + ":" + std::to_string(m_port) + ", err=" + std::strerror(errno);
310304
}
311305
}
312306

0 commit comments

Comments
 (0)