ESPHome 2025.5.0
Loading...
Searching...
No Matches
esphome_syslog.cpp
Go to the documentation of this file.
1#include "esphome_syslog.h"
2
5#include "esphome/core/time.h"
6
7namespace esphome {
8namespace syslog {
9
10// Map log levels to syslog severity using an array, indexed by ESPHome log level (1-7)
12 3, // NONE
13 3, // ERROR
14 4, // WARN
15 5, // INFO
16 6, // CONFIG
17 7, // DEBUG
18 7, // VERBOSE
19 7 // VERY_VERBOSE
20};
21
24 [this](int level, const char *tag, const char *message) { this->log_(level, tag, message); });
25}
26
27void Syslog::log_(const int level, const char *tag, const char *message) const {
28 if (level > this->log_level_)
29 return;
30 // Syslog PRI calculation: facility * 8 + severity
31 int severity = 7;
32 if ((unsigned) level <= 7) {
33 severity = LOG_LEVEL_TO_SYSLOG_SEVERITY[level];
34 }
35 int pri = this->facility_ * 8 + severity;
36 auto timestamp = this->time_->now().strftime("%b %d %H:%M:%S");
37 unsigned len = strlen(message);
38 // remove color formatting
39 if (this->strip_ && message[0] == 0x1B && len > 11) {
40 message += 7;
41 len -= 11;
42 }
43
44 auto data = str_sprintf("<%d>%s %s %s: %.*s", pri, timestamp.c_str(), App.get_name().c_str(), tag, len, message);
45 this->parent_->send_packet((const uint8_t *) data.data(), data.size());
46}
47
48} // namespace syslog
49} // namespace esphome
const std::string & get_name() const
Get the name of this Application set by pre_setup().
void add_on_log_callback(std::function< void(int, const char *, const char *)> &&callback)
Register a callback that will be called for every log message sent.
Definition logger.cpp:217
time::RealTimeClock * time_
void log_(int level, const char *tag, const char *message) const
ESPTime now()
Get the time in the currently defined timezone.
void send_packet(const uint8_t *data, size_t size)
Logger * global_logger
Definition logger.cpp:251
constexpr int LOG_LEVEL_TO_SYSLOG_SEVERITY[]
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:301
std::string str_sprintf(const char *fmt,...)
Definition helpers.cpp:323
Application App
Global storage of Application pointer - only one Application can exist.
size_t strftime(char *buffer, size_t buffer_len, const char *format)
Convert this ESPTime struct to a null-terminated c string buffer as specified by the format argument.
Definition time.cpp:15
uint16_t timestamp
Definition tt21100.cpp:2