13#include "freertos/FreeRTOS.h"
14#include "freertos/task.h"
16#if CONFIG_TINYUSB_CDC_ENABLED
57 if (level != CDC_LOG_LEVEL_ERROR && level != CDC_LOG_LEVEL_WARN)
return;
62 entry->timestamp_ms = (uint32_t)(esp_timer_get_time() / 1000);
81 if (!entries || max_entries == 0)
return 0;
126 uint32_t secs = e->timestamp_ms / 1000;
127 uint32_t mins = secs / 60;
128 uint32_t hours = mins / 60;
130 hours % 24, mins % 60, secs % 60,
131 e->level == CDC_LOG_LEVEL_ERROR ?
"E" :
"W",
172void log_write(log_level_t level,
const char* tag,
const char* fmt, ...) {
174 bool capture = (level == CDC_LOG_LEVEL_ERROR || level == CDC_LOG_LEVEL_WARN);
180 vsnprintf(buf,
sizeof(buf), fmt, args);
185 snprintf(line,
sizeof(line),
"[%s][%s] %s",
186 level_str[level], tag ? tag :
"???", buf);
213 vsnprintf(buf,
sizeof(buf), fmt, args);
225void log_hex(
const char* tag,
const char* label,
const uint8_t* data,
size_t len) {
228 (void)tag; (void)label; (void)data; (void)len;
230 console_printf(
"[D][%s] %s (%zu bytes): ", tag ? tag :
"HEX", label ? label :
"data", len);
231 for (
size_t i = 0; i < len; i++) {
233 if ((i + 1) % 32 == 0 && (i + 1) < len) {
235 }
else if ((i + 1) % 4 == 0 && (i + 1) < len) {
250 int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
252 fcntl(STDIN_FILENO, F_SETFL,
flags | O_NONBLOCK);
265#if CONFIG_TINYUSB_CDC_ENABLED
266 if (tud_cdc_connected() && tud_cdc_available() > 0) {
286#if CONFIG_TINYUSB_CDC_ENABLED
288 if (tud_cdc_connected() && tud_cdc_available()) {
289 return tud_cdc_read_char();
315 size_t len = strlen(str);
321#if CONFIG_TINYUSB_CDC_ENABLED
331 const TickType_t deadline =
332 xTaskGetTickCount() + pdMS_TO_TICKS(20);
333 while (written < len) {
334 size_t avail = tud_cdc_write_available();
336 tud_cdc_write_flush();
337 if (xTaskGetTickCount() >= deadline)
break;
341 size_t to_write = len - written;
342 if (to_write > avail) to_write = avail;
343 written += tud_cdc_write(str + written, to_write);
345 tud_cdc_write_flush();
366 va_copy(args_copy, args);
367 int len = vsnprintf(buf,
sizeof(buf), fmt, args);
369 if (len > 0 &&
static_cast<size_t>(len) >=
sizeof(buf)) {
370 char* heap =
static_cast<char*
>(malloc(
static_cast<size_t>(len) + 1));
372 vsnprintf(heap,
static_cast<size_t>(len) + 1, fmt, args_copy);
391#if CONFIG_TINYUSB_CDC_ENABLED
393 tud_cdc_write_char(c);
394 tud_cdc_write_flush();
408#if CONFIG_TINYUSB_CDC_ENABLED
410 tud_cdc_write_flush();
static void error_log_add(log_level_t level, const char *message)
Adds warning/error entry to PSRAM-backed ring log.
size_t error_log_get_entries(error_log_entry_t *entries, size_t max_entries)
Copies stored error-log entries in chronological order.
void log_init(void)
Logging API implementation.
size_t error_log_get_count(void)
Returns number of buffered error-log entries.
int console_getchar(void)
Reads one character from available console input source.
void console_register_input_hook(console_input_available_hook_t avail_hook, console_input_getchar_hook_t getchar_hook)
Registers optional additional input transport hooks.
log_level_t log_get_level(void)
Returns current log verbosity threshold.
bool console_available(void)
Returns whether any console input source has pending data.
void console_printf(const char *fmt,...)
Formatted write helper for console output.
static log_level_t s_log_level
static const char * level_str[]
void error_log_dump(void)
Dumps buffered error-log entries to console.
static console_output_hook_t s_output_hook
Optional console hooks for additional I/O transports (for example BLE).
static error_log_entry_t s_error_log[ERROR_LOG_MAX_ENTRIES]
PSRAM-backed error/warn ring log storage (no heap allocation).
void log_set_level(log_level_t level)
Sets runtime log verbosity threshold.
void log_write(log_level_t level, const char *tag, const char *fmt,...)
Writes formatted tagged log line with optional suppression.
static log_authgate_hook_t s_authgate_hook
static bool s_initialized
static size_t s_error_log_head
void console_print(const char *str)
Writes string to active console outputs.
void console_register_output_hook(console_output_hook_t hook)
Console hook registration API.
void console_flush(void)
Flushes buffered console output transports.
void log_register_authgate_hook(log_authgate_hook_t hook)
Installs the auth-gate hook used to suppress INFO/DEBUG/VERBOSE output while a session is unauthentic...
void console_putchar(char c)
Writes single character to active console outputs.
void console_init(void)
Initializes console I/O transport state.
static console_input_available_hook_t s_input_avail_hook
void error_log_clear(void)
Clears error-log ring buffer state.
void log_raw(const char *fmt,...)
Writes untagged raw formatted text to console.
void log_hex(const char *tag, const char *label, const uint8_t *data, size_t len)
Logs binary buffer as grouped hexadecimal bytes.
static size_t s_error_log_count
static console_input_getchar_hook_t s_input_getchar_hook
CDC Log: logging over TinyUSB CDC and UART.
int(* console_input_getchar_hook_t)(void)
Input getchar hook used to fetch one character from an additional source.
bool(* log_authgate_hook_t)(void)
Hook polled before INFO/DEBUG/VERBOSE log lines reach the console.
void(* console_output_hook_t)(const char *data, size_t len)
Output hook called for every console output.
bool(* console_input_available_hook_t)(void)
Input-available hook used to check if additional input is available.
#define ERROR_LOG_MAX_ENTRIES
#define ERROR_LOG_LINE_LEN