43 this->data[this->
pos++] =
'\n';
44 }
else if (this->size > 0) {
46 this->data[this->size - 1] =
'\n';
52 if (this->
pos + MAX_HEADER_SIZE > this->size)
55 char *p = this->current_();
58 this->write_ansi_color_(p, level);
67 *p++ = LOG_LEVEL_LETTER_CHARS[level];
74 this->copy_string_(p,
tag);
79 if (
line > 999) [[unlikely]] {
80 write_digit(p,
line, 1000);
82 write_digit(p,
line, 100);
83 write_digit(p,
line, 10);
87#if defined(USE_ESP32) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR) || defined(USE_HOST)
89 if (thread_name !=
nullptr) {
90 this->write_ansi_color_(p, 1);
92 this->copy_string_(p, thread_name);
94 this->write_ansi_color_(p, level);
107#ifdef USE_STORE_LOG_STR_IN_FLASH
114 this->write_(text, text_length);
119 bool full_()
const {
return this->
pos >= this->
size; }
120 uint16_t remaining_()
const {
return this->size - this->
pos; }
121 char *current_() {
return this->data + this->
pos; }
122 void write_(
const char *value, uint16_t
length) {
123 const uint16_t available = this->remaining_();
124 const uint16_t copy_len = (
length < available) ?
length : available;
126 memcpy(this->current_(), value, copy_len);
127 this->
pos += copy_len;
132 static constexpr uint16_t RESET_COLOR_LEN =
sizeof(ESPHOME_LOG_RESET_COLOR) - 1;
133 this->write_(ESPHOME_LOG_RESET_COLOR, RESET_COLOR_LEN);
135 this->data[this->full_() ? this->size - 1 : this->
pos] =
'\0';
137 void strip_trailing_newlines_() {
138 while (this->
pos > 0 && this->data[this->
pos - 1] ==
'\n')
141 void process_vsnprintf_result_(
int ret) {
144 const uint16_t rem = this->remaining_();
145 this->
pos += (ret >= rem) ? (rem - 1) :
static_cast<uint16_t
>(ret);
146 this->strip_trailing_newlines_();
148 void format_vsnprintf_(
const char *
format, va_list
args) {
151 this->process_vsnprintf_result_(vsnprintf(this->current_(), this->remaining_(),
format,
args));
153#ifdef USE_STORE_LOG_STR_IN_FLASH
154 void format_vsnprintf_P_(PGM_P
format, va_list
args) {
157 this->process_vsnprintf_result_(vsnprintf_P(this->current_(), this->remaining_(),
format,
args));
161 static inline void ESPHOME_ALWAYS_INLINE write_digit(
char *&p,
int &value,
int divisor) {
163 while (value >= divisor) {
171 void write_ansi_color_(
char *&p, uint8_t level) {
177 *p++ = (level == 1) ?
'1' :
'0';
180 *p++ = LOG_LEVEL_COLOR_DIGIT[level];
185 void copy_string_(
char *&p,
const char *str) {
186 const size_t len = strlen(str);