syslog add hostname, rename host_ to ip_, query status

This commit is contained in:
MichaelDvP
2021-07-26 11:06:23 +02:00
parent f97cdcb4d6
commit 1df427366f
2 changed files with 54 additions and 16 deletions

View File

@@ -18,9 +18,6 @@
#include "uuid/syslog.h" #include "uuid/syslog.h"
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include "../../../src/emsesp.h" #include "../../../src/emsesp.h"
@@ -153,16 +150,38 @@ void SyslogService::maximum_log_messages(size_t count) {
} }
std::pair<IPAddress, uint16_t> SyslogService::destination() const { std::pair<IPAddress, uint16_t> SyslogService::destination() const {
return std::make_pair(host_, port_); return std::make_pair(ip_, port_);
} }
void SyslogService::destination(IPAddress host, uint16_t port) { void SyslogService::destination(IPAddress host, uint16_t port) {
host_ = host; ip_ = host;
port_ = port; port_ = port;
if ((uint32_t)host_ == (uint32_t)0) { if ((uint32_t)ip_ == (uint32_t)0) {
started_ = false; started_ = false;
remove_queued_messages(log_level()); remove_queued_messages(log_level());
host_.clear();
}
}
void SyslogService::destination(const char * host, uint16_t port) {
if (host == nullptr || host[0] == '\0') {
started_ = false;
remove_queued_messages(log_level());
ip_ = (IPAddress)(uint32_t)0;
host_.clear();
return;
}
host_ = host;
port_ = port;
if (ip_.fromString(host)) {
host_.clear();
if ((uint32_t)ip_ == (uint32_t)0) {
started_ = false;
remove_queued_messages(log_level());
}
} else {
ip_ = (IPAddress)(uint32_t)0;
} }
} }
@@ -257,12 +276,15 @@ void SyslogService::loop() {
} }
bool SyslogService::can_transmit() { bool SyslogService::can_transmit() {
if (!host_.empty() && (uint32_t)ip_ == 0) {
WiFi.hostByName(host_.c_str(), ip_);
}
#if UUID_SYSLOG_HAVE_IPADDRESS_TYPE #if UUID_SYSLOG_HAVE_IPADDRESS_TYPE
if (host_.isV4() && (uint32_t)host_ == (uint32_t)0) { if (ip_.isV4() && (uint32_t)ip_ == (uint32_t)0) {
return false; return false;
} }
#else #else
if ((uint32_t)host_ == (uint32_t)0) { if ((uint32_t)ip_ == (uint32_t)0) {
return false; return false;
} }
#endif #endif
@@ -276,14 +298,14 @@ bool SyslogService::can_transmit() {
#if UUID_SYSLOG_ARP_CHECK #if UUID_SYSLOG_ARP_CHECK
#if UUID_SYSLOG_HAVE_IPADDRESS_TYPE #if UUID_SYSLOG_HAVE_IPADDRESS_TYPE
if (host_.isV4()) if (ip_.isV4())
#endif #endif
{ {
message_delay = 10; message_delay = 10;
} }
#endif #endif
#if UUID_SYSLOG_NDP_CHECK && UUID_SYSLOG_HAVE_IPADDRESS_TYPE #if UUID_SYSLOG_NDP_CHECK && UUID_SYSLOG_HAVE_IPADDRESS_TYPE
if (host_.isV6()) { if (ip_.isV6()) {
message_delay = 10; message_delay = 10;
} }
#endif #endif
@@ -294,12 +316,12 @@ bool SyslogService::can_transmit() {
#if UUID_SYSLOG_ARP_CHECK #if UUID_SYSLOG_ARP_CHECK
#if UUID_SYSLOG_HAVE_IPADDRESS_TYPE #if UUID_SYSLOG_HAVE_IPADDRESS_TYPE
if (host_.isV4()) if (ip_.isV4())
#endif #endif
{ {
ip4_addr_t ipaddr; ip4_addr_t ipaddr;
ip4_addr_set_u32(&ipaddr, (uint32_t)host_); ip4_addr_set_u32(&ipaddr, (uint32_t)ip_);
if (!ip4_addr_isloopback(&ipaddr) && !ip4_addr_ismulticast(&ipaddr) && !ip4_addr_isbroadcast(&ipaddr, netif_default)) { if (!ip4_addr_isloopback(&ipaddr) && !ip4_addr_ismulticast(&ipaddr) && !ip4_addr_isbroadcast(&ipaddr, netif_default)) {
struct eth_addr * eth_ret = nullptr; struct eth_addr * eth_ret = nullptr;
@@ -326,10 +348,10 @@ bool SyslogService::can_transmit() {
#endif #endif
#if UUID_SYSLOG_NDP_CHECK && UUID_SYSLOG_HAVE_IPADDRESS_TYPE #if UUID_SYSLOG_NDP_CHECK && UUID_SYSLOG_HAVE_IPADDRESS_TYPE
if (host_.isV6()) { if (ip_.isV6()) {
ip6_addr_t ip6addr; ip6_addr_t ip6addr;
IP6_ADDR(&ip6addr, host_.raw6()[0], host_.raw6()[1], host_.raw6()[2], host_.raw6()[3]); IP6_ADDR(&ip6addr, ip_.raw6()[0], ip_.raw6()[1], ip_.raw6()[2], ip_.raw6()[3]);
ip6_addr_assign_zone(&ip6addr, IP6_UNICAST, netif_default); ip6_addr_assign_zone(&ip6addr, IP6_UNICAST, netif_default);
if (!ip6_addr_isloopback(&ip6addr) && !ip6_addr_ismulticast(&ip6addr)) { if (!ip6_addr_isloopback(&ip6addr) && !ip6_addr_ismulticast(&ip6addr)) {
@@ -400,7 +422,7 @@ bool SyslogService::transmit(const QueuedLogMessage & message) {
tzm = diff < 0 ? (0 - diff) % 60 : diff % 60; tzm = diff < 0 ? (0 - diff) % 60 : diff % 60;
} }
if (udp_.beginPacket(host_, port_) != 1) { if (udp_.beginPacket(ip_, port_) != 1) {
last_transmit_ = uuid::get_uptime_ms(); last_transmit_ = uuid::get_uptime_ms();
return false; return false;
} }

View File

@@ -129,6 +129,7 @@ class SyslogService : public uuid::log::Handler {
* @since 2.0.0 * @since 2.0.0
*/ */
void destination(IPAddress host, uint16_t port = DEFAULT_PORT); void destination(IPAddress host, uint16_t port = DEFAULT_PORT);
void destination(const char * host, uint16_t port = DEFAULT_PORT);
/** /**
* Get local hostname. * Get local hostname.
@@ -183,6 +184,20 @@ class SyslogService : public uuid::log::Handler {
*/ */
virtual void operator<<(std::shared_ptr<uuid::log::Message> message); virtual void operator<<(std::shared_ptr<uuid::log::Message> message);
/**
* added MichaelDvP
* query status variables
*/
size_t queued() {
return log_messages_.size();
}
bool started() {
return started_;
}
IPAddress ip() {
return ip_;
}
private: private:
/** /**
* Log message that has been queued. * Log message that has been queued.
@@ -244,7 +259,8 @@ class SyslogService : public uuid::log::Handler {
bool started_ = false; /*!< Flag to indicate that messages have started being transmitted. @since 1.0.0 */ bool started_ = false; /*!< Flag to indicate that messages have started being transmitted. @since 1.0.0 */
WiFiUDP udp_; /*!< UDP client. @since 1.0.0 */ WiFiUDP udp_; /*!< UDP client. @since 1.0.0 */
IPAddress host_; /*!< Host to send messages to. @since 1.0.0 */ IPAddress ip_; /*!< Host-IP to send messages to. @since 1.0.0 */
std::string host_; /*!< Host to send messages to. */
uint16_t port_ = DEFAULT_PORT; /*!< Port to send messages to. @since 1.0.0 */ uint16_t port_ = DEFAULT_PORT; /*!< Port to send messages to. @since 1.0.0 */
uint64_t last_transmit_ = 0; /*!< Last transmit time. @since 1.0.0 */ uint64_t last_transmit_ = 0; /*!< Last transmit time. @since 1.0.0 */
std::string hostname_{'-'}; /*!< Local hostname. @since 1.0.0 */ std::string hostname_{'-'}; /*!< Local hostname. @since 1.0.0 */