Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
log_macros.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
36#pragma once
37
38#include "log_functions.h"
39
40// =============================================================================
41// Variadic Macro Dispatch Helpers
42// =============================================================================
43
54// Argument counter: returns 2 for 2+ args, 1 for 1 arg
55#define KCENON_LOG_ARG_COUNT(...) \
56 KCENON_LOG_ARG_COUNT_IMPL(__VA_ARGS__, 2, 1)
57#define KCENON_LOG_ARG_COUNT_IMPL(_1, _2, N, ...) N
58
59// Token paste helpers for macro dispatch
60#define KCENON_LOG_PASTE(a, b) KCENON_LOG_PASTE_IMPL(a, b)
61#define KCENON_LOG_PASTE_IMPL(a, b) a##b
62
63// Dispatch macro: calls LEVEL_1 or LEVEL_2 based on argument count
64#define KCENON_LOG_DISPATCH(LEVEL, ...) \
65 KCENON_LOG_PASTE(LEVEL##_, KCENON_LOG_ARG_COUNT(__VA_ARGS__))(__VA_ARGS__)
66
67// =============================================================================
68// Primary LOG_* Macros (Unified Variadic Interface)
69// =============================================================================
70
82#define LOG_TRACE(...) KCENON_LOG_DISPATCH(LOG_TRACE, __VA_ARGS__)
83#define LOG_TRACE_1(msg) \
84 ::kcenon::common::logging::log_trace(msg)
85#define LOG_TRACE_2(logger_name, msg) \
86 ::kcenon::common::logging::log_trace_to(logger_name, msg)
87
99#define LOG_DEBUG(...) KCENON_LOG_DISPATCH(LOG_DEBUG, __VA_ARGS__)
100#define LOG_DEBUG_1(msg) \
101 ::kcenon::common::logging::log_debug(msg)
102#define LOG_DEBUG_2(logger_name, msg) \
103 ::kcenon::common::logging::log_debug_to(logger_name, msg)
104
116#define LOG_INFO(...) KCENON_LOG_DISPATCH(LOG_INFO, __VA_ARGS__)
117#define LOG_INFO_1(msg) \
118 ::kcenon::common::logging::log_info(msg)
119#define LOG_INFO_2(logger_name, msg) \
120 ::kcenon::common::logging::log_info_to(logger_name, msg)
121
133#define LOG_WARNING(...) KCENON_LOG_DISPATCH(LOG_WARNING, __VA_ARGS__)
134#define LOG_WARNING_1(msg) \
135 ::kcenon::common::logging::log_warning(msg)
136#define LOG_WARNING_2(logger_name, msg) \
137 ::kcenon::common::logging::log_warning_to(logger_name, msg)
138
150#define LOG_ERROR(...) KCENON_LOG_DISPATCH(LOG_ERROR, __VA_ARGS__)
151#define LOG_ERROR_1(msg) \
152 ::kcenon::common::logging::log_error(msg)
153#define LOG_ERROR_2(logger_name, msg) \
154 ::kcenon::common::logging::log_error_to(logger_name, msg)
155
167#define LOG_CRITICAL(...) KCENON_LOG_DISPATCH(LOG_CRITICAL, __VA_ARGS__)
168#define LOG_CRITICAL_1(msg) \
169 ::kcenon::common::logging::log_critical(msg)
170#define LOG_CRITICAL_2(logger_name, msg) \
171 ::kcenon::common::logging::log_critical_to(logger_name, msg)
172
173// =============================================================================
174// Conditional Logging Macros
175// =============================================================================
176
193#define LOG_IF(level, msg) \
194 do { \
195 if (::kcenon::common::logging::is_enabled(level)) { \
196 ::kcenon::common::logging::log(level, msg); \
197 } \
198 } while (0)
199
208#define LOG_IF_TO(level, logger_name, msg) \
209 do { \
210 if (::kcenon::common::logging::is_enabled(level, logger_name)) { \
211 ::kcenon::common::logging::log(level, msg, logger_name); \
212 } \
213 } while (0)
214
215// =============================================================================
216// Utility Macros
217// =============================================================================
218
223#define LOG_FLUSH() \
224 ::kcenon::common::logging::flush()
225
231#define LOG_FLUSH_TO(logger_name) \
232 ::kcenon::common::logging::flush(logger_name)
233
240#define LOG_IS_ENABLED(level) \
241 ::kcenon::common::logging::is_enabled(level)
242
250#define LOG_IS_ENABLED_FOR(level, logger_name) \
251 ::kcenon::common::logging::is_enabled(level, logger_name)
252
253// =============================================================================
254// Compile-time Log Level Control (Optional)
255// =============================================================================
256
280#ifndef KCENON_MIN_LOG_LEVEL
281 #define KCENON_MIN_LOG_LEVEL 0
282#endif
283
284// Redefine macros if compile-time level filtering is enabled
285// Each level is disabled when KCENON_MIN_LOG_LEVEL exceeds its threshold
286// Note: variadic LOG_* macros are replaced with no-op when compile-time level filtering is enabled
287#if KCENON_MIN_LOG_LEVEL > 0 // threshold: trace = 0
288 #undef LOG_TRACE
289 #define LOG_TRACE(...) ((void)0)
290#endif
291
292#if KCENON_MIN_LOG_LEVEL > 1 // threshold: debug = 1
293 #undef LOG_DEBUG
294 #define LOG_DEBUG(...) ((void)0)
295#endif
296
297#if KCENON_MIN_LOG_LEVEL > 2 // threshold: info = 2
298 #undef LOG_INFO
299 #define LOG_INFO(...) ((void)0)
300#endif
301
302#if KCENON_MIN_LOG_LEVEL > 3 // threshold: warning = 3
303 #undef LOG_WARNING
304 #define LOG_WARNING(...) ((void)0)
305#endif
306
307#if KCENON_MIN_LOG_LEVEL > 4 // threshold: error = 4
308 #undef LOG_ERROR
309 #define LOG_ERROR(...) ((void)0)
310#endif
311
312#if KCENON_MIN_LOG_LEVEL > 5 // threshold: critical = 5
313 #undef LOG_CRITICAL
314 #define LOG_CRITICAL(...) ((void)0)
315#endif
Unified logging functions with source_location support.