From 2d1689b79a37c0d776d551f6b9174e3f9ef72be0 Mon Sep 17 00:00:00 2001 From: Proddy Date: Sat, 11 Feb 2023 19:05:43 +0100 Subject: [PATCH] make syslog work again --- lib/uuid-syslog/src/syslog.cpp | 69 ++++++++++++++++++++----------- lib/uuid-syslog/src/uuid/syslog.h | 7 +++- src/system.cpp | 4 +- src/web/WebStatusService.cpp | 8 ++-- 4 files changed, 57 insertions(+), 31 deletions(-) diff --git a/lib/uuid-syslog/src/syslog.cpp b/lib/uuid-syslog/src/syslog.cpp index 70fbe6318..0c07da065 100644 --- a/lib/uuid-syslog/src/syslog.cpp +++ b/lib/uuid-syslog/src/syslog.cpp @@ -132,13 +132,17 @@ void SyslogService::log_level(uuid::log::Level level) { remove_queued_messages(level); } - static bool level_set = false; - bool level_changed = !level_set || (level != log_level()); - level_set = true; + // commented out for EMS-ESP + /* + static bool level_set = false; + level_set = true; + bool level_changed = !level_set || (level != log_level()); + if (level_changed) { - logger_.info("Log level set to %S", uuid::log::format_level_uppercase(level)); + logger_.info("Log level set to %S", uuid::log::format_level_uppercase(level)); } + */ uuid::log::Logger::register_handler(this, level); } @@ -170,17 +174,38 @@ size_t SyslogService::current_log_messages() const { } 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) { - ip_ = host; +void SyslogService::destination(IPAddress ip, uint16_t port) { + ip_ = ip; port_ = port; if ((uint32_t)ip_ == (uint32_t)0) { started_ = false; remove_queued_messages(log_level()); - // host_.clear(); + 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; } } @@ -207,7 +232,7 @@ void SyslogService::mark_interval(unsigned long interval) { SyslogService::QueuedLogMessage::QueuedLogMessage(unsigned long id, std::shared_ptr && content) : id_(id) , content_(std::move(content)) { - // Added by proddy for EMS-ESP + // Added for EMS-ESP // check for Ethernet too. This assumes the network has already started. if (time_good_ || emsesp::EMSESP::system_.network_connected()) { #if UUID_SYSLOG_HAVE_GETTIMEOFDAY @@ -324,25 +349,23 @@ void SyslogService::loop() { } bool SyslogService::can_transmit() { - // TODO removed this, it was in name)); + udp_.printf(" %s %s - - - ", hostname_.c_str(), message.content_->name); char id_c_str[15]; snprintf(id_c_str, sizeof(id_c_str), " %lu: ", message.id_); diff --git a/lib/uuid-syslog/src/uuid/syslog.h b/lib/uuid-syslog/src/uuid/syslog.h index 35a9e3f7b..a7f725238 100644 --- a/lib/uuid-syslog/src/uuid/syslog.h +++ b/lib/uuid-syslog/src/uuid/syslog.h @@ -161,6 +161,9 @@ class SyslogService : public uuid::log::Handler { */ void destination(IPAddress host, uint16_t port = DEFAULT_PORT); + // added for EMS-ESP + void destination(const char * host, uint16_t port = DEFAULT_PORT); + /** * Get local hostname. * @@ -310,7 +313,6 @@ class SyslogService : public uuid::log::Handler { bool started_ = false; /*!< Flag to indicate that messages have started being transmitted. @since 1.0.0 */ bool level_set_ = false; /*!< Flag to indicate that the log level has been set at least once. @since 2.2.0 */ WiFiUDP udp_; /*!< UDP client. @since 1.0.0 */ - IPAddress host_; /*!< Host 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 */ std::string hostname_{'-'}; /*!< Local hostname. @since 1.0.0 */ @@ -324,7 +326,8 @@ class SyslogService : public uuid::log::Handler { uint64_t last_message_ = 0; /*!< Last message/mark time. @since 2.0.0 */ // added by MichaelDvP for EMS-ESP - IPAddress ip_; /*!< Host-IP to send messages to. @since 1.0.0 */ + IPAddress ip_; /*!< Host to send messages to. @since 1.0.0 */ + std::string host_; /*!< Host-IP to send messages to */ unsigned long log_message_fails_ = 0; }; diff --git a/src/system.cpp b/src/system.cpp index 7dc8a1dbb..ef1899c6c 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -1,6 +1,6 @@ /* * EMS-ESP - https://github.com/emsesp/EMS-ESP - * Copyright 2020 Paul Derbyshire + * Copyright 2020-2023 Paul Derbyshire * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -409,8 +409,8 @@ void System::start() { led_init(false); // init LED button_init(false); // the special button network_init(false); // network - EMSESP::uart_init(); // start UART + syslog_init(); // start syslog } // button single click diff --git a/src/web/WebStatusService.cpp b/src/web/WebStatusService.cpp index c4b20f9df..ab3fc935d 100644 --- a/src/web/WebStatusService.cpp +++ b/src/web/WebStatusService.cpp @@ -1,6 +1,6 @@ /* * EMS-ESP - https://github.com/emsesp/EMS-ESP - * Copyright 2020 Paul Derbyshire + * Copyright 2020-2023 Paul Derbyshire * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -42,7 +42,7 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { case ARDUINO_EVENT_WIFI_STA_GOT_IP: EMSESP::logger().info("WiFi connected with IP=%s, hostname=%s", WiFi.localIP().toString().c_str(), WiFi.getHostname()); - EMSESP::system_.syslog_init(); + // EMSESP::system_.syslog_init(); mDNS_start(); EMSESP::system_.send_info_mqtt("connected"); break; @@ -65,7 +65,7 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { if (!EMSESP::system_.ethernet_connected()) { EMSESP::logger().info("Ethernet connected with IP=%s, speed %d Mbps", ETH.localIP().toString().c_str(), ETH.linkSpeed()); // EMSESP::system_.send_heartbeat(); - EMSESP::system_.syslog_init(); + // EMSESP::system_.syslog_init(); EMSESP::system_.ethernet_connected(true); mDNS_start(); EMSESP::system_.send_info_mqtt("connected"); @@ -105,7 +105,7 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { } else { EMSESP::logger().info("WiFi connected with IPv6=%s, hostname=%s", WiFi.localIPv6().toString().c_str(), WiFi.getHostname()); } - EMSESP::system_.syslog_init(); + // EMSESP::system_.syslog_init(); mDNS_start(); EMSESP::system_.send_info_mqtt("connected"); break;