13static constexpr uint16_t MAX_HEADER_SIZE = 128;
15static constexpr size_t MAX_TAG_LENGTH = 32;
18static const char LOG_LEVEL_COLOR_DIGIT[] PROGMEM = {
29static const char LOG_LEVEL_LETTER_CHARS[] PROGMEM = {
51 if (this->
pos + MAX_HEADER_SIZE > this->size)
54 char *p = this->current_();
57 p = write_ansi_color(p, level);
66 *p++ =
static_cast<char>(
progmem_read_byte(
reinterpret_cast<const uint8_t *
>(&LOG_LEVEL_LETTER_CHARS[level])));
75 p = copy_string(p,
tag, MAX_TAG_LENGTH);
78 p = copy_string(p,
tag, this->remaining_() - (MAX_HEADER_SIZE - MAX_TAG_LENGTH));
84 if (
line > 999) [[unlikely]] {
85 p = write_digit(p,
line, 1000);
87 p = write_digit(p,
line, 100);
88 p = write_digit(p,
line, 10);
92#if defined(USE_ESP32) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR) || defined(USE_HOST)
94 if (thread_name !=
nullptr) {
95 p = write_ansi_color(p, 1);
97 p = copy_string(p, thread_name, MAX_TAG_LENGTH);
99 p = write_ansi_color(p, level);
109 this->format_vsnprintf_(
format, args);
114 this->format_vsnprintf_p_(
format, args);
121 const int room = this->body_room_();
123 const uint16_t limit = room - 1;
125 memcpy(this->current_(), text, copy_len);
126 this->
pos += copy_len;
127 this->strip_trailing_newlines_();
128 this->write_ansi_reset_();
134 const int room = this->body_room_();
136 char *
const start = this->current_();
138 this->strip_trailing_newlines_();
139 this->write_ansi_reset_();
149 if (this->
pos + REPLAY_HEADER_MAX_SIZE > this->size)
151 char *p = this->current_();
152 p = write_ansi_color(p, level);
158 this->
pos =
static_cast<uint16_t
>(p - this->
data);
172 uint16_t remaining_()
const {
return this->size - this->
pos; }
174 int body_room_()
const {
return this->remaining_() -
ANSI_RESET_LEN; }
175 char *current_() {
return this->data + this->
pos; }
178 inline void ESPHOME_ALWAYS_INLINE finalize_() { this->data[this->
pos] =
'\0'; }
181 inline void ESPHOME_ALWAYS_INLINE write_ansi_reset_() {
182 char *p = this->current_();
190 inline void ESPHOME_ALWAYS_INLINE strip_trailing_newlines_() {
191 while (this->
pos > 0 && this->data[this->
pos - 1] ==
'\n')
194 void process_vsnprintf_result_(
int ret,
int room) {
196 this->
pos += (ret >= room) ? (room - 1) :
ret;
197 this->strip_trailing_newlines_();
199 this->write_ansi_reset_();
201 void format_vsnprintf_(
const char *
format, va_list args) {
202 const int room = this->body_room_();
205 this->process_vsnprintf_result_(vsnprintf(this->current_(), room,
format, args), room);
208 void format_vsnprintf_p_(PGM_P
format, va_list args) {
209 const int room = this->body_room_();
212 this->process_vsnprintf_result_(vsnprintf_P(this->current_(), room,
format, args), room);
216 static inline char *ESPHOME_ALWAYS_INLINE write_digit(
char *p,
int &
value,
int divisor) {
218 while (
value >= divisor) {
226 static char *write_ansi_color(
char *p, uint8_t level) {
232 *p++ = (level == 1) ?
'1' :
'0';
235 *p++ =
static_cast<char>(
progmem_read_byte(
reinterpret_cast<const uint8_t *
>(&LOG_LEVEL_COLOR_DIGIT[level])));
241 static inline char *ESPHOME_ALWAYS_INLINE copy_string(
char *p,
const char *str,
size_t max_len) {
246 const size_t len = strnlen(str, max_len);
char *ESPHOME_ALWAYS_INLINE progmem_copy_str(char *dst, const char *end, const char *src)
Copy a PROGMEM string to dst, stopping at its null terminator or at end, and return the new end of ds...
char * uint32_to_str_unchecked(char *buf, uint32_t val)
Write unsigned 32-bit integer to buffer (internal, no size check).
void const char int const char * format
uint8_t progmem_read_byte(const uint8_t *addr)
void HOT format_body_p(PGM_P format, va_list args)
void write_replay_header(uint8_t level, uint32_t ms)
static constexpr uint16_t ANSI_COLOR_LEN
static constexpr uint16_t ANSI_RESET_LEN
static constexpr uint16_t REPLAY_HEADER_MAX_SIZE
void write_body(const char *text, uint16_t text_length)
void HOT write_header(uint8_t level, const char *tag, int line, const char *thread_name)
void HOT format_body(const char *format, va_list args)
void __attribute__((noinline)) write_body(const char *text
void HOT ESPHOME_ALWAYS_INLINE write_unformatted_body_p(ESPHOME_PGM_P text)
void terminate_with_newline()
void uint16_t text_length
void HOT ESPHOME_ALWAYS_INLINE write_unformatted_body(const char *text, size_t len)