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
macros.hpp
Go to the documentation of this file.
1/**
2 * @file macros.hpp
3 * @brief Zero-overhead logging macros with file/line capture
4 *
5 * This header names no device. Without SBL_LOG_OUTPUT and SBL_LOG_TIMESTAMP
6 * the logger writes nothing and stamps 0. Firmware includes <sbl/log.hpp>,
7 * which picks the build's sink and timestamp and then includes this.
8 *
9 * Usage:
10 * @code
11 * // Output sink and timestamp provider
12 * struct MyOutput { static void write(const char* s) { ... } };
13 * struct MyTime { static uint32_t now() { return millis(); } };
14 *
15 * // Configure before including
16 * #define SBL_LOG_OUTPUT MyOutput
17 * #define SBL_LOG_TIMESTAMP MyTime
18 * #define SBL_LOG_LEVEL SBL_LOG_LEVEL_DEBUG
19 * #include <sbl/common/log/macros.hpp>
20 *
21 * SBL_LOG_INFO("Hello %s", "world");
22 * SBL_LOG_ERROR("Failed with code %d", err);
23 * @endcode
24 *
25 * SBL_LOG_LEVEL is an integer, so the preprocessor can filter on it:
26 * - SBL_LOG_LEVEL_OFF (0): disable all logging
27 * - SBL_LOG_LEVEL_ERROR (1): errors only
28 * - SBL_LOG_LEVEL_WARN (2): warnings and above
29 * - SBL_LOG_LEVEL_INFO (3): info and above (default)
30 * - SBL_LOG_LEVEL_DEBUG (4): debug and above
31 * - SBL_LOG_LEVEL_TRACE (5): everything
32 */
33
34#ifndef SBL_COMMON_LOG_MACROS_HPP_
35#define SBL_COMMON_LOG_MACROS_HPP_
36
38
39// Log level as integer for preprocessor (must match Level enum values)
40// Level::Off=0, Error=1, Warn=2, Info=3, Debug=4, Trace=5
41#ifndef SBL_LOG_LEVEL
42#define SBL_LOG_LEVEL 3 // Info
43#endif
44
45// Convenience defines for setting log level
46#define SBL_LOG_LEVEL_OFF 0
47#define SBL_LOG_LEVEL_ERROR 1
48#define SBL_LOG_LEVEL_WARN 2
49#define SBL_LOG_LEVEL_INFO 3
50#define SBL_LOG_LEVEL_DEBUG 4
51#define SBL_LOG_LEVEL_TRACE 5
52
53// The sink and the timestamp provider: the app's, or <sbl/log.hpp>'s choice, or the ones that do nothing.
54#ifndef SBL_LOG_OUTPUT
55#define SBL_LOG_OUTPUT sbl::log::NullOutput
56#endif
57
58#ifndef SBL_LOG_TIMESTAMP
59#define SBL_LOG_TIMESTAMP sbl::log::NullTimestamp
60#endif
61
62// Convert integer level to enum for template
63namespace sbl::log::detail {
64constexpr Level level_from_int(int lvl) { return static_cast<Level>(lvl); }
65} // namespace sbl::log::detail
66
67// Logger type alias
68using SblLogger_ = sbl::log::Logger<SBL_LOG_OUTPUT, SBL_LOG_TIMESTAMP, sbl::log::detail::level_from_int(SBL_LOG_LEVEL)>;
69
70/**
71 * @brief Main logging macros with compile-time level filtering
72 *
73 * These expand to nothing when the level is disabled, achieving
74 * zero overhead for disabled log levels.
75 */
76#if SBL_LOG_LEVEL >= SBL_LOG_LEVEL_ERROR
77#define SBL_LOG_ERROR(...) SblLogger_::log(sbl::log::Level::Error, __FILE__, __LINE__, __VA_ARGS__)
78#else
79#define SBL_LOG_ERROR(...) ((void)0)
80#endif
81
82#if SBL_LOG_LEVEL >= SBL_LOG_LEVEL_WARN
83#define SBL_LOG_WARN(...) SblLogger_::log(sbl::log::Level::Warn, __FILE__, __LINE__, __VA_ARGS__)
84#else
85#define SBL_LOG_WARN(...) ((void)0)
86#endif
87
88#if SBL_LOG_LEVEL >= SBL_LOG_LEVEL_INFO
89#define SBL_LOG_INFO(...) SblLogger_::log(sbl::log::Level::Info, __FILE__, __LINE__, __VA_ARGS__)
90#else
91#define SBL_LOG_INFO(...) ((void)0)
92#endif
93
94#if SBL_LOG_LEVEL >= SBL_LOG_LEVEL_DEBUG
95#define SBL_LOG_DEBUG(...) SblLogger_::log(sbl::log::Level::Debug, __FILE__, __LINE__, __VA_ARGS__)
96#else
97#define SBL_LOG_DEBUG(...) ((void)0)
98#endif
99
100#if SBL_LOG_LEVEL >= SBL_LOG_LEVEL_TRACE
101#define SBL_LOG_TRACE(...) SblLogger_::log(sbl::log::Level::Trace, __FILE__, __LINE__, __VA_ARGS__)
102#else
103#define SBL_LOG_TRACE(...) ((void)0)
104#endif
105
106/**
107 * @brief Conditional logging (runtime condition, compile-time level check)
108 */
109#define SBL_LOG_ERROR_IF(cond, ...) \
110 do { \
111 if (cond) { \
112 SBL_LOG_ERROR(__VA_ARGS__); \
113 } \
114 } while (0)
115
116#define SBL_LOG_WARN_IF(cond, ...) \
117 do { \
118 if (cond) { \
119 SBL_LOG_WARN(__VA_ARGS__); \
120 } \
121 } while (0)
122
123#define SBL_LOG_INFO_IF(cond, ...) \
124 do { \
125 if (cond) { \
126 SBL_LOG_INFO(__VA_ARGS__); \
127 } \
128 } while (0)
129
130/**
131 * @brief Log without file/line (smaller output)
132 */
133#if SBL_LOG_LEVEL > SBL_LOG_LEVEL_OFF
134#define SBL_LOG_SIMPLE(lvl, ...) SblLogger_::log_simple(lvl, __VA_ARGS__)
135#else
136#define SBL_LOG_SIMPLE(lvl, ...) ((void)0)
137#endif
138
139#endif // SBL_COMMON_LOG_MACROS_HPP_
Logger class with compile-time level filtering.
Definition logger.hpp:74
Logger class with timestamp provider and output sink.
constexpr bool is_sink_v
Definition sink.hpp:55
Level
Log severity levels.
Definition logger.hpp:27