mirror of
https://github.com/esphome/esphome.git
synced 2025-10-27 13:13:50 +00:00
Optimize logger callback API by including message length parameter (#9368)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -21,10 +21,12 @@ constexpr int LOG_LEVEL_TO_SYSLOG_SEVERITY[] = {
|
||||
|
||||
void Syslog::setup() {
|
||||
logger::global_logger->add_on_log_callback(
|
||||
[this](int level, const char *tag, const char *message) { this->log_(level, tag, message); });
|
||||
[this](int level, const char *tag, const char *message, size_t message_len) {
|
||||
this->log_(level, tag, message, message_len);
|
||||
});
|
||||
}
|
||||
|
||||
void Syslog::log_(const int level, const char *tag, const char *message) const {
|
||||
void Syslog::log_(const int level, const char *tag, const char *message, size_t message_len) const {
|
||||
if (level > this->log_level_)
|
||||
return;
|
||||
// Syslog PRI calculation: facility * 8 + severity
|
||||
@@ -34,7 +36,7 @@ void Syslog::log_(const int level, const char *tag, const char *message) const {
|
||||
}
|
||||
int pri = this->facility_ * 8 + severity;
|
||||
auto timestamp = this->time_->now().strftime("%b %d %H:%M:%S");
|
||||
unsigned len = strlen(message);
|
||||
size_t len = message_len;
|
||||
// remove color formatting
|
||||
if (this->strip_ && message[0] == 0x1B && len > 11) {
|
||||
message += 7;
|
||||
|
||||
@@ -17,7 +17,7 @@ class Syslog : public Component, public Parented<udp::UDPComponent> {
|
||||
|
||||
protected:
|
||||
int log_level_;
|
||||
void log_(int level, const char *tag, const char *message) const;
|
||||
void log_(int level, const char *tag, const char *message, size_t message_len) const;
|
||||
time::RealTimeClock *time_;
|
||||
bool strip_{true};
|
||||
int facility_{16};
|
||||
|
||||
Reference in New Issue
Block a user