forked from zackkitzmiller/statsd-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsd.cpp
43 lines (36 loc) · 939 Bytes
/
statsd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "Arduino.h"
#include "Statsd.h"
#include "Ethernet.h"
#include "EthernetUDP.h"
#include "IPAddress.h"
#include "string.h"
EthernetUDP statsd;
StatsD::StatsD(IPAddress ip, int port, int localPort)
{
_ip = ip;
_port = port;
statsd.begin(localPort);
}
void StatsD::increment(const char *metric) {
StatsD::_send(metric, ":1|c");
}
void StatsD::decrement(const char *metric) {
StatsD::_send(metric, ":-1|c");
}
void StatsD::timing(const char *metric, int ms) {
char timing[sizeof(&metric) + 20];
strcpy(timing, metric);
strcat(timing, ":");
strcat(timing, (char*)ms);
StatsD::_send(timing, "|ms");
}
void StatsD::_send(const char *metric, const char *cmd) {
int buffer_len = sizeof(&metric) + 4;
char buffer[buffer_len];
strcpy(buffer, metric);
strcat(buffer, cmd);
const uint8_t* msg = (uint8_t*)buffer;
statsd.beginPacket(_ip, _port);
statsd.write(msg, sizeof(&msg));
statsd.endPacket();
}