Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.

Commit a97eaef

Browse files
committed
Fix esp-idf and IPv6 support by using plain UDP socket, drop Syslog library
It turns out to be fairly simple to just send the UDP packets directly, and the UDP socket handling can then be a whole lot more generic.
1 parent 6cab198 commit a97eaef

File tree

4 files changed

+33
-42
lines changed

4 files changed

+33
-42
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
A simple syslog component for esphome. The component is designed to auto attach itself to the logger core module (like the MQTT component does with the `log_topic`)
44

5-
This component uses the https://github.com/arcao/Syslog library version 2.0 at its core
6-
75
## How to
86

97
### Manually

components/syslog/__init__.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,22 @@
88
CONF_ENABLE_LOGGER_MESSAGES = "enable_logger"
99
CONF_MIN_LEVEL = "min_level"
1010

11-
DEPENDENCIES = ['logger','network']
11+
DEPENDENCIES = ['logger','network','socket']
1212

1313
debug_ns = cg.esphome_ns.namespace('debug')
1414
syslog_ns = cg.esphome_ns.namespace('syslog')
1515

1616
SyslogComponent = syslog_ns.class_('SyslogComponent', cg.Component)
1717
SyslogLogAction = syslog_ns.class_('SyslogLogAction', automation.Action)
1818

19-
CONFIG_SCHEMA = cv.All(
20-
cv.Schema({
21-
cv.GenerateID(): cv.declare_id(SyslogComponent),
22-
cv.Optional(CONF_IP_ADDRESS, default="255.255.255.255"): cv.string_strict,
23-
cv.Optional(CONF_PORT, default=514): cv.port,
24-
cv.Optional(CONF_ENABLE_LOGGER_MESSAGES, default=True): cv.boolean,
25-
cv.Optional(CONF_STRIP_COLORS, default=True): cv.boolean,
26-
cv.Optional(CONF_MIN_LEVEL, default="DEBUG"): is_log_level,
27-
}),
28-
cv.only_with_arduino,
29-
)
19+
CONFIG_SCHEMA = cv.Schema({
20+
cv.GenerateID(): cv.declare_id(SyslogComponent),
21+
cv.Optional(CONF_IP_ADDRESS, default="255.255.255.255"): cv.string_strict,
22+
cv.Optional(CONF_PORT, default=514): cv.port,
23+
cv.Optional(CONF_ENABLE_LOGGER_MESSAGES, default=True): cv.boolean,
24+
cv.Optional(CONF_STRIP_COLORS, default=True): cv.boolean,
25+
cv.Optional(CONF_MIN_LEVEL, default="DEBUG"): is_log_level,
26+
})
3027

3128
SYSLOG_LOG_ACTION_SCHEMA = cv.Schema({
3229
cv.GenerateID(): cv.use_id(SyslogComponent),
@@ -36,8 +33,6 @@
3633
})
3734

3835
def to_code(config):
39-
cg.add_library('Syslog', '2.0.0')
40-
4136
var = cg.new_Pvariable(config[CONF_ID])
4237
yield cg.register_component(var, config)
4338

components/syslog/syslog_component.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,22 @@ static const uint8_t esphome_to_syslog_log_levels[] = {0, 3, 4, 6, 5, 7, 7, 7};
2323

2424
SyslogComponent::SyslogComponent() {
2525
this->settings_.client_id = App.get_name();
26-
// Get the WifiUDP client here instead of getting it in setup() to make sure we always have a client when calling log()
27-
// Calling log() without the device connected should not be an issue since there is a wifi connected check and WifiUDP fails "silently" and doesn't generate an exception anyways
28-
this->udp_ = new WiFiUDP();
2926
}
3027

3128
void SyslogComponent::setup() {
29+
30+
this->server_socklen = socket::set_sockaddr((struct sockaddr *)&this->server, sizeof(this->server),
31+
this->settings_.address, this->settings_.port);
32+
if (!this->server_socklen) {
33+
ESP_LOGW(TAG, "Failed to parse server IP address '%s'", this->settings_.address.c_str());
34+
return;
35+
}
36+
this->socket_ = socket::socket(this->server.ss_family, SOCK_DGRAM, IPPROTO_UDP);
37+
if (!this->socket_) {
38+
ESP_LOGW(TAG, "Failed to create UDP socket");
39+
return;
40+
}
41+
3242
this->log(ESPHOME_LOG_LEVEL_INFO , "syslog", "Syslog started");
3343
ESP_LOGI(TAG, "Started");
3444

@@ -53,21 +63,16 @@ void SyslogComponent::loop() {
5363
void SyslogComponent::log(uint8_t level, const std::string &tag, const std::string &payload) {
5464
level = level > 7 ? 7 : level;
5565

56-
// Simple check to make sure that there is connectivity, if not, log the issue and return
57-
if(WiFi.status() != WL_CONNECTED) {
58-
ESP_LOGW(TAG, "Tried to send \"%s\"@\"%s\" with level %d but Wifi isn't connected yet", tag.c_str(), payload.c_str(), level);
66+
if (!this->socket_) {
67+
ESP_LOGW(TAG, "Tried to send \"%s\"@\"%s\" with level %d but socket isn't connected", tag.c_str(), payload.c_str(), level);
5968
return;
6069
}
6170

62-
Syslog syslog(
63-
*this->udp_,
64-
this->settings_.address.c_str(),
65-
this->settings_.port,
66-
this->settings_.client_id.c_str(),
67-
tag.c_str(),
68-
LOG_KERN
69-
);
70-
if(!syslog.log(esphome_to_syslog_log_levels[level], payload.c_str())) {
71+
int pri = esphome_to_syslog_log_levels[level];
72+
std::string buf = str_sprintf("<%d>1 - %s %s - - - \xEF\xBB\xBF%s",
73+
pri, this->settings_.client_id.c_str(),
74+
tag.c_str(), payload.c_str());
75+
if (this->socket_->sendto(buf.c_str(), buf.length(), 0, (struct sockaddr *)&this->server, this->server_socklen) < 0) {
7176
ESP_LOGW(TAG, "Tried to send \"%s\"@\"%s\" with level %d but but failed for an unknown reason", tag.c_str(), payload.c_str(), level);
7277
}
7378
}

components/syslog/syslog_component.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@
66
#include "esphome/core/defines.h"
77
#include "esphome/core/automation.h"
88
#include "esphome/core/log.h"
9-
#include <Syslog.h>
10-
#include <Udp.h>
11-
12-
#if defined ESP8266 || defined ARDUINO_ESP8266_ESP01
13-
#include <ESP8266WiFi.h>
14-
#else
15-
#include <WiFi.h>
16-
#endif
17-
18-
#include <WiFiUdp.h>
9+
#include "esphome/components/socket/socket.h"
1910

2011
namespace esphome {
2112
namespace syslog {
@@ -51,7 +42,9 @@ class SyslogComponent : public Component {
5142
bool strip_colors;
5243
bool enable_logger;
5344
SYSLOGSettings settings_;
54-
UDP *udp_ = NULL;
45+
std::unique_ptr<socket::Socket> socket_ = nullptr;
46+
struct sockaddr_storage server;
47+
socklen_t server_socklen;
5548
};
5649

5750
template<typename... Ts> class SyslogLogAction : public Action<Ts...> {

0 commit comments

Comments
 (0)