diff --git a/btproxy.yaml b/btproxy.yaml index e5b6460..59308f5 100644 --- a/btproxy.yaml +++ b/btproxy.yaml @@ -13,5 +13,3 @@ packages: btproxy: !include templates/btproxy.yaml # Allow HA to use our proxy. api: !include templates/api.yaml - # With idf framework, we can use syslog. - syslog: !include templates/syslog.yaml diff --git a/components/syslog/syslog.cpp b/components/syslog/syslog.cpp index f777b65..e6d615b 100644 --- a/components/syslog/syslog.cpp +++ b/components/syslog/syslog.cpp @@ -1,21 +1,16 @@ #include "syslog.h" +#ifdef USE_NETWORK #include #include "esphome/core/log.h" #include "esphome/core/application.h" +#include "esphome/components/network/util.h" #ifdef USE_LOGGER #include "esphome/components/logger/logger.h" #endif -#include "esphome/components/network/util.h" -/* -#include "esphome/core/helpers.h" -#include "esphome/core/defines.h" -#include "esphome/core/version.h" -*/ - namespace esphome { namespace syslog { @@ -86,12 +81,21 @@ void Syslog::setup() { } #endif - this->socket_fd_ = ::socket(AF_INET, SOCK_DGRAM, 0); +#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) + this->socket_ = socket::socket(AF_INET, SOCK_DGRAM, PF_INET); + // We don't expect any reply from syslog server, so there's no need to block. + this->socket_->setblocking(false); + this->destination_len_ = socket::set_sockaddr( + reinterpret_cast(&this->destination_), sizeof(this->destination_), + this->server_ip_address_, this->server_port_); - memset(&this->destination_, 0, sizeof(this->destination_)); - this->destination_.sin_family = AF_INET; - this->destination_.sin_port = htons(this->server_port_); - this->destination_.sin_addr.s_addr = inet_addr(this->server_ip_address_.c_str()); + if (this->destination_len_ == 0) { + std::string error_message(strerror(errno)); + ESP_LOGE(TAG, "Cannot use IP address '%s' or port %d for server connection: %s", + this->server_ip_address_.c_str(), this->server_port_, + error_message.c_str()); + } +#endif } void Syslog::loop() { @@ -130,14 +134,26 @@ void Syslog::log(int level, const std::string &tag, const std::string &msg) { << App.get_name() << " - - - " << msg; std::string payload = payload_stream.str(); - ssize_t bytes_sent = ::sendto( - this->socket_fd_, - payload.c_str(), payload.length(), - 0, - reinterpret_cast(&this->destination_), sizeof(this->destination_)); + ssize_t bytes_sent = 0; +#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) + if (this->destination_len_ > 0) { + bytes_sent = this->socket_->sendto( + payload.c_str(), payload.length(), + 0, + reinterpret_cast(&this->destination_), this->destination_len_); + } +#else + if (this->udp_client_.beginPacket(this->server_ip_address_.c_str(), this->server_port_)) { + bytes_sent = this->udp_client_.write(payload.c_str(), payload.length()); + if (!this->udp_client_.endPacket()) { + bytes_sent = 0; + } + } +#endif if (bytes_sent != payload.length()) { - // Can't log here, but loop() will pick it up. + // Can't log here as we could be within logger callback, but our loop() will + // pick it up. this->latest_error_message_ = strerror(errno); ++this->errors_encountered_; } @@ -145,3 +161,4 @@ void Syslog::log(int level, const std::string &tag, const std::string &msg) { } // namespace syslog } // namespace esphome +#endif diff --git a/components/syslog/syslog.h b/components/syslog/syslog.h index 8850f64..f45f001 100644 --- a/components/syslog/syslog.h +++ b/components/syslog/syslog.h @@ -1,20 +1,27 @@ #pragma once +#include "esphome/core/defines.h" +#ifdef USE_NETWORK /* Potential future improvements: - hostname instead of IP address - custom mapping of log leves to syslog levels - set custom facility - - protocol support: Structured Data, BSD, IPv6 + - protocol support: Structured Data, BSD - allow specifying time component to get timestamp - code optimization (e.g. marking methods as HOT or using fewer complex ops) + - fix esp8266 skipping logs at startup due to dump_config flood */ #include "esphome/core/component.h" -#include "esphome/core/defines.h" #include "esphome/core/automation.h" #include "esphome/core/log.h" + +#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) #include "esphome/components/socket/socket.h" +#else +#include "WiFiUdp.h" +#endif namespace esphome { @@ -47,8 +54,14 @@ class Syslog : public Component { bool forward_logger_; bool strip_color_codes_; - int socket_fd_; - struct sockaddr_in destination_; +#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) + std::unique_ptr socket_{}; + struct sockaddr_storage destination_; + socklen_t destination_len_; +#else + // The socket class doesn't implement UDP for ESP8266. + WiFiUDP udp_client_{}; +#endif int errors_encountered_; std::string latest_error_message_; @@ -76,3 +89,4 @@ class Syslog : public Component { } // namespace syslog } // namespace esphome +#endif diff --git a/heating-eg.yaml b/heating-eg.yaml index e8ac8ec..1eac63d 100644 --- a/heating-eg.yaml +++ b/heating-eg.yaml @@ -16,6 +16,7 @@ packages: mqtt: !include templates/mqtt.yaml uptime: !include templates/uptime.yaml restart: !include templates/restart.yaml + syslog: !include templates/syslog.yaml logger: diff --git a/heating-og.yaml b/heating-og.yaml index dffbac3..db1aef0 100644 --- a/heating-og.yaml +++ b/heating-og.yaml @@ -16,6 +16,7 @@ packages: mqtt: !include templates/mqtt.yaml uptime: !include templates/uptime.yaml restart: !include templates/restart.yaml + syslog: !include templates/syslog.yaml logger: diff --git a/templates/esp01m-ir.yaml b/templates/esp01m-ir.yaml index 7e8e25c..c51308e 100644 --- a/templates/esp01m-ir.yaml +++ b/templates/esp01m-ir.yaml @@ -7,6 +7,7 @@ packages: mqtt: !include mqtt.yaml uptime: !include uptime.yaml restart: !include restart.yaml + syslog: !include syslog.yaml logger: diff --git a/templates/esp01s.yaml b/templates/esp01s.yaml index 0972d7b..bfa8de0 100644 --- a/templates/esp01s.yaml +++ b/templates/esp01s.yaml @@ -7,6 +7,7 @@ packages: mqtt: !include mqtt.yaml uptime: !include uptime.yaml restart: !include restart.yaml + syslog: !include syslog.yaml logger: diff --git a/templates/esp32-poe.yaml b/templates/esp32-poe.yaml index cc41428..20f7e79 100644 --- a/templates/esp32-poe.yaml +++ b/templates/esp32-poe.yaml @@ -27,3 +27,4 @@ packages: mqtt: !include mqtt.yaml uptime: !include uptime.yaml restart: !include restart.yaml + syslog: !include syslog.yaml diff --git a/templates/esp32.yaml b/templates/esp32.yaml index dffac4a..e653576 100644 --- a/templates/esp32.yaml +++ b/templates/esp32.yaml @@ -7,6 +7,7 @@ packages: mqtt: !include mqtt.yaml uptime: !include uptime.yaml restart: !include restart.yaml + syslog: !include syslog.yaml logger: diff --git a/templates/esp8266.yaml b/templates/esp8266.yaml index 46f2220..e256ff7 100644 --- a/templates/esp8266.yaml +++ b/templates/esp8266.yaml @@ -7,6 +7,7 @@ packages: mqtt: !include mqtt.yaml uptime: !include uptime.yaml restart: !include restart.yaml + syslog: !include syslog.yaml logger: