CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
host_api_log.cpp
Go to the documentation of this file.
1
9
11#include "cdc_log.h"
12
13#include <cstdio>
14
15extern "C" {
16
17void host_log(uint8_t level, const char* tag, const char* msg)
18{
19 if (!tag) tag = "plugin";
20 if (!msg) msg = "";
21 switch (level) {
22 case LOG_LEVEL_ERROR: LOG_E(tag, "%s", msg); break;
23 case LOG_LEVEL_WARN: LOG_W(tag, "%s", msg); break;
24 case LOG_LEVEL_INFO: LOG_I(tag, "%s", msg); break;
25 case LOG_LEVEL_DEBUG: LOG_D(tag, "%s", msg); break;
26 default: LOG_V(tag, "%s", msg); break;
27 }
28}
29
30void host_log_hex(const char* tag, const char* label, const uint8_t* data, size_t len)
31{
32 if (!tag) tag = "plugin";
33 if (!label) label = "";
34 LOG_I(tag, "%s (%zu bytes)", label, len);
35 char line[80];
36 size_t off = 0;
37 while (off < len) {
38 size_t chunk = (len - off) > 16 ? 16 : (len - off);
39 int written = 0;
40 for (size_t i = 0; i < chunk; ++i) {
41 written += std::snprintf(line + written, sizeof(line) - written,
42 "%02X ", data[off + i]);
43 }
44 LOG_I(tag, " %04zu %s", off, line);
45 off += chunk;
46 }
47}
48
49} // extern "C"
CDC Log: logging over TinyUSB CDC and UART.
#define LOG_W(tag, fmt,...)
Definition cdc_log.h:146
#define LOG_D(tag, fmt,...)
Definition cdc_log.h:148
#define LOG_I(tag, fmt,...)
Definition cdc_log.h:147
#define LOG_V(tag, fmt,...)
Definition cdc_log.h:149
#define LOG_E(tag, fmt,...)
Definition cdc_log.h:145
#define LOG_LEVEL_DEBUG
Definition host_api.h:61
#define LOG_LEVEL_WARN
Definition host_api.h:59
void host_log_hex(const char *tag, const char *label, const uint8_t *data, size_t len)
Write a labelled hex dump of a binary buffer at debug level.
#define LOG_LEVEL_INFO
Definition host_api.h:60
#define LOG_LEVEL_ERROR
Definition host_api.h:58
void host_log(uint8_t level, const char *tag, const char *msg)
Write a single log line at the given level.
CDC Badge OS plugin host API - canonical C ABI contract.