RemoteLogRK
|
Log server module for writing to a syslog server over UDP. More...
#include <RemoteLogRK.h>
Public Member Functions | |
RemoteLogSyslogUDP (size_t bufLen=256) | |
Constructor. More... | |
RemoteLogSyslogUDP (const char *hostname, uint16_t port, size_t bufLen=256) | |
Constructor. More... | |
virtual | ~RemoteLogSyslogUDP () |
Destructor. More... | |
RemoteLogSyslogUDP & | withHostnameAndPort (const char *hostname, uint16_t port) |
Sets the hostname and port of the UDP syslog server. More... | |
RemoteLogSyslogUDP & | withDeviceNameCallback (std::function< bool(String &)> deviceNameCallback) |
Sets the callback to get the device name, used in the syslog packet. More... | |
RemoteLogSyslogUDP & | withMinSendPeriodMs (unsigned long valueMs) |
Sets the minimum period between UDP sends (default: 100 milliseconds) More... | |
void | loop (size_t &readIndex) |
Perform loop time operations such as reading and writing sockets. | |
Public Member Functions inherited from RemoteLogServer | |
RemoteLogServer () | |
Base class constructor. Doesn't do anything. | |
virtual | ~RemoteLogServer () |
This class is not typically deleted, because you can't unregister one. | |
virtual void | setup () |
Perform setup operations. More... | |
virtual void | reset () |
Reset is about to occur, do things like disconnect sockets. More... | |
Protected Attributes | |
unsigned long | minSendPeriodMs = 100 |
UDP send rate limit (milliseconds) More... | |
unsigned long | lastSendMs = 0 |
The millis value of the last send used for rate limiting. | |
String | hostname |
The hostname to send to. More... | |
IPAddress | remoteAddr |
The IPv4 address of the remote host. | |
uint16_t | port = 0 |
The UDP port to send to. | |
std::function< bool(String &)> | deviceNameCallback = 0 |
The function to call to find the name of this device set using withDeviceNameCallback. | |
UDP | udp |
The Wiring UDP object used to send UDP data. | |
bool | networkReady = false |
true if Celluar.ready() or WiFi.ready() is true | |
uint8_t * | buf |
The packet buffer. Allocated in the constructor. | |
size_t | bufLen |
The size of the packet buffer. Set in the constructor. | |
Additional Inherited Members | |
Protected Member Functions inherited from RemoteLogServer | |
RemoteLogServer (const RemoteLogServer &)=delete | |
This class is not copyable. | |
RemoteLogServer & | operator= (const RemoteLogServer &)=delete |
This class is not copyable. | |
Log server module for writing to a syslog server over UDP.
The syslog server can be on your local network, or cloud-hosted, like Solarwinds Papertrail.
RemoteLogSyslogUDP::RemoteLogSyslogUDP | ( | size_t | bufLen = 256 | ) |
Constructor.
bufLen | The maximum UDP packet size. Default is 256. It should not be smaller than this, because the first 128 bytes of is used as temporary storage for formatting the syslog data before the actual event date is shifted in the buffer. |
If you use this constructor you must set the hostname and port using
RemoteLogSyslogUDP::RemoteLogSyslogUDP | ( | const char * | hostname, |
uint16_t | port, | ||
size_t | bufLen = 256 |
||
) |
Constructor.
hostname | The UDP hostname to send to. |
port | The UDP port to send to. |
bufLen | The maximum UDP packet size. Default is 256. It should not be smaller than this, because the first 128 bytes of is used as temporary storage for formatting the syslog data before the actual event date is shifted in the buffer. |
|
virtual |
Destructor.
This object is not typically deleted as you can't unregister a server, so deleting it would cause a dangling pointer.
|
inline |
Sets the callback to get the device name, used in the syslog packet.
deviceNameCallback | The callback function or C++11 lambda. |
The callback function or C++ lambda should have the prototype:
bool callback(String &deviceName);
The deviceName should be filled in, if known, and return true. If the device name is not yet known, then return false. This will prevent syslog messages from going out, however, so you may want to return some default value and return true instead.
RemoteLogSyslogUDP & RemoteLogSyslogUDP::withHostnameAndPort | ( | const char * | hostname, |
uint16_t | port | ||
) |
Sets the hostname and port of the UDP syslog server.
hostname | The UDP hostname to send to. |
port | The UDP port to send to. |
|
inline |
Sets the minimum period between UDP sends (default: 100 milliseconds)
It's possible to overload the UDP stack causing packets to be dropped. Checking the return value from UDP.sendPacket would help, however there is no good way to put the data back into the buffer after removing it.
Adding rate limiting can also help slow down transmission if runaway recursion occurs. The value could be make even higher (1000 ms) to help protect against this on cellular devices in particular.
valueMs | the value in milliseconds to set the sendPeriodMs to. |
|
protected |
The hostname to send to.
This is resolved by DNS once at startup, then the cached address is used. If you specify a dotted decimal IPv4 address in hostname (10.1.2.3, for example), it's parsed directly without network access.
|
protected |
UDP send rate limit (milliseconds)
This is a safety net to avoid sending data too quickly. Since UDP data is not acknowledged, there's no way to know if it's been discarded. This can be set to 0 for no rate limiting.