Expand Syslog to esp8266

This commit is contained in:
2025-01-25 13:59:51 +01:00
parent d060aa8eb4
commit 8a547e3e63
10 changed files with 60 additions and 24 deletions
-2
View File
@@ -13,5 +13,3 @@ packages:
btproxy: !include templates/btproxy.yaml btproxy: !include templates/btproxy.yaml
# Allow HA to use our proxy. # Allow HA to use our proxy.
api: !include templates/api.yaml api: !include templates/api.yaml
# With idf framework, we can use syslog.
syslog: !include templates/syslog.yaml
+35 -18
View File
@@ -1,21 +1,16 @@
#include "syslog.h" #include "syslog.h"
#ifdef USE_NETWORK
#include <sstream> #include <sstream>
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/application.h" #include "esphome/core/application.h"
#include "esphome/components/network/util.h"
#ifdef USE_LOGGER #ifdef USE_LOGGER
#include "esphome/components/logger/logger.h" #include "esphome/components/logger/logger.h"
#endif #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 esphome {
namespace syslog { namespace syslog {
@@ -86,12 +81,21 @@ void Syslog::setup() {
} }
#endif #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<sockaddr *>(&this->destination_), sizeof(this->destination_),
this->server_ip_address_, this->server_port_);
memset(&this->destination_, 0, sizeof(this->destination_)); if (this->destination_len_ == 0) {
this->destination_.sin_family = AF_INET; std::string error_message(strerror(errno));
this->destination_.sin_port = htons(this->server_port_); ESP_LOGE(TAG, "Cannot use IP address '%s' or port %d for server connection: %s",
this->destination_.sin_addr.s_addr = inet_addr(this->server_ip_address_.c_str()); this->server_ip_address_.c_str(), this->server_port_,
error_message.c_str());
}
#endif
} }
void Syslog::loop() { void Syslog::loop() {
@@ -130,14 +134,26 @@ void Syslog::log(int level, const std::string &tag, const std::string &msg) {
<< App.get_name() << " - - - " << msg; << App.get_name() << " - - - " << msg;
std::string payload = payload_stream.str(); std::string payload = payload_stream.str();
ssize_t bytes_sent = ::sendto( ssize_t bytes_sent = 0;
this->socket_fd_, #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
payload.c_str(), payload.length(), if (this->destination_len_ > 0) {
0, bytes_sent = this->socket_->sendto(
reinterpret_cast<sockaddr*>(&this->destination_), sizeof(this->destination_)); payload.c_str(), payload.length(),
0,
reinterpret_cast<sockaddr *>(&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()) { 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->latest_error_message_ = strerror(errno);
++this->errors_encountered_; ++this->errors_encountered_;
} }
@@ -145,3 +161,4 @@ void Syslog::log(int level, const std::string &tag, const std::string &msg) {
} // namespace syslog } // namespace syslog
} // namespace esphome } // namespace esphome
#endif
+18 -4
View File
@@ -1,20 +1,27 @@
#pragma once #pragma once
#include "esphome/core/defines.h"
#ifdef USE_NETWORK
/* Potential future improvements: /* Potential future improvements:
- hostname instead of IP address - hostname instead of IP address
- custom mapping of log leves to syslog levels - custom mapping of log leves to syslog levels
- set custom facility - set custom facility
- protocol support: Structured Data, BSD, IPv6 - protocol support: Structured Data, BSD
- allow specifying time component to get timestamp - allow specifying time component to get timestamp
- code optimization (e.g. marking methods as HOT or using fewer complex ops) - 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/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/automation.h" #include "esphome/core/automation.h"
#include "esphome/core/log.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" #include "esphome/components/socket/socket.h"
#else
#include "WiFiUdp.h"
#endif
namespace esphome { namespace esphome {
@@ -47,8 +54,14 @@ class Syslog : public Component {
bool forward_logger_; bool forward_logger_;
bool strip_color_codes_; bool strip_color_codes_;
int socket_fd_; #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
struct sockaddr_in destination_; std::unique_ptr<esphome::socket::Socket> 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_; int errors_encountered_;
std::string latest_error_message_; std::string latest_error_message_;
@@ -76,3 +89,4 @@ class Syslog : public Component {
} // namespace syslog } // namespace syslog
} // namespace esphome } // namespace esphome
#endif
+1
View File
@@ -16,6 +16,7 @@ packages:
mqtt: !include templates/mqtt.yaml mqtt: !include templates/mqtt.yaml
uptime: !include templates/uptime.yaml uptime: !include templates/uptime.yaml
restart: !include templates/restart.yaml restart: !include templates/restart.yaml
syslog: !include templates/syslog.yaml
logger: logger:
+1
View File
@@ -16,6 +16,7 @@ packages:
mqtt: !include templates/mqtt.yaml mqtt: !include templates/mqtt.yaml
uptime: !include templates/uptime.yaml uptime: !include templates/uptime.yaml
restart: !include templates/restart.yaml restart: !include templates/restart.yaml
syslog: !include templates/syslog.yaml
logger: logger:
+1
View File
@@ -7,6 +7,7 @@ packages:
mqtt: !include mqtt.yaml mqtt: !include mqtt.yaml
uptime: !include uptime.yaml uptime: !include uptime.yaml
restart: !include restart.yaml restart: !include restart.yaml
syslog: !include syslog.yaml
logger: logger:
+1
View File
@@ -7,6 +7,7 @@ packages:
mqtt: !include mqtt.yaml mqtt: !include mqtt.yaml
uptime: !include uptime.yaml uptime: !include uptime.yaml
restart: !include restart.yaml restart: !include restart.yaml
syslog: !include syslog.yaml
logger: logger:
+1
View File
@@ -27,3 +27,4 @@ packages:
mqtt: !include mqtt.yaml mqtt: !include mqtt.yaml
uptime: !include uptime.yaml uptime: !include uptime.yaml
restart: !include restart.yaml restart: !include restart.yaml
syslog: !include syslog.yaml
+1
View File
@@ -7,6 +7,7 @@ packages:
mqtt: !include mqtt.yaml mqtt: !include mqtt.yaml
uptime: !include uptime.yaml uptime: !include uptime.yaml
restart: !include restart.yaml restart: !include restart.yaml
syslog: !include syslog.yaml
logger: logger:
+1
View File
@@ -7,6 +7,7 @@ packages:
mqtt: !include mqtt.yaml mqtt: !include mqtt.yaml
uptime: !include uptime.yaml uptime: !include uptime.yaml
restart: !include restart.yaml restart: !include restart.yaml
syslog: !include syslog.yaml
logger: logger: