CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
cdc_log.cpp File Reference

Logging and console I/O implementation with optional hook transports. More...

#include "cdc_log.h"
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "sdkconfig.h"
#include "esp_timer.h"
#include "esp_attr.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

Go to the source code of this file.

Macros

#define DEBUG_MODE   1

Functions

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.
size_t error_log_get_count (void)
 Returns number of buffered error-log entries.
void error_log_clear (void)
 Clears error-log ring buffer state.
void error_log_dump (void)
 Dumps buffered error-log entries to console.
void log_init (void)
 Logging API implementation.
void log_set_level (log_level_t level)
 Sets runtime log verbosity threshold.
log_level_t log_get_level (void)
 Returns current log verbosity threshold.
void log_write (log_level_t level, const char *tag, const char *fmt,...)
 Writes formatted tagged log line with optional suppression.
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.
void console_init (void)
 Initializes console I/O transport state.
bool console_available (void)
 Returns whether any console input source has pending data.
int console_getchar (void)
 Reads one character from available console input source.
void console_print (const char *str)
 Writes string to active console outputs.
void console_printf (const char *fmt,...)
 Formatted write helper for console output.
void console_putchar (char c)
 Writes single character to active console outputs.
void console_flush (void)
 Flushes buffered console output transports.
void console_register_output_hook (console_output_hook_t hook)
 Console hook registration API.
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.
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 unauthenticated.

Variables

static log_level_t s_log_level = CDC_LOG_LEVEL_DEBUG
static bool s_initialized = false
static console_output_hook_t s_output_hook = nullptr
 Optional console hooks for additional I/O transports (for example BLE).
static console_input_available_hook_t s_input_avail_hook = nullptr
static log_authgate_hook_t s_authgate_hook = nullptr
static console_input_getchar_hook_t s_input_getchar_hook = nullptr
static const char * level_str []
static error_log_entry_t s_error_log [ERROR_LOG_MAX_ENTRIES]
 PSRAM-backed error/warn ring log storage (no heap allocation).
static size_t s_error_log_head = 0
static size_t s_error_log_count = 0

Detailed Description

Logging and console I/O implementation with optional hook transports.

Definition in file cdc_log.cpp.

Macro Definition Documentation

◆ DEBUG_MODE

#define DEBUG_MODE   1

Definition at line 20 of file cdc_log.cpp.

Function Documentation

◆ console_available()

bool console_available ( void )

Returns whether any console input source has pending data.

Returns
true if input is available.

Definition at line 261 of file cdc_log.cpp.

References s_initialized, and s_input_avail_hook.

Referenced by cdc::serial::Console::available().

◆ console_flush()

void console_flush ( void )

Flushes buffered console output transports.

Definition at line 393 of file cdc_log.cpp.

References s_initialized.

Referenced by cdc::hal::WifiController::enable(), cdc::serial::Console::flush(), and cdc::hal::WifiController::onWifiEvent().

◆ console_getchar()

int console_getchar ( void )

Reads one character from available console input source.

Returns
Character value or -1 when no data is available.

Definition at line 282 of file cdc_log.cpp.

References s_initialized, and s_input_getchar_hook.

Referenced by cdc::serial::Console::getchar().

◆ console_init()

void console_init ( void )

Initializes console I/O transport state.

Definition at line 245 of file cdc_log.cpp.

References flags, and s_initialized.

Referenced by log_init().

◆ console_print()

void console_print ( const char * str)

Writes string to active console outputs.

Parameters
strNull-terminated string.

Definition at line 312 of file cdc_log.cpp.

References s_initialized, and s_output_hook.

Referenced by console_printf(), log_hex(), log_raw(), and cdc::serial::Console::print().

◆ console_printf()

void console_printf ( const char * fmt,
... )

Formatted write helper for console output.

Parameters
fmtPrintf-style format string.

Definition at line 358 of file cdc_log.cpp.

References console_print().

Referenced by error_log_dump(), log_hex(), and log_write().

◆ console_putchar()

void console_putchar ( char c)

Writes single character to active console outputs.

Parameters
cCharacter to output.

Definition at line 373 of file cdc_log.cpp.

References s_initialized, and s_output_hook.

Referenced by log_hex(), and cdc::serial::Console::putchar().

◆ console_register_input_hook()

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.

Registers the console input hooks (only one set supported at a time).

Parameters
avail_hookInput-available callback.
getchar_hookCharacter-read callback.

Definition at line 416 of file cdc_log.cpp.

References s_input_avail_hook, and s_input_getchar_hook.

◆ console_register_output_hook()

void console_register_output_hook ( console_output_hook_t hook)

Console hook registration API.

Registers the console output hook (only one supported at a time).

Registers optional additional output transport hook.

Parameters
hookOutput callback.

Definition at line 407 of file cdc_log.cpp.

References s_output_hook.

◆ error_log_add()

void error_log_add ( log_level_t level,
const char * message )
static

Adds warning/error entry to PSRAM-backed ring log.

Parameters
levelLog level.
messageFormatted message text.

Definition at line 55 of file cdc_log.cpp.

References ERROR_LOG_LINE_LEN, ERROR_LOG_MAX_ENTRIES, s_error_log, s_error_log_count, and s_error_log_head.

Referenced by log_write().

◆ error_log_clear()

void error_log_clear ( void )

Clears error-log ring buffer state.

