12#include "freertos/FreeRTOS.h"
13#include "freertos/task.h"
15#if CONFIG_TINYUSB_CDC_ENABLED
56 if (level != CDC_LOG_LEVEL_ERROR && level != CDC_LOG_LEVEL_WARN)
return;
61 entry->timestamp_ms = (uint32_t)(esp_timer_get_time() / 1000);
80 if (!entries || max_entries == 0)
return 0;
125 uint32_t secs = e->timestamp_ms / 1000;
126 uint32_t mins = secs / 60;
127 uint32_t hours = mins / 60;
129 hours % 24, mins % 60, secs % 60,
130 e->level == CDC_LOG_LEVEL_ERROR ?
"E" :
"W",
171void log_write(log_level_t level,
const char* tag,
const char* fmt, ...) {
173 bool capture = (level == CDC_LOG_LEVEL_ERROR || level == CDC_LOG_LEVEL_WARN);
179 vsnprintf(buf,
sizeof(buf), fmt, args);
184 snprintf(line,
sizeof(line),
"[%s][%s] %s",
185 level_str[level], tag ? tag :
"???", buf);
212 vsnprintf(buf,
sizeof(buf), fmt, args);
224void log_hex(
const char* tag,
const char* label,
const uint8_t* data,
size_t len) {
227 (void)tag; (void)label; (void)data; (void)len;
229 console_printf(
"[D][%s] %s (%zu bytes): ", tag ? tag :
"HEX", label ? label :
"data", len);
230 for (
size_t i = 0; i < len; i++) {
232 if ((i + 1) % 32 == 0 && (i + 1) < len) {
234 }
else if ((i + 1) % 4 == 0 && (i + 1) < len) {
249 int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
251 fcntl(STDIN_FILENO, F_SETFL,
flags | O_NONBLOCK);
264#if CONFIG_TINYUSB_CDC_ENABLED
265 if (tud_cdc_connected() && tud_cdc_available() > 0) {
285#if CONFIG_TINYUSB_CDC_ENABLED
287 if (tud_cdc_connected() && tud_cdc_available()) {
288 return tud_cdc_read_char();
314 size_t len = strlen(str);
320#if CONFIG_TINYUSB_CDC_ENABLED
330 const TickType_t deadline =
331 xTaskGetTickCount() + pdMS_TO_TICKS(20);
332 while (written < len) {
333 size_t avail = tud_cdc_write_available();
335 tud_cdc_write_flush();
336 if (xTaskGetTickCount() >= deadline)
break;
340 size_t to_write = len - written;
341 if (to_write > avail) to_write = avail;
342 written += tud_cdc_write(str + written, to_write);
344 tud_cdc_write_flush();
364 vsnprintf(buf,
sizeof(buf), fmt, args);
377#if CONFIG_TINYUSB_CDC_ENABLED
379 tud_cdc_write_char(c);
380 tud_cdc_write_flush();
394#if CONFIG_TINYUSB_CDC_ENABLED
396 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