19 #include "uuid/syslog.h"
22 #ifdef ARDUINO_ARCH_ESP8266
23 # include <ESP8266WiFi.h>
29 #if defined __has_include
30 # if __has_include (<lwip/init.h>)
31 # include <lwip/init.h>
35 #ifndef UUID_SYSLOG_HAVE_GETTIMEOFDAY
36 # if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
38 # define UUID_SYSLOG_HAVE_GETTIMEOFDAY 1
42 #ifndef UUID_SYSLOG_HAVE_GETTIMEOFDAY
43 # define UUID_SYSLOG_HAVE_GETTIMEOFDAY 0
46 #ifndef UUID_SYSLOG_HAVE_IPADDRESS_TYPE
47 # if defined(ARDUINO_ARCH_ESP8266)
48 # define UUID_SYSLOG_HAVE_IPADDRESS_TYPE 1
52 #ifndef UUID_SYSLOG_HAVE_IPADDRESS_TYPE
53 # define UUID_SYSLOG_HAVE_IPADDRESS_TYPE 0
56 #ifndef UUID_SYSLOG_ARP_CHECK
57 # if defined(LWIP_VERSION_MAJOR) && defined(LWIP_IPV4) && LWIP_VERSION_MAJOR >= 2 && LWIP_IPV4
58 # define UUID_SYSLOG_ARP_CHECK 1
62 #ifndef UUID_SYSLOG_ARP_CHECK
63 # define UUID_SYSLOG_ARP_CHECK 0
66 #ifndef UUID_SYSLOG_NDP_CHECK
67 # if defined(LWIP_VERSION_MAJOR) && defined(LWIP_IPV6) && LWIP_VERSION_MAJOR >= 2 && LWIP_IPV6
68 # define UUID_SYSLOG_NDP_CHECK 1
72 #ifndef UUID_SYSLOG_NDP_CHECK
73 # define UUID_SYSLOG_NDP_CHECK 0
76 #if UUID_SYSLOG_ARP_CHECK or UUID_SYSLOG_NDP_CHECK
77 # include <lwip/netif.h>
79 #if UUID_SYSLOG_ARP_CHECK
80 # include <lwip/ip4_addr.h>
81 # include <lwip/etharp.h>
83 #if UUID_SYSLOG_NDP_CHECK
84 # include <lwip/ip6_addr.h>
85 # include <lwip/nd6.h>
91 #if UUID_SYSLOG_THREAD_SAFE
96 #include <uuid/common.h>
99 #ifndef UUID_SYSLOG_UDP_BASE_MESSAGE_DELAY
100 # define UUID_SYSLOG_UDP_BASE_MESSAGE_DELAY 100
103 #ifndef UUID_SYSLOG_UDP_IPV4_ARP_MESSAGE_DELAY
104 # define UUID_SYSLOG_UDP_IPV4_ARP_MESSAGE_DELAY 10
107 #ifndef UUID_SYSLOG_UDP_IPV6_NDP_MESSAGE_DELAY
108 # define UUID_SYSLOG_UDP_IPV6_NDP_MESSAGE_DELAY 10
112 # define PSTR_ALIGN 4
115 static const char __pstr__logger_name[] __attribute__((__aligned__(PSTR_ALIGN))) PROGMEM =
"syslog";
133 #if UUID_SYSLOG_THREAD_SAFE
134 std::lock_guard<std::mutex> lock{
mutex_};
136 unsigned long offset = 0;
139 if (it->content_->level > level) {
159 if (level_changed && level < uuid::log::Level::NOTICE) {
163 if (level_changed && level >= uuid::log::Level::NOTICE) {
169 #if UUID_SYSLOG_THREAD_SAFE
170 std::lock_guard<std::mutex> lock{
mutex_};
177 #if UUID_SYSLOG_THREAD_SAFE
178 std::lock_guard<std::mutex> lock{
mutex_};
189 #if UUID_SYSLOG_THREAD_SAFE
190 std::lock_guard<std::mutex> lock{
mutex_};
204 if ((uint32_t)
host_ == (uint32_t)0) {
231 : id_(id), content_(std::move(content)) {
232 if (
time_good_ || WiFi.status() == WL_CONNECTED) {
233 #if UUID_SYSLOG_HAVE_GETTIMEOFDAY
234 if (gettimeofday(&
time_,
nullptr) != 0) {
235 time_.tv_sec = (time_t)-1;
238 time_.tv_sec = time(
nullptr);
242 if (
time_.tv_sec >= 0 &&
time_.tv_sec < 18140 * 86400) {
243 time_.tv_sec = (time_t)-1;
246 if (
time_.tv_sec != (time_t)-1) {
250 time_.tv_sec = (time_t)-1;
264 #if UUID_SYSLOG_THREAD_SAFE
265 std::lock_guard<std::mutex> lock{
mutex_};
272 #if UUID_SYSLOG_THREAD_SAFE
273 std::unique_lock<std::mutex> lock{
mutex_};
278 #if UUID_SYSLOG_THREAD_SAFE
285 #if UUID_SYSLOG_THREAD_SAFE
296 #if UUID_SYSLOG_THREAD_SAFE
302 #if UUID_SYSLOG_THREAD_SAFE
312 #if UUID_SYSLOG_THREAD_SAFE
319 #if UUID_SYSLOG_THREAD_SAFE
338 uuid::log::Level::INFO,
339 uuid::log::Facility::SYSLOG,
340 reinterpret_cast<const __FlashStringHelper *
>(__pstr__logger_name),
348 #if UUID_SYSLOG_HAVE_IPADDRESS_TYPE
349 if (
host_.isV4() && (uint32_t)
host_ == (uint32_t)0) {
353 if ((uint32_t)
host_ == (uint32_t)0) {
358 if (WiFi.status() != WL_CONNECTED) {
363 uint64_t message_delay = UUID_SYSLOG_UDP_BASE_MESSAGE_DELAY;
365 #if UUID_SYSLOG_ARP_CHECK
366 # if UUID_SYSLOG_HAVE_IPADDRESS_TYPE
370 message_delay = UUID_SYSLOG_UDP_IPV4_ARP_MESSAGE_DELAY;
373 #if UUID_SYSLOG_NDP_CHECK && UUID_SYSLOG_HAVE_IPADDRESS_TYPE
375 message_delay = UUID_SYSLOG_UDP_IPV6_NDP_MESSAGE_DELAY;
383 #if UUID_SYSLOG_ARP_CHECK
384 # if UUID_SYSLOG_HAVE_IPADDRESS_TYPE
390 ip4_addr_set_u32(&ipaddr, (uint32_t)
host_);
392 if (!ip4_addr_isloopback(&ipaddr)
393 && !ip4_addr_ismulticast(&ipaddr)
394 && !ip4_addr_isbroadcast(&ipaddr, netif_default)) {
395 struct eth_addr *eth_ret =
nullptr;
396 const ip4_addr_t *ip_ret =
nullptr;
398 if (!ip4_addr_netcmp(&ipaddr, netif_ip4_addr(netif_default), netif_ip4_netmask(netif_default))) {
400 const ip4_addr_t *gw_addr = netif_ip4_gw(netif_default);
402 if (gw_addr !=
nullptr) {
407 if (etharp_find_addr(netif_default, &ipaddr, ð_ret, &ip_ret) == -1) {
408 etharp_query(netif_default, &ipaddr, NULL);
418 #if UUID_SYSLOG_NDP_CHECK && UUID_SYSLOG_HAVE_IPADDRESS_TYPE
423 ip6_addr_assign_zone(&ip6addr, IP6_UNICAST, netif_default);
425 if (!ip6_addr_isloopback(&ip6addr)
426 && !ip6_addr_ismulticast(&ip6addr)) {
428 bool have_address =
false;
429 const u8_t *hwaddr =
nullptr;
431 for (
size_t i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
432 if (ip6_addr_isvalid(netif_ip6_addr_state(netif_default, i))) {
433 if (ip6_addr_islinklocal(&ip6addr)) {
434 if (ip6_addr_islinklocal(netif_ip6_addr(netif_default, i))) {
438 }
else if (ip6_addr_isglobal(&ip6addr)
439 || ip6_addr_isuniquelocal(&ip6addr)
440 || ip6_addr_issitelocal(&ip6addr)) {
441 if (ip6_addr_isglobal(netif_ip6_addr(netif_default, i))
442 || ip6_addr_isuniquelocal(netif_ip6_addr(netif_default, i))
443 || ip6_addr_issitelocal(netif_ip6_addr(netif_default, i))) {
459 }
else if (nd6_get_next_hop_addr_or_queue(netif_default, NULL, &ip6addr, &hwaddr) != ERR_OK) {
476 if (message.
time_.tv_sec != (time_t)-1) {
477 gmtime_r(&message.
time_.tv_sec, &tm);
500 udp_.printf_P(PSTR(
"<%u>1 "), (uint8_t)(message.
content_->facility * 8U)
501 + std::min(7U, (
unsigned int)message.
content_->level));
502 if (tm.tm_year != 0) {
503 udp_.printf_P(PSTR(
"%04u-%02u-%02uT%02u:%02u:%02u.%06luZ"),
504 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
505 tm.tm_hour, tm.tm_min, tm.tm_sec, (
unsigned long)message.
time_.tv_usec);
509 udp_.printf_P(PSTR(
" %s - - - - \xEF\xBB\xBF"),
hostname_.c_str());
511 #pragma GCC diagnostic push
512 #pragma GCC diagnostic ignored "-Wformat"
514 #pragma GCC diagnostic pop
516 bool ok = (
udp_.endPacket() == 1);