6#ifndef ZEPHYR_INCLUDE_LOGGING_LOG_CORE_H_
7#define ZEPHYR_INCLUDE_LOGGING_LOG_CORE_H_
19#define LOG_LEVEL_NONE 0
20#define LOG_LEVEL_ERR 1
21#define LOG_LEVEL_WRN 2
22#define LOG_LEVEL_INF 3
23#define LOG_LEVEL_DBG 4
30#define CONFIG_LOG_DEFAULT_LEVEL 0
31#define CONFIG_LOG_MAX_LEVEL 0
35#define Z_LOG_LOCAL_DOMAIN_ID 0
37#define LOG_FUNCTION_PREFIX_MASK \
38 (((uint32_t)IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_ERR) << \
40 ((uint32_t)IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_WRN) << \
42 ((uint32_t)IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_INF) << \
44 ((uint32_t)IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_DBG) << LOG_LEVEL_DBG))
50#define Z_LOG_RESOLVED_LEVEL(_level, _default) \
51 Z_LOG_RESOLVED_LEVEL1(_level, _default)
53#define Z_LOG_RESOLVED_LEVEL1(_level, _default) \
54 __COND_CODE(_LOG_XXXX##_level, (_level), (_default))
56#define _LOG_XXXX0 _LOG_YYYY,
57#define _LOG_XXXX1 _LOG_YYYY,
58#define _LOG_XXXX2 _LOG_YYYY,
59#define _LOG_XXXX3 _LOG_YYYY,
60#define _LOG_XXXX4 _LOG_YYYY,
76#define Z_LOG_EVAL(_eval_level, _iftrue, _iffalse) \
77 Z_LOG_EVAL1(_eval_level, _iftrue, _iffalse)
79#define Z_LOG_EVAL1(_eval_level, _iftrue, _iffalse) \
80 __COND_CODE(_LOG_ZZZZ##_eval_level, _iftrue, _iffalse)
82#define _LOG_ZZZZ1 _LOG_YYYY,
83#define _LOG_ZZZZ2 _LOG_YYYY,
84#define _LOG_ZZZZ3 _LOG_YYYY,
85#define _LOG_ZZZZ4 _LOG_YYYY,
91#define LOG_CURRENT_MODULE_ID() (__log_level != 0 ? \
92 log_const_source_id(__log_current_const_data) : 0U)
95#define Z_LOG_FUNC_PREFIX_0 0
96#define Z_LOG_FUNC_PREFIX_1 COND_CODE_1(CONFIG_LOG_FUNC_NAME_PREFIX_ERR, (1), (0))
97#define Z_LOG_FUNC_PREFIX_2 COND_CODE_1(CONFIG_LOG_FUNC_NAME_PREFIX_WRN, (1), (0))
98#define Z_LOG_FUNC_PREFIX_3 COND_CODE_1(CONFIG_LOG_FUNC_NAME_PREFIX_INF, (1), (0))
99#define Z_LOG_FUNC_PREFIX_4 COND_CODE_1(CONFIG_LOG_FUNC_NAME_PREFIX_DBG, (1), (0))
110#define Z_LOG_STR_WITH_PREFIX2(...) \
111 "%s: " GET_ARG_N(1, __VA_ARGS__), (const char *)__func__\
112 COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__),\
114 (, GET_ARGS_LESS_N(1, __VA_ARGS__))\
120#define Z_LOG_STR_WITH_PREFIX(...) \
121 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
122 ("%s", (const char *)__func__), \
123 (Z_LOG_STR_WITH_PREFIX2(__VA_ARGS__)))
130#define Z_LOG_STR(_level, ...) \
131 COND_CODE_1(UTIL_CAT(Z_LOG_FUNC_PREFIX_##_level), \
132 (Z_LOG_STR_WITH_PREFIX(__VA_ARGS__)), (__VA_ARGS__))
134#define Z_LOG_LEVEL_CHECK(_level, _check_level, _default_level) \
135 ((_level) <= Z_LOG_RESOLVED_LEVEL(_check_level, _default_level))
146#define Z_LOG_CONST_LEVEL_CHECK(_level) \
147 (IS_ENABLED(CONFIG_LOG) && \
148 (Z_LOG_LEVEL_CHECK(_level, CONFIG_LOG_OVERRIDE_LEVEL, LOG_LEVEL_NONE) \
150 ((IS_ENABLED(CONFIG_LOG_OVERRIDE_LEVEL) == false) && \
151 ((_level) <= __log_level) && \
152 ((_level) <= CONFIG_LOG_MAX_LEVEL) \
170#define Z_LOG_STATIC_INST_LEVEL_CHECK(_level, _inst, _source) \
171 (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) || !_inst || \
172 (_level <= ((const struct log_source_const_data *)_source)->level))
184#define Z_LOG_DYNAMIC_LEVEL_CHECK(_level, _source) \
185 (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) || k_is_user_context() || \
186 ((_level) <= Z_LOG_RUNTIME_FILTER(((struct log_source_dynamic_data *)_source)->filters)))
199#define Z_LOG_LEVEL_ALL_CHECK(_level, _inst, _source) \
200 (Z_LOG_CONST_LEVEL_CHECK(_level) && \
201 Z_LOG_STATIC_INST_LEVEL_CHECK(_level, _inst, _source) && \
202 Z_LOG_DYNAMIC_LEVEL_CHECK(_level, _source))
209#define Z_LOG_CURRENT_DATA() \
210 COND_CODE_1(CONFIG_LOG_RUNTIME_FILTERING, \
211 (__log_current_dynamic_data), (__log_current_const_data))
216void z_log_minimal_hexdump_print(
int level,
const void *data,
size_t size);
217void z_log_minimal_vprintk(
const char *fmt, va_list ap);
218void z_log_minimal_printk(
const char *fmt, ...);
220#define Z_LOG_TO_PRINTK(_level, fmt, ...) do { \
221 z_log_minimal_printk("%c: " fmt "\n", \
222 z_log_minimal_level_to_char(_level), \
226#define Z_LOG_TO_VPRINTK(_level, fmt, valist) do { \
227 z_log_minimal_printk("%c: ", z_log_minimal_level_to_char(_level)); \
228 z_log_minimal_vprintk(fmt, valist); \
229 z_log_minimal_printk("\n"); \
232static inline char z_log_minimal_level_to_char(
int level)
248#define Z_LOG_INST(_inst) COND_CODE_1(CONFIG_LOG, (_inst), NULL)
257#ifdef CONFIG_LOG_FMT_STRING_VALIDATE
258#define LOG_STRING_WARNING(_mode, _src, ...) \
259 Z_LOG_MSG_CREATE(UTIL_NOT(IS_ENABLED(CONFIG_USERSPACE)), _mode, \
260 Z_LOG_LOCAL_DOMAIN_ID, _src, LOG_LEVEL_ERR, NULL, 0, \
261 "char pointer used for %%p, cast to void *:\"%s\"", \
262 GET_ARG_N(1, __VA_ARGS__))
264#define LOG_POINTERS_VALIDATE(string_ok, ...) \
265 _Pragma("GCC diagnostic push") \
266 _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"") \
267 string_ok = Z_CBPRINTF_POINTERS_VALIDATE(__VA_ARGS__); \
268 _Pragma("GCC diagnostic pop")
270#define LOG_POINTERS_VALIDATE(string_ok, ...) string_ok = true
271#define LOG_STRING_WARNING(_mode, _src, ...)
292#define Z_LOG2(_level, _inst, _source, ...) \
294 if (!Z_LOG_LEVEL_ALL_CHECK(_level, _inst, _source)) { \
297 if (IS_ENABLED(CONFIG_LOG_MODE_MINIMAL)) { \
298 Z_LOG_TO_PRINTK(_level, __VA_ARGS__); \
303 LOG_POINTERS_VALIDATE(string_ok, __VA_ARGS__); \
305 LOG_STRING_WARNING(_mode, _source, __VA_ARGS__); \
308 Z_LOG_MSG_CREATE(UTIL_NOT(IS_ENABLED(CONFIG_USERSPACE)), _mode, \
309 Z_LOG_LOCAL_DOMAIN_ID, _source, _level, NULL, 0, __VA_ARGS__); \
315 z_log_printf_arg_checker(__VA_ARGS__); \
319#define Z_LOG(_level, ...) Z_LOG2(_level, 0, Z_LOG_CURRENT_DATA(), __VA_ARGS__)
320#define Z_LOG_INSTANCE(_level, _inst, ...) Z_LOG2(_level, 1, Z_LOG_INST(_inst), __VA_ARGS__)
345#define Z_LOG_HEXDUMP2(_level, _inst, _source, _data, _len, ...) \
347 if (!Z_LOG_LEVEL_ALL_CHECK(_level, _inst, _source)) { \
350 const char *_str = GET_ARG_N(1, __VA_ARGS__); \
351 if (IS_ENABLED(CONFIG_LOG_MODE_MINIMAL)) { \
352 Z_LOG_TO_PRINTK(_level, "%s", _str); \
353 z_log_minimal_hexdump_print((_level), (const char *)(_data), (_len)); \
357 Z_LOG_MSG_CREATE(UTIL_NOT(IS_ENABLED(CONFIG_USERSPACE)), mode, \
358 Z_LOG_LOCAL_DOMAIN_ID, _source, _level, _data, _len, \
359 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
361 (COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
362 ("%s", __VA_ARGS__), (__VA_ARGS__))))); \
365#define Z_LOG_HEXDUMP(_level, _data, _length, ...) \
366 Z_LOG_HEXDUMP2(_level, 0, Z_LOG_CURRENT_DATA(), _data, _length, __VA_ARGS__)
368#define Z_LOG_HEXDUMP_INSTANCE(_level, _inst, _data, _length, ...) \
369 Z_LOG_HEXDUMP2(_level, 1, Z_LOG_INST(_inst), _data, _length, __VA_ARGS__)
376#define LOG_LEVEL_BITS 3U
379#define LOG_FILTER_SLOT_SIZE LOG_LEVEL_BITS
382#define LOG_FILTERS_NUM_OF_SLOTS (32 / LOG_FILTER_SLOT_SIZE)
385#define LOG_FILTERS_MAX_BACKENDS \
386 (LOG_FILTERS_NUM_OF_SLOTS - (1 + IS_ENABLED(CONFIG_LOG_FRONTEND)))
389#define LOG_FRONTEND_SLOT_ID (LOG_FILTERS_NUM_OF_SLOTS - 1)
392#define LOG_FILTER_SLOT_MASK (BIT(LOG_FILTER_SLOT_SIZE) - 1U)
398#define LOG_FILTER_SLOT_SHIFT(_id) (LOG_FILTER_SLOT_SIZE * (_id))
400#define LOG_FILTER_SLOT_GET(_filters, _id) \
401 ((*(_filters) >> LOG_FILTER_SLOT_SHIFT(_id)) & LOG_FILTER_SLOT_MASK)
403#define LOG_FILTER_SLOT_SET(_filters, _id, _filter) \
405 *(_filters) &= ~(LOG_FILTER_SLOT_MASK << \
406 LOG_FILTER_SLOT_SHIFT(_id)); \
407 *(_filters) |= ((_filter) & LOG_FILTER_SLOT_MASK) << \
408 LOG_FILTER_SLOT_SHIFT(_id); \
411#define LOG_FILTER_AGGR_SLOT_IDX 0
413#define LOG_FILTER_AGGR_SLOT_GET(_filters) \
414 LOG_FILTER_SLOT_GET(_filters, LOG_FILTER_AGGR_SLOT_IDX)
416#define LOG_FILTER_FIRST_BACKEND_SLOT_IDX 1
422#define Z_LOG_RUNTIME_FILTER(_filter) \
423 LOG_FILTER_SLOT_GET(&(_filter), LOG_FILTER_AGGR_SLOT_IDX)
428#define LOG_LEVEL_INTERNAL_RAW_STRING LOG_LEVEL_NONE
443#define Z_LOG_PRINTK(_is_raw, ...) do { \
444 if (!IS_ENABLED(CONFIG_LOG)) { \
447 if (IS_ENABLED(CONFIG_LOG_MODE_MINIMAL)) { \
448 z_log_minimal_printk(__VA_ARGS__); \
453 z_log_printf_arg_checker(__VA_ARGS__); \
455 Z_LOG_MSG_CREATE(!IS_ENABLED(CONFIG_USERSPACE), _mode, \
456 Z_LOG_LOCAL_DOMAIN_ID, (const void *)(uintptr_t)_is_raw, \
457 LOG_LEVEL_INTERNAL_RAW_STRING, NULL, 0, __VA_ARGS__);\
481#define LOG_ITEM_DYNAMIC_DATA(_name) UTIL_CAT(log_dynamic_, _name)
483#define LOG_INSTANCE_DYNAMIC_DATA(_module_name, _inst) \
484 LOG_ITEM_DYNAMIC_DATA(Z_LOG_INSTANCE_FULL_NAME(_module_name, _inst))
508 return IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ?
514static inline __printf_like(1, 2)
515void z_log_printf_arg_checker(const
char *fmt, ...)
531 z_log_msg_runtime_vcreate(Z_LOG_LOCAL_DOMAIN_ID, NULL, level,
532 NULL, 0, 0, fmt, ap);
#define TYPE_SECTION_START_EXTERN(type, secname)
iterable section extern for start symbol for a generic type
Definition iterable_sections.h:78
#define TYPE_SECTION_START(secname)
iterable section start symbol for a generic type
Definition iterable_sections.h:55
#define TYPE_SECTION_END_EXTERN(type, secname)
iterable section extern for end symbol for a generic type
Definition iterable_sections.h:92
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:140
static uint32_t log_source_id(const void *source)
Get index of the log source based on the address of the associated data.
Definition log_core.h:506
#define LOG_LEVEL_INF
Definition log_core.h:22
#define LOG_LEVEL_WRN
Definition log_core.h:21
static uint32_t log_dynamic_source_id(struct log_source_dynamic_data *data)
Get index of the log source based on the address of the dynamic data associated with the source.
Definition log_core.h:493
#define LOG_LEVEL_ERR
Definition log_core.h:20
static void log_generic(uint8_t level, const char *fmt, va_list ap)
Write a generic log message.
Definition log_core.h:529
#define LOG_LEVEL_DBG
Definition log_core.h:23
static uint32_t log_const_source_id(const struct log_source_const_data *data)
Get index of the log source based on the address of the constant data associated with the source.
Definition log_core.h:467
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Constant data associated with the source of log messages.
Definition log_instance.h:17
Dynamic data associated with the source of log messages.
Definition log_instance.h:30