Sound Byte Libs 1ee2ca6
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
34// Log level as integer for preprocessor (must match Level enum values)
35// Level::Off=0, Error=1, Warn=2, Info=3, Debug=4, Trace=5
36#ifndef SBL_LOG_LEVEL
37 #define SBL_LOG_LEVEL 3 // Info
38#endif
39
40// Convenience defines for setting log level
41#define SBL_LOG_LEVEL_OFF 0
42#define SBL_LOG_LEVEL_ERROR 1
43#define SBL_LOG_LEVEL_WARN 2
44#define SBL_LOG_LEVEL_INFO 3
45#define SBL_LOG_LEVEL_DEBUG 4
46#define SBL_LOG_LEVEL_TRACE 5
47
48// Output sink must be defined by application
49#ifndef SBL_LOG_OUTPUT
50 #define SBL_LOG_OUTPUT sbl::log::NullOutput
51#endif
52
53// Timestamp provider (optional, defaults to NullTimestamp)
54#ifndef SBL_LOG_TIMESTAMP
55 #define SBL_LOG_TIMESTAMP sbl::log::NullTimestamp
56#endif
57
58// Convert integer level to enum for template
60 constexpr Level level_from_int(int lvl) {
61 return static_cast<Level>(lvl);
62 }
63}
64
65// Logger type alias
70>;
71
72/**
73 * @brief Main logging macros with compile-time level filtering
74 *
75 * These expand to nothing when the level is disabled, achieving
76 * zero overhead for disabled log levels.
77 */
78#if SBL_LOG_LEVEL >= SBL_LOG_LEVEL_ERROR
79 #define SBL_LOG_ERROR(...) \
80 SblLogger_::log(sbl::log::Level::Error, __FILE__, __LINE__, __VA_ARGS__)
81#else
82 #define SBL_LOG_ERROR(...) ((void)0)
83#endif
84
85#if SBL_LOG_LEVEL >= SBL_LOG_LEVEL_WARN
86 #define SBL_LOG_WARN(...) \
87 SblLogger_::log(sbl::log::Level::Warn, __FILE__, __LINE__, __VA_ARGS__)
88#else
89 #define SBL_LOG_WARN(...) ((void)0)
90#endif
91
92#if SBL_LOG_LEVEL >= SBL_LOG_LEVEL_INFO
93 #define SBL_LOG_INFO(...) \
94 SblLogger_::log(sbl::log::Level::Info, __FILE__, __LINE__, __VA_ARGS__)
95#else
96 #define SBL_LOG_INFO(...) ((void)0)
97#endif
98
99#if SBL_LOG_LEVEL >= SBL_LOG_LEVEL_DEBUG
100 #define SBL_LOG_DEBUG(...) \
101 SblLogger_::log(sbl::log::Level::Debug, __FILE__, __LINE__, __VA_ARGS__)
102#else
103 #define SBL_LOG_DEBUG(...) ((void)0)
104#endif
105
106#if SBL_LOG_LEVEL >= SBL_LOG_LEVEL_TRACE
107 #define SBL_LOG_TRACE(...) \
108 SblLogger_::log(sbl::log::Level::Trace, __FILE__, __LINE__, __VA_ARGS__)
109#else
110 #define SBL_LOG_TRACE(...) ((void)0)
111#endif
112
113/**
114 * @brief Conditional logging (runtime condition, compile-time level check)
115 */
116#define SBL_LOG_ERROR_IF(cond, ...) \
117 do { if (cond) { SBL_LOG_ERROR(__VA_ARGS__); } } while(0)
118
119#define SBL_LOG_WARN_IF(cond, ...) \
120 do { if (cond) { SBL_LOG_WARN(__VA_ARGS__); } } while(0)
121
122#define SBL_LOG_INFO_IF(cond, ...) \
123 do { if (cond) { SBL_LOG_INFO(__VA_ARGS__); } } while(0)
124
125/**
126 * @brief Log without file/line (smaller output)
127 */
128#if SBL_LOG_LEVEL > SBL_LOG_LEVEL_OFF
129 #define SBL_LOG_SIMPLE(lvl, ...) \
130 SblLogger_::log_simple(lvl, __VA_ARGS__)
131#else
132 #define SBL_LOG_SIMPLE(lvl, ...) ((void)0)
133#endif
134
135#endif // SBL_LOG_MACROS_HPP_
Logger class with compile-time level filtering.
Definition logger.hpp:89
Logger class with timestamp provider and output sink.
#define SBL_LOG_OUTPUT
Definition macros.hpp:50
#define SBL_LOG_LEVEL
Definition macros.hpp:37
#define SBL_LOG_TIMESTAMP
Definition macros.hpp:55
constexpr Level level_from_int(int lvl)
Definition macros.hpp:60
Level
Log severity levels.
Definition logger.hpp:26