Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
banner.hpp
Go to the documentation of this file.
1/**
2 * @file banner.hpp
3 * @brief Boot banner formatter
4 *
5 * Formats a startup banner with app name, build info, MCU metadata,
6 * and audio configuration. Uses __has_include for optional headers
7 * so it works in both firmware and unit test builds.
8 *
9 * Usage:
10 * @code
11 * #include <sbl/common/log/banner.hpp>
12 *
13 * char buf[384];
14 * int len = sbl::log::format_banner(buf, sizeof(buf),
15 * "hello-instrument", 48000, 48);
16 * SBL_LOG_OUTPUT::write(buf);
17 * @endcode
18 *
19 * Gated behind SBL_BOOT_BANNER (default: 1). Set to 0 to disable.
20 */
21#ifndef SBL_COMMON_LOG_BANNER_HPP_
22#define SBL_COMMON_LOG_BANNER_HPP_
23
24#include <cstddef>
25#include <cstdint>
26
27#ifndef SBL_BOOT_BANNER
28#define SBL_BOOT_BANNER 1
29#endif
30
31// Optional constexpr-only headers - silently omitted when absent
32#if __has_include(<sbl/hw/config/mcu.hpp>)
33#include <sbl/hw/config/mcu.hpp>
34#define SBL_BANNER_HAS_MCU 1
35#else
36#define SBL_BANNER_HAS_MCU 0
37#endif
38
39#if __has_include(<sbl/hw/config/target.hpp>)
40#include <sbl/hw/config/target.hpp>
41#define SBL_BANNER_HAS_TARGET 1
42#else
43#define SBL_BANNER_HAS_TARGET 0
44#endif
45
46#if __has_include(<sbl/version.hpp>)
47#include <sbl/version.hpp>
48#define SBL_BANNER_HAS_VERSION 1
49#else
50#define SBL_BANNER_HAS_VERSION 0
51#endif
52
53namespace sbl::log {
54
55namespace detail {
56
57// Minimal string append helpers - no printf dependency
58inline int banner_append(char* buf, size_t size, int pos, const char* str) {
59 while (*str && static_cast<size_t>(pos) < size - 1) {
60 buf[pos++] = *str++;
61 }
62 return pos;
63}
64
65inline int banner_append_uint(char* buf, size_t size, int pos, uint32_t val) {
66 char tmp[12];
67 int len = 0;
68 if (val == 0) {
69 tmp[len++] = '0';
70 } else {
71 char rev[12];
72 int rlen = 0;
73 while (val > 0) {
74 rev[rlen++] = static_cast<char>('0' + (val % 10));
75 val /= 10;
76 }
77 while (rlen > 0) {
78 tmp[len++] = rev[--rlen];
79 }
80 }
81 for (int i = 0; i < len && static_cast<size_t>(pos) < size - 1; ++i) {
82 buf[pos++] = tmp[i];
83 }
84 return pos;
85}
86
87} // namespace detail
88
89/**
90 * @brief Format a boot banner into a buffer
91 *
92 * @param buf Output buffer
93 * @param size Buffer size (recommend >= 384)
94 * @param app_name Application name (e.g., "hello-instrument")
95 * @param sample_rate Audio sample rate in Hz (e.g., 48000)
96 * @param block_size Audio block size in frames (e.g., 48)
97 * @return Number of characters written (excluding null terminator)
98 */
99inline int format_banner(char* buf, size_t size, const char* app_name, uint32_t sample_rate, uint16_t block_size) {
100#if !SBL_BOOT_BANNER
101 (void)app_name;
102 (void)sample_rate;
103 (void)block_size;
104 if (size > 0) {
105 buf[0] = '\0';
106 }
107 return 0;
108#else
109 if (size == 0 || buf == nullptr) {
110 return 0;
111 }
112
113 int p = 0;
114 const char* sep = "======================================\r\n";
115
116 p = detail::banner_append(buf, size, p, sep);
117
118 // App name + version
119 p = detail::banner_append(buf, size, p, " ");
120 p = detail::banner_append(buf, size, p, app_name);
121#if SBL_BANNER_HAS_VERSION
122 p = detail::banner_append(buf, size, p, " (sbl ");
123 p = detail::banner_append(buf, size, p, sbl::version::string);
124 p = detail::banner_append(buf, size, p, ")");
125#endif
126 p = detail::banner_append(buf, size, p, "\r\n");
127
128 // Build date/time
129 p = detail::banner_append(buf, size, p, " Built: ");
130 p = detail::banner_append(buf, size, p, __DATE__);
131 p = detail::banner_append(buf, size, p, " ");
132 p = detail::banner_append(buf, size, p, __TIME__);
133 p = detail::banner_append(buf, size, p, "\r\n");
134
135 // MCU info
136#if SBL_BANNER_HAS_MCU
137 p = detail::banner_append(buf, size, p, " MCU: ");
138 p = detail::banner_append(buf, size, p, sbl::hw::mcu::name);
139#if SBL_BANNER_HAS_TARGET
140 p = detail::banner_append(buf, size, p, " @ ");
141 p = detail::banner_append_uint(buf, size, p, sbl::hw::target::sysclk_mhz);
142 p = detail::banner_append(buf, size, p, " MHz");
143#endif
144 p = detail::banner_append(buf, size, p, "\r\n");
145#endif
146
147 // Audio config
148 p = detail::banner_append(buf, size, p, " Audio: ");
149 p = detail::banner_append_uint(buf, size, p, sample_rate);
150 p = detail::banner_append(buf, size, p, " Hz / ");
151 p = detail::banner_append_uint(buf, size, p, block_size);
152 p = detail::banner_append(buf, size, p, " frames\r\n");
153
154 // Profiling status
155#ifdef SBL_PROFILING_ENABLED
156 p = detail::banner_append(buf, size, p, " Profiling: enabled\r\n");
157#endif
158
159 // Git info
160#if SBL_BANNER_HAS_VERSION
161 p = detail::banner_append(buf, size, p, " Git: ");
162 p = detail::banner_append(buf, size, p, sbl::version::git_hash);
163 if (sbl::version::git_dirty) {
164 p = detail::banner_append(buf, size, p, "-dirty");
165 }
166 p = detail::banner_append(buf, size, p, "\r\n");
167#endif
168
169 p = detail::banner_append(buf, size, p, sep);
170
171 buf[static_cast<size_t>(p) < size ? p : size - 1] = '\0';
172 return p;
173#endif // SBL_BOOT_BANNER
174}
175
176} // namespace sbl::log
177
178#endif // SBL_COMMON_LOG_BANNER_HPP_
Logging.
Definition banner.hpp:53
int format_banner(char *buf, size_t size, const char *app_name, uint32_t sample_rate, uint16_t block_size)
Format a boot banner into a buffer.
Definition banner.hpp:99