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