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
logger.hpp
Go to the documentation of this file.
1/**
2 * @file logger.hpp
3 * @brief Logger class with timestamp provider and output sink
4 *
5 * Output format: I 12345 main.cpp:42| Message
6 *
7 * The Logger class is templated on:
8 * - Output: Sink type with static write(const char*) method
9 * - TimestampProvider: Type with static uint32_t now() method
10 *
11 * Use the macros in macros.hpp for automatic file/line capture.
12 */
13
14#ifndef SBL_COMMON_LOG_LOGGER_HPP_
15#define SBL_COMMON_LOG_LOGGER_HPP_
16
17#include <cstdint>
18#include <cstddef>
21
22namespace sbl::log {
23
24/**
25 * @brief Log severity levels
26 */
27enum class Level : uint8_t {
28 Off = 0, // Disable all logging
29 Error = 1,
30 Warn = 2,
31 Info = 3,
32 Debug = 4,
33 Trace = 5
34};
35
36/**
37 * @brief Convert level to single character
38 */
39constexpr char level_char(Level level) {
40 switch (level) {
41 case Level::Error: return 'E';
42 case Level::Warn: return 'W';
43 case Level::Info: return 'I';
44 case Level::Debug: return 'D';
45 case Level::Trace: return 'T';
46 default: return '?';
47 }
48}
49
50/**
51 * @brief Extract filename from path (compile-time safe)
52 *
53 * Returns pointer to last path component (e.g., "main.cpp" from "/foo/bar/main.cpp")
54 */
55constexpr const char* basename(const char* path) {
56 const char* file = path;
57 while (*path) {
58 if (*path == '/' || *path == '\\') {
59 file = path + 1;
60 }
61 path++;
62 }
63 return file;
64}
65
66/**
67 * @brief Logger class with compile-time level filtering
68 *
69 * @tparam Output Sink type with static write(const char*) method
70 * @tparam TimestampProvider Type with static uint32_t now() method
71 * @tparam MinLevel Minimum level to compile (lower levels are eliminated)
72 */
73template<typename Output, typename TimestampProvider = NullTimestamp, Level MinLevel = Level::Info>
74class Logger {
75 static_assert(is_sink_v<Output>, "SBL ERROR: Logger's Output is not a sink.\n"
76 "Required: static void write(const char* str)");
77 static_assert(is_timestamp_v<TimestampProvider>, "SBL ERROR: Logger's TimestampProvider has no now().\n"
78 "Required: static uint32_t now()");
79
80public:
81 static constexpr size_t BUFFER_SIZE = 256;
82
83 /**
84 * @brief Log a message with file/line location
85 *
86 * @param level Log severity level
87 * @param file Source file name
88 * @param line Source line number
89 * @param fmt Printf-style format string
90 * @param ... Format arguments
91 */
92 static void log(Level level, const char* file, int line, const char* fmt, ...) {
93 // Runtime level check (compile-time filtering done in macros)
94 if (static_cast<int>(level) > static_cast<int>(MinLevel)) {
95 return;
96 }
97
98 char buffer[BUFFER_SIZE];
99 size_t pos = 0;
100
101 // Format header: I 12345 file.cpp:42|
102 uint32_t timestamp = TimestampProvider::now();
103 pos += format(buffer, BUFFER_SIZE, "%c %u %s:%d| ", level_char(level), timestamp, basename(file), line);
104
105 // Format message
106 if (pos < BUFFER_SIZE - 1) {
108 va_start(args, fmt);
109 pos += vformat(buffer + pos, BUFFER_SIZE - pos, fmt, args);
110 va_end(args);
111 }
112
113 // Add newline if space
114 if (pos < BUFFER_SIZE - 1) {
115 buffer[pos++] = '\n';
116 buffer[pos] = '\0';
117 }
118
119 // Output
120 Output::write(buffer);
121 }
122
123 /**
124 * @brief Log without location info (simpler format)
125 *
126 * Format: I 12345| Message
127 */
128 static void log_simple(Level level, const char* fmt, ...) {
129 if (static_cast<int>(level) > static_cast<int>(MinLevel)) {
130 return;
131 }
132
133 char buffer[BUFFER_SIZE];
134 size_t pos = 0;
135
136 uint32_t timestamp = TimestampProvider::now();
137 pos += format(buffer, BUFFER_SIZE, "%c %u| ", level_char(level), timestamp);
138
139 if (pos < BUFFER_SIZE - 1) {
141 va_start(args, fmt);
142 pos += vformat(buffer + pos, BUFFER_SIZE - pos, fmt, args);
143 va_end(args);
144 }
145
146 if (pos < BUFFER_SIZE - 1) {
147 buffer[pos++] = '\n';
148 buffer[pos] = '\0';
149 }
150
151 Output::write(buffer);
152 }
153};
154
155} // namespace sbl::log
156
157#endif // SBL_COMMON_LOG_LOGGER_HPP_
Logger class with compile-time level filtering.
Definition logger.hpp:74
static void log(Level level, const char *file, int line, const char *fmt,...)
Log a message with file/line location.
Definition logger.hpp:92
static void log_simple(Level level, const char *fmt,...)
Log without location info (simpler format)
Definition logger.hpp:128
Minimal safe string formatting for embedded systems.
Logging.
Definition banner.hpp:53
constexpr bool is_sink_v
Definition sink.hpp:55
constexpr char level_char(Level level)
Convert level to single character.
Definition logger.hpp:39
constexpr const char * basename(const char *path)
Extract filename from path (compile-time safe)
Definition logger.hpp:55
int vformat(char *buf, size_t size, const char *fmt, va_list args)
Format a string into a buffer (vsnprintf-style)
Definition format.hpp:35
int format(char *buf, size_t size, const char *fmt,...)
Format a string into a buffer (snprintf-style)
Definition format.hpp:265
Level
Log severity levels.
Definition logger.hpp:27
The logger's two contracts: an output sink and a timestamp provider.