CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
cdc_log.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <stdio.h>
11#include <stdarg.h>
12#include <stdbool.h>
13#include <stdint.h>
14#include <stddef.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20#ifdef __DOXYGEN__
21namespace cdc::log {
22#endif
23
24// Log levels (prefixed with CDC_ to avoid NimBLE conflicts)
33
34// ============================================================================
35// Error Log (WARNING and ERROR messages, PSRAM-backed stack)
36// ============================================================================
37#define ERROR_LOG_MAX_ENTRIES 50
38#define ERROR_LOG_LINE_LEN 100
39
45
46#ifdef __DOXYGEN__
47} // namespace cdc::log
48#endif
49
50// Get error log entries (returns count, fills entries array)
51size_t error_log_get_entries(error_log_entry_t* entries, size_t max_entries);
52size_t error_log_get_count(void);
53void error_log_clear(void);
54void error_log_dump(void); // Dump to console
55
56// ============================================================================
57// Logging API
58// ============================================================================
59
60// Initialize logging system (call after USB CDC is ready)
61void log_init(void);
62
63// Set the log level (messages below this level are suppressed)
64void log_set_level(log_level_t level);
65log_level_t log_get_level(void);
66
67// Core logging function
68void log_write(log_level_t level, const char* tag, const char* fmt, ...);
69
70// Raw output (no level check, no prefix)
71void log_raw(const char* fmt, ...);
72
73// Hex dump
74void log_hex(const char* tag, const char* label, const uint8_t* data, size_t len);
75
76// ============================================================================
77// Console I/O (for serial command interface)
78// ============================================================================
79
80void console_init(void);
81bool console_available(void);
82int console_getchar(void);
83void console_print(const char* str);
84void console_printf(const char* fmt, ...);
85void console_putchar(char c);
86void console_flush(void);
87
88// ============================================================================
89// Console Hooks (for additional I/O transports like BLE)
90// ============================================================================
91
97typedef void (*console_output_hook_t)(const char* data, size_t len);
98
103typedef bool (*console_input_available_hook_t)(void);
104
109typedef int (*console_input_getchar_hook_t)(void);
110
116
123 console_input_getchar_hook_t getchar_hook);
124
132typedef bool (*log_authgate_hook_t)(void);
133
139
140#ifdef __cplusplus
141}
142#endif
143
144// Convenience macros
145#define LOG_E(tag, fmt, ...) log_write(CDC_LOG_LEVEL_ERROR, tag, fmt, ##__VA_ARGS__)
146#define LOG_W(tag, fmt, ...) log_write(CDC_LOG_LEVEL_WARN, tag, fmt, ##__VA_ARGS__)
147#define LOG_I(tag, fmt, ...) log_write(CDC_LOG_LEVEL_INFO, tag, fmt, ##__VA_ARGS__)
148#define LOG_D(tag, fmt, ...) log_write(CDC_LOG_LEVEL_DEBUG, tag, fmt, ##__VA_ARGS__)
149#define LOG_V(tag, fmt, ...) log_write(CDC_LOG_LEVEL_VERBOSE, tag, fmt, ##__VA_ARGS__)
150
151// Short forms without tag (uses "APP")
152#define LOGE(fmt, ...) LOG_E("APP", fmt, ##__VA_ARGS__)
153#define LOGW(fmt, ...) LOG_W("APP", fmt, ##__VA_ARGS__)
154#define LOGI(fmt, ...) LOG_I("APP", fmt, ##__VA_ARGS__)
155#define LOGD(fmt, ...) LOG_D("APP", fmt, ##__VA_ARGS__)
156#define LOGV(fmt, ...) LOG_V("APP", fmt, ##__VA_ARGS__)
int(* console_input_getchar_hook_t)(void)
Input getchar hook used to fetch one character from an additional source.
Definition cdc_log.h:109
size_t error_log_get_entries(error_log_entry_t *entries, size_t max_entries)
Copies stored error-log entries in chronological order.
Definition cdc_log.cpp:79
void log_init(void)
Logging API implementation.
Definition cdc_log.cpp:140
size_t error_log_get_count(void)
Returns number of buffered error-log entries.
Definition cdc_log.cpp:97
int console_getchar(void)
Reads one character from available console input source.
Definition cdc_log.cpp:282
void console_register_input_hook(console_input_available_hook_t avail_hook, console_input_getchar_hook_t getchar_hook)
Registers the console input hooks (only one set supported at a time).
Definition cdc_log.cpp:416
log_level_t log_get_level(void)
Returns current log verbosity threshold.
Definition cdc_log.cpp:161
bool console_available(void)
Returns whether any console input source has pending data.
Definition cdc_log.cpp:261
void console_printf(const char *fmt,...)
Formatted write helper for console output.
Definition cdc_log.cpp:358
void error_log_dump(void)
Dumps buffered error-log entries to console.
Definition cdc_log.cpp:112
void log_set_level(log_level_t level)
Sets runtime log verbosity threshold.
Definition cdc_log.cpp:153
void log_write(log_level_t level, const char *tag, const char *fmt,...)
Writes formatted tagged log line with optional suppression.
Definition cdc_log.cpp:171
void console_print(const char *str)
Writes string to active console outputs.
Definition cdc_log.cpp:312
void console_register_output_hook(console_output_hook_t hook)
Registers the console output hook (only one supported at a time).
Definition cdc_log.cpp:407
void console_flush(void)
Flushes buffered console output transports.
Definition cdc_log.cpp:393
bool(* log_authgate_hook_t)(void)
Hook polled before INFO/DEBUG/VERBOSE log lines reach the console.
Definition cdc_log.h:132
void(* console_output_hook_t)(const char *data, size_t len)
Output hook called for every console output.
Definition cdc_log.h:97
void log_register_authgate_hook(log_authgate_hook_t hook)
Registers (or clears) the auth-gate hook for INFO/DEBUG/VERBOSE.
Definition cdc_log.cpp:428
void console_putchar(char c)
Writes single character to active console outputs.
Definition cdc_log.cpp:373
void console_init(void)
Initializes console I/O transport state.
Definition cdc_log.cpp:245
bool(* console_input_available_hook_t)(void)
Input-available hook used to check if additional input is available.
Definition cdc_log.h:103
void error_log_clear(void)
Clears error-log ring buffer state.
Definition cdc_log.cpp:104
void log_raw(const char *fmt,...)
Writes untagged raw formatted text to console.
Definition cdc_log.cpp:208
#define ERROR_LOG_LINE_LEN
Definition cdc_log.h:38
void log_hex(const char *tag, const char *label, const uint8_t *data, size_t len)
Logs binary buffer as grouped hexadecimal bytes.
Definition cdc_log.cpp:224
log_level_t
Definition cdc_log.h:25
@ CDC_LOG_LEVEL_DEBUG
Definition cdc_log.h:30
@ CDC_LOG_LEVEL_NONE
Definition cdc_log.h:26
@ CDC_LOG_LEVEL_VERBOSE
Definition cdc_log.h:31
@ CDC_LOG_LEVEL_ERROR
Definition cdc_log.h:27
@ CDC_LOG_LEVEL_INFO
Definition cdc_log.h:29
@ CDC_LOG_LEVEL_WARN
Definition cdc_log.h:28
Definition cdc_log.h:40
char message[100]
Definition cdc_log.h:43
log_level_t level
Definition cdc_log.h:42
uint32_t timestamp_ms
Definition cdc_log.h:41