Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12a5531

Browse files
committed
set default value to client; change send buf to local vars;
1 parent 39af011 commit 12a5531

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ all:
77
clean:
88
make -C src/ clean;
99
make -C demo/ clean;
10+
@rm -f ./system_monitor ./test_client
1011

src/statsd_client.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,21 @@ inline bool should_send(float sample_rate)
3232

3333

3434
struct _StatsdClientData {
35-
int sock;
36-
struct sockaddr_in server;
35+
int sock;
36+
struct sockaddr_in server;
3737

38-
std::string ns;
39-
std::string host;
40-
short port;
41-
bool init;
38+
string ns;
39+
string host;
40+
short port;
41+
bool init;
4242

43-
char buf[255];
44-
char errmsg[1024];
43+
char errmsg[1024];
4544
};
4645

4746
StatsdClient::StatsdClient(const string& host, int port, const string& ns)
4847
{
4948
d = new _StatsdClientData;
50-
d->ns = ns;
51-
d->host = host;
52-
d->port = port;
53-
d->init = false;
54-
d->sock = -1;
49+
config(host, port, ns);
5550
srandom(time(NULL));
5651
}
5752

@@ -64,6 +59,18 @@ StatsdClient::~StatsdClient()
6459
}
6560
}
6661

62+
void StatsdClient::config(const string& host, int port, const string& ns)
63+
{
64+
d->ns = ns;
65+
d->host = host;
66+
d->port = port;
67+
d->init = false;
68+
if ( d->sock >= 0 ) {
69+
close(d->sock);
70+
}
71+
d->sock = -1;
72+
}
73+
6774
int StatsdClient::init()
6875
{
6976
if ( d->init ) return 0;
@@ -146,18 +153,19 @@ int StatsdClient::send(string key, size_t value, const string &type, float sampl
146153

147154
cleanup(key);
148155

156+
char buf[256];
149157
if ( fequal( sample_rate, 1.0 ) )
150158
{
151-
snprintf(d->buf, sizeof(d->buf), "%s%s:%zd|%s",
159+
snprintf(buf, sizeof(buf), "%s%s:%zd|%s",
152160
d->ns.c_str(), key.c_str(), value, type.c_str());
153161
}
154162
else
155163
{
156-
snprintf(d->buf, sizeof(d->buf), "%s%s:%zd|%s|@%.2f",
164+
snprintf(buf, sizeof(buf), "%s%s:%zd|%s|@%.2f",
157165
d->ns.c_str(), key.c_str(), value, type.c_str(), sample_rate);
158166
}
159167

160-
return send(d->buf);
168+
return send(buf);
161169
}
162170

163171
int StatsdClient::send(const string &message)

src/statsd_client.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ struct _StatsdClientData;
1313

1414
class StatsdClient {
1515
public:
16-
StatsdClient(const std::string& host, int port, const std::string& ns = "");
16+
StatsdClient(const std::string& host="127.0.0.1", int port=8125, const std::string& ns = "");
1717
~StatsdClient();
1818

1919
public:
20+
// you can config at anytime; client will use new address (useful for Singleton)
21+
void config(const std::string& host, int port, const std::string& ns = "");
2022
const char* errmsg();
23+
24+
public:
2125
int inc(const std::string& key, float sample_rate = 1.0);
2226
int dec(const std::string& key, float sample_rate = 1.0);
2327
int count(const std::string& key, size_t count, float sample_rate = 1.0);

0 commit comments

Comments
 (0)