43 this->data[this->
pos++] =
'\n';
44 }
else if (this->size > 0) {
46 this->data[this->size - 1] =
'\n';
50 void HOT
write_header(uint8_t level,
const char *tag,
int line,
const char *thread_name) {
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 int thousands = line / 1000;
81 *p++ =
'0' + thousands;
82 line -= thousands * 1000;
84 int hundreds = line / 100;
85 int remainder = line - hundreds * 100;
86 int tens = remainder / 10;
87 *p++ =
'0' + hundreds;
89 *p++ =
'0' + (remainder - tens * 10);
92#if defined(USE_ESP32) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR) || defined(USE_HOST)
94 if (thread_name !=
nullptr) {
95 this->write_ansi_color_(p, 1);
97 this->copy_string_(p, thread_name);
99 this->write_ansi_color_(p, level);
109 this->format_vsnprintf_(format, args);
112#ifdef USE_STORE_LOG_STR_IN_FLASH
114 this->format_vsnprintf_P_(format, args);
119 this->write_(text, text_length);
124 bool full_()
const {
return this->
pos >= this->
size; }
125 uint16_t remaining_()
const {
return this->size - this->
pos; }
126 char *current_() {
return this->data + this->
pos; }
127 void write_(
const char *value, uint16_t
length) {
128 const uint16_t available = this->remaining_();
129 const uint16_t copy_len = (
length < available) ?
length : available;
131 memcpy(this->current_(), value, copy_len);
132 this->
pos += copy_len;
137 static constexpr uint16_t RESET_COLOR_LEN =
sizeof(ESPHOME_LOG_RESET_COLOR) - 1;
138 this->write_(ESPHOME_LOG_RESET_COLOR, RESET_COLOR_LEN);
140 this->data[this->full_() ? this->size - 1 : this->
pos] =
'\0';
142 void strip_trailing_newlines_() {
143 while (this->
pos > 0 && this->data[this->
pos - 1] ==
'\n')
146 void process_vsnprintf_result_(
int ret) {
149 const uint16_t rem = this->remaining_();
150 this->
pos += (ret >= rem) ? (rem - 1) :
static_cast<uint16_t
>(ret);
151 this->strip_trailing_newlines_();
153 void format_vsnprintf_(
const char *format, va_list args) {
156 this->process_vsnprintf_result_(vsnprintf(this->current_(), this->remaining_(), format, args));
158#ifdef USE_STORE_LOG_STR_IN_FLASH
159 void format_vsnprintf_P_(PGM_P format, va_list args) {
162 this->process_vsnprintf_result_(vsnprintf_P(this->current_(), this->remaining_(), format, args));
167 void write_ansi_color_(
char *&p, uint8_t level) {
173 *p++ = (level == 1) ?
'1' :
'0';
176 *p++ = LOG_LEVEL_COLOR_DIGIT[level];
181 void copy_string_(
char *&p,
const char *str) {
182 const size_t len = strlen(str);