-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogSyslogModule.cpp
More file actions
31 lines (26 loc) · 1.03 KB
/
LogSyslogModule.cpp
File metadata and controls
31 lines (26 loc) · 1.03 KB
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
#include "LogSyslogModule.h"
LogSyslogModule::LogSyslogModule(UDP& _udp, String _server, String _hostname, String _appName, uint16_t _port) : udp(_udp), server(_server), hostname(_hostname), appName(_appName), port(_port) {
setMinimumLogLevel(10);
}
bool LogSyslogModule::isConnected() {
// UDP is connectionless; send() reports whether the datagram could be
// dispatched, so always attempt and let it decide.
return true;
}
bool LogSyslogModule::send(const String& message) {
// RFC 5424 header:
// <PRI>VERSION TIMESTAMP HOSTNAME APP-NAME PROCID MSGID MSG
// NILVALUE "-" is used for fields we cannot provide (no RTC for timestamp).
int pri = facility * 8 + severity;
String packet = String("<") + pri + ">1 - ";
packet += (hostname.length() ? hostname : String("-"));
packet += " ";
packet += (appName.length() ? appName : String("-"));
packet += " - - ";
packet += message;
if (!udp.beginPacket(server.c_str(), port)) {
return false;
}
udp.write((const uint8_t*)packet.c_str(), packet.length());
return udp.endPacket() == 1;
}