diff --git a/lib/uuid-syslog/src/syslog.cpp b/lib/uuid-syslog/src/syslog.cpp index b5913d1a6..8d8b18625 100644 --- a/lib/uuid-syslog/src/syslog.cpp +++ b/lib/uuid-syslog/src/syslog.cpp @@ -18,9 +18,6 @@ #include "uuid/syslog.h" -#include -#include -#include #include "../../../src/emsesp.h" @@ -153,16 +150,38 @@ void SyslogService::maximum_log_messages(size_t count) { } std::pair SyslogService::destination() const { - return std::make_pair(host_, port_); + return std::make_pair(ip_, port_); } void SyslogService::destination(IPAddress host, uint16_t port) { - host_ = host; + ip_ = host; port_ = port; - if ((uint32_t)host_ == (uint32_t)0) { + if ((uint32_t)ip_ == (uint32_t)0) { started_ = false; 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() { + if (!host_.empty() && (uint32_t)ip_ == 0) { + WiFi.hostByName(host_.c_str(), ip_); + } #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; } #else - if ((uint32_t)host_ == (uint32_t)0) { + if ((uint32_t)ip_ == (uint32_t)0) { return false; } #endif @@ -276,14 +298,14 @@ bool SyslogService::can_transmit() { #if UUID_SYSLOG_ARP_CHECK #if UUID_SYSLOG_HAVE_IPADDRESS_TYPE - if (host_.isV4()) + if (ip_.isV4()) #endif { message_delay = 10; } #endif #if UUID_SYSLOG_NDP_CHECK && UUID_SYSLOG_HAVE_IPADDRESS_TYPE - if (host_.isV6()) { + if (ip_.isV6()) { message_delay = 10; } #endif @@ -294,12 +316,12 @@ bool SyslogService::can_transmit() { #if UUID_SYSLOG_ARP_CHECK #if UUID_SYSLOG_HAVE_IPADDRESS_TYPE - if (host_.isV4()) + if (ip_.isV4()) #endif { 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)) { struct eth_addr * eth_ret = nullptr; @@ -326,10 +348,10 @@ bool SyslogService::can_transmit() { #endif #if UUID_SYSLOG_NDP_CHECK && UUID_SYSLOG_HAVE_IPADDRESS_TYPE - if (host_.isV6()) { + if (ip_.isV6()) { 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); 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; } - if (udp_.beginPacket(host_, port_) != 1) { + if (udp_.beginPacket(ip_, port_) != 1) { last_transmit_ = uuid::get_uptime_ms(); return false; } diff --git a/lib/uuid-syslog/src/uuid/syslog.h b/lib/uuid-syslog/src/uuid/syslog.h index 70c94cdd1..7d1d3299e 100644 --- a/lib/uuid-syslog/src/uuid/syslog.h +++ b/lib/uuid-syslog/src/uuid/syslog.h @@ -129,6 +129,7 @@ class SyslogService : public uuid::log::Handler { * @since 2.0.0 */ void destination(IPAddress host, uint16_t port = DEFAULT_PORT); + void destination(const char * host, uint16_t port = DEFAULT_PORT); /** * Get local hostname. @@ -183,6 +184,20 @@ class SyslogService : public uuid::log::Handler { */ virtual void operator<<(std::shared_ptr message); + /** + * added MichaelDvP + * query status variables + */ + size_t queued() { + return log_messages_.size(); + } + bool started() { + return started_; + } + IPAddress ip() { + return ip_; + } + private: /** * 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 */ 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 */ uint64_t last_transmit_ = 0; /*!< Last transmit time. @since 1.0.0 */ std::string hostname_{'-'}; /*!< Local hostname. @since 1.0.0 */