Definition at line 104 of file cdc_log.cpp.

References s_error_log_count, and s_error_log_head.

Referenced by cdc::serial::cmdErrorLog().

◆ error_log_dump()

void error_log_dump ( void )

Dumps buffered error-log entries to console.

Definition at line 112 of file cdc_log.cpp.

References console_printf(), ERROR_LOG_MAX_ENTRIES, s_error_log, s_error_log_count, and s_error_log_head.

Referenced by cdc::serial::cmdErrorLog().

◆ error_log_get_count()

size_t error_log_get_count ( void )

Returns number of buffered error-log entries.

Returns
Entry count.

Definition at line 97 of file cdc_log.cpp.

References s_error_log_count.

◆ error_log_get_entries()

size_t error_log_get_entries ( error_log_entry_t * entries,
size_t max_entries )

Copies stored error-log entries in chronological order.

Parameters
entriesOutput entry buffer.
max_entriesMaximum writable entries.
Returns
Number of copied entries.

Definition at line 79 of file cdc_log.cpp.

References ERROR_LOG_MAX_ENTRIES, s_error_log, s_error_log_count, and s_error_log_head.

◆ log_get_level()

log_level_t log_get_level ( void )

Returns current log verbosity threshold.

Returns
Active log level.

Definition at line 161 of file cdc_log.cpp.

References s_log_level.

◆ log_hex()

void log_hex ( const char * tag,
const char * label,
const uint8_t * data,
size_t len )

Logs binary buffer as grouped hexadecimal bytes.

Parameters
tagLog tag.
labelData label.
dataInput byte buffer.
lenBuffer length.

Definition at line 224 of file cdc_log.cpp.

References console_print(), console_printf(), and console_putchar().

◆ log_init()

void log_init ( void )

Logging API implementation.

Initializes logging subsystem and console backend.

Definition at line 140 of file cdc_log.cpp.

References console_init(), and s_log_level.

Referenced by initCoreServices().

◆ log_raw()

void log_raw ( const char * fmt,
... )

Writes untagged raw formatted text to console.

Parameters
fmtPrintf-style format string.

Definition at line 208 of file cdc_log.cpp.

References console_print().

◆ log_register_authgate_hook()

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 unauthenticated.

Registers (or clears) the auth-gate hook for INFO/DEBUG/VERBOSE.

Parameters
hookCallback returning true when the gate is open, false to drop the line. NULL disables gating.

Definition at line 428 of file cdc_log.cpp.

References s_authgate_hook.

Referenced by cdc::serial::SerialCmd::init().

◆ log_set_level()

void log_set_level ( log_level_t level)

Sets runtime log verbosity threshold.

Parameters
levelNew log level.

Definition at line 153 of file cdc_log.cpp.

References s_log_level.

Referenced by cdc::serial::SerialCmd::authenticate(), cdc::serial::SerialCmd::init(), cdc::serial::SerialCmd::isAuthenticated(), and cdc::serial::SerialCmd::logout().

◆ log_write()

void log_write ( log_level_t level,
const char * tag,
const char * fmt,
... )

Writes formatted tagged log line with optional suppression.

Parameters
levelLog level.
tagLog tag.
fmtPrintf-style format string.

Definition at line 171 of file cdc_log.cpp.

References console_printf(), error_log_add(), level_str, s_authgate_hook, and s_log_level.

Variable Documentation

◆ level_str

const char* level_str[]
static
Initial value:
= {
"",
"E",
"W",
"I",
"D",
"V"
}

Definition at line 36 of file cdc_log.cpp.

Referenced by log_write().

◆ s_authgate_hook

log_authgate_hook_t s_authgate_hook = nullptr
static

Definition at line 33 of file cdc_log.cpp.

Referenced by log_register_authgate_hook(), and log_write().

◆ s_error_log

error_log_entry_t s_error_log[ERROR_LOG_MAX_ENTRIES]
static

PSRAM-backed error/warn ring log storage (no heap allocation).

Definition at line 46 of file cdc_log.cpp.

Referenced by error_log_add(), error_log_dump(), and error_log_get_entries().

◆ s_error_log_count

size_t s_error_log_count = 0
static

◆ s_error_log_head

size_t s_error_log_head = 0
static

Definition at line 47 of file cdc_log.cpp.

Referenced by error_log_add(), error_log_clear(), error_log_dump(), and error_log_get_entries().

◆ s_initialized

bool s_initialized = false
static

◆ s_input_avail_hook

console_input_available_hook_t s_input_avail_hook = nullptr
static

Definition at line 32 of file cdc_log.cpp.

Referenced by console_available(), and console_register_input_hook().

◆ s_input_getchar_hook

console_input_getchar_hook_t s_input_getchar_hook = nullptr
static

Definition at line 34 of file cdc_log.cpp.

Referenced by console_getchar(), and console_register_input_hook().

◆ s_log_level

log_level_t s_log_level = CDC_LOG_LEVEL_DEBUG
static

Definition at line 24 of file cdc_log.cpp.

Referenced by log_get_level(), log_init(), log_set_level(), and log_write().

◆ s_output_hook

console_output_hook_t s_output_hook = nullptr
static

Optional console hooks for additional I/O transports (for example BLE).

Definition at line 31 of file cdc_log.cpp.

Referenced by console_print(), console_putchar(), and console_register_output_hook().