Skip to content

Commit 87f9076

Browse files
committed
First version
1 parent 711159e commit 87f9076

File tree

8 files changed

+167
-0
lines changed

8 files changed

+167
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

include/Message.h

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef GELF_MESSAGE_H
2+
#define GELF_MESSAGE_H
3+
4+
// TODO add consts where needed!
5+
6+
#include "ArduinoJson.h"
7+
// TODO: different amounts of memory depending on the platform!
8+
9+
#ifdef ESP8266
10+
#include <ESP8266WiFi.h>
11+
#endif
12+
13+
#define GRAYLOG_DEFAULT_HOST "graylog-default-host"
14+
15+
class Message
16+
{
17+
public:
18+
explicit Message(const char *short_message) {
19+
20+
_json["version"] = "1.1";
21+
#ifdef ESP8266
22+
_json["host"] = WiFi.hostname();
23+
#else
24+
_json["host"] = GRAYLOG_DEFAULT_HOST;
25+
#endif
26+
_json["short_message"] = short_message;
27+
}
28+
template<typename T>
29+
void set(const char *field, T value) {
30+
if (strncmp("id", field, 2) == 0 || strncmp("_id", field, 3) == 0) {
31+
return;
32+
}
33+
34+
if (field[0] != '_') {
35+
// TODO: prepend underscore!
36+
}
37+
38+
// TODO: Check regex - ^[\w\.\-]*$
39+
40+
_json[field] = value;
41+
}
42+
43+
unsigned int getEncodedLength() {
44+
return measureJson(_json);
45+
}
46+
void encode(char *buffer, size_t buffer_size) {
47+
serializeJson(_json, buffer, buffer_size);
48+
}
49+
50+
private:
51+
// We could use the heap but whatever
52+
StaticJsonDocument<512> _json;
53+
};
54+
55+
#endif

include/Publisher.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef GELF_PUBLISHER_H
2+
#define GELF_PUBLISHER_H
3+
4+
#include "Transport/Transport.h"
5+
#include "Message.h"
6+
7+
class Publisher
8+
{
9+
public:
10+
explicit Publisher(Transport *transport);
11+
void publish(Message *message);
12+
private:
13+
Transport *_transport = nullptr;
14+
};
15+
16+
#endif

include/Transport/Transport.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef GELF_TRANSPORT_H
2+
#define GELF_TRANSPORT_H
3+
4+
#include "../Message.h"
5+
6+
class Transport
7+
{
8+
public:
9+
virtual void send(Message *message) = 0;
10+
};
11+
12+
#endif

include/Transport/UDPTransport.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef GELF_UDPTRANSPORT_H
2+
#define GELF_UDPTRANSPORT_H
3+
4+
#include "Transport/Transport.h"
5+
#include "../Message.h"
6+
7+
#include <ESP8266WiFi.h>
8+
#include <WiFiUdp.h>
9+
10+
#define GRAYLOG_UDP_DEFAULT_PORT 12201
11+
12+
class UDPTransport : public Transport
13+
{
14+
public:
15+
explicit UDPTransport(char *host, int port = GRAYLOG_UDP_DEFAULT_PORT);
16+
void send(Message *message) override;
17+
private:
18+
char *_host = nullptr;
19+
int _port = 0;
20+
21+
WiFiUDP _udp = WiFiUDP();
22+
};
23+
24+
#endif

library.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "gelf-embedded",
3+
"version": "1.0.0",
4+
"description": "An easy to use GELF (graylog) library for Arduino, ESP8266 and other embedded platforms",
5+
"keywords": "esp8266, arduino, gelf, graylog, log, debug",
6+
"repository":
7+
{
8+
"type": "git",
9+
"url": "https://github.com/fermino/gelf-embedded.git"
10+
},
11+
"authors":
12+
[
13+
{
14+
"name": "Fermín Olaiz",
15+
"email": "[email protected]",
16+
"url": "https://github.com/fermino"
17+
}
18+
],
19+
"license": "MIT",
20+
"homepage": "https://github.com/fermino/gelf-embedded/",
21+
"dependencies": {
22+
"bblanchon/ArduinoJson":"^6.18.5"
23+
},
24+
"frameworks": "*",
25+
"platforms": "*"
26+
}

src/Publisher.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "../include/Publisher.h"
2+
3+
Publisher::Publisher(Transport *transport) {
4+
_transport = transport;
5+
}
6+
7+
void Publisher::publish(Message *message) {
8+
_transport->send(message);
9+
}

src/Transport/UDPTransport.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "../../include/Transport/UDPTransport.h"
2+
3+
UDPTransport::UDPTransport(char *host, int port)
4+
{
5+
_host = host;
6+
_port = port;
7+
}
8+
9+
void UDPTransport::send(Message *message)
10+
{
11+
_udp.beginPacket(_host, _port);
12+
13+
unsigned long length = message->getEncodedLength();
14+
char buffer[length + 1];
15+
message->encode(buffer, length); // Debería ser length + 1??
16+
buffer[length] = '\0'; // This might be unnecessary
17+
18+
_udp.write(buffer);
19+
_udp.endPacket();
20+
21+
// https://stackoverflow.com/questions/1098897/what-is-the-largest-safe-udp-packet-size-on-the-internet
22+
// PS: We don't want to deal with chunking yet
23+
//udp.begin(_port);
24+
}

0 commit comments

Comments
 (0)