1 /*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #ifndef ZEPHYR_INCLUDE_LOGGING_LOG_CORE_H_
7 #define ZEPHYR_INCLUDE_LOGGING_LOG_CORE_H_
8
9 #include <zephyr/logging/log_msg.h>
10 #include <zephyr/logging/log_instance.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stdarg.h>
14 #include <zephyr/sys/util.h>
15
16 /* This header file keeps all macros and functions needed for creating logging
17 * messages (macros like @ref LOG_ERR).
18 */
19 #define LOG_LEVEL_NONE 0U
20 #define LOG_LEVEL_ERR 1U
21 #define LOG_LEVEL_WRN 2U
22 #define LOG_LEVEL_INF 3U
23 #define LOG_LEVEL_DBG 4U
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #ifndef CONFIG_LOG
30 #define CONFIG_LOG_DEFAULT_LEVEL 0U
31 #define CONFIG_LOG_MAX_LEVEL 0U
32 #endif
33
34 /* Id of local domain. */
35 #define Z_LOG_LOCAL_DOMAIN_ID 0
36
37 #define LOG_FUNCTION_PREFIX_MASK \
38 (((uint32_t)IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_ERR) << \
39 LOG_LEVEL_ERR) | \
40 ((uint32_t)IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_WRN) << \
41 LOG_LEVEL_WRN) | \
42 ((uint32_t)IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_INF) << \
43 LOG_LEVEL_INF) | \
44 ((uint32_t)IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_DBG) << LOG_LEVEL_DBG))
45
46 /** @brief Macro for returning local level value if defined or default.
47 *
48 * Check @ref IS_ENABLED macro for detailed explanation of the trick.
49 */
50 #define Z_LOG_RESOLVED_LEVEL(_level, _default) \
51 Z_LOG_RESOLVED_LEVEL1(_level, _default)
52
53 #define Z_LOG_RESOLVED_LEVEL1(_level, _default) \
54 __COND_CODE(_LOG_XXXX##_level, (_level), (_default))
55
56 #define _LOG_XXXX0 _LOG_YYYY,
57 #define _LOG_XXXX0U _LOG_YYYY,
58 #define _LOG_XXXX1 _LOG_YYYY,
59 #define _LOG_XXXX1U _LOG_YYYY,
60 #define _LOG_XXXX2 _LOG_YYYY,
61 #define _LOG_XXXX2U _LOG_YYYY,
62 #define _LOG_XXXX3 _LOG_YYYY,
63 #define _LOG_XXXX3U _LOG_YYYY,
64 #define _LOG_XXXX4 _LOG_YYYY,
65 #define _LOG_XXXX4U _LOG_YYYY,
66
67 /**
68 * @brief Macro for conditional code generation if provided log level allows.
69 *
70 * Macro behaves similarly to standard \#if \#else \#endif clause. The
71 * difference is that it is evaluated when used and not when header file is
72 * included.
73 *
74 * @param _eval_level Evaluated level. If level evaluates to one of existing log
75 * log level (1-4) then macro evaluates to _iftrue.
76 * @param _iftrue Code that should be inserted when evaluated to true. Note,
77 * that parameter must be provided in brackets.
78 * @param _iffalse Code that should be inserted when evaluated to false.
79 * Note, that parameter must be provided in brackets.
80 */
81 #define Z_LOG_EVAL(_eval_level, _iftrue, _iffalse) \
82 Z_LOG_EVAL1(_eval_level, _iftrue, _iffalse)
83
84 #define Z_LOG_EVAL1(_eval_level, _iftrue, _iffalse) \
85 __COND_CODE(_LOG_ZZZZ##_eval_level, _iftrue, _iffalse)
86
87 #define _LOG_ZZZZ0 _LOG_YYYY,
88 #define _LOG_ZZZZ0U _LOG_YYYY,
89 #define _LOG_ZZZZ1 _LOG_YYYY,
90 #define _LOG_ZZZZ1U _LOG_YYYY,
91 #define _LOG_ZZZZ2 _LOG_YYYY,
92 #define _LOG_ZZZZ2U _LOG_YYYY,
93 #define _LOG_ZZZZ3 _LOG_YYYY,
94 #define _LOG_ZZZZ3U _LOG_YYYY,
95 #define _LOG_ZZZZ4 _LOG_YYYY,
96 #define _LOG_ZZZZ4U _LOG_YYYY,
97
98 /**
99 *
100 * @brief Macro for getting ID of current module.
101 */
102 #define LOG_CURRENT_MODULE_ID() (__log_level != 0 ? \
103 log_const_source_id(__log_current_const_data) : 0U)
104
105 /* Set of defines that are set to 1 if function name prefix is enabled for given level. */
106 #define Z_LOG_FUNC_PREFIX_0U 0
107 #define Z_LOG_FUNC_PREFIX_1U COND_CODE_1(CONFIG_LOG_FUNC_NAME_PREFIX_ERR, (1), (0))
108 #define Z_LOG_FUNC_PREFIX_2U COND_CODE_1(CONFIG_LOG_FUNC_NAME_PREFIX_WRN, (1), (0))
109 #define Z_LOG_FUNC_PREFIX_3U COND_CODE_1(CONFIG_LOG_FUNC_NAME_PREFIX_INF, (1), (0))
110 #define Z_LOG_FUNC_PREFIX_4U COND_CODE_1(CONFIG_LOG_FUNC_NAME_PREFIX_DBG, (1), (0))
111
112 /**
113 * @brief Macro for optional injection of function name as first argument of
114 * formatted string. COND_CODE_0() macro is used to handle no arguments
115 * case.
116 *
117 * The purpose of this macro is to prefix string literal with format specifier
118 * for function name and inject function name as first argument. In order to
119 * handle string with no arguments _LOG_Z_EVAL is used.
120 */
121 #define Z_LOG_STR_WITH_PREFIX2(...) \
122 "%s: " GET_ARG_N(1, __VA_ARGS__), (const char *)__func__\
123 COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__),\
124 (),\
125 (, GET_ARGS_LESS_N(1, __VA_ARGS__))\
126 )
127
128 /* Macro handles case when no format string is provided: e.g. LOG_DBG().
129 * Handling of format string is deferred to the next level macro.
130 */
131 #define Z_LOG_STR_WITH_PREFIX(...) \
132 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
133 ("%s", (const char *)__func__), \
134 (Z_LOG_STR_WITH_PREFIX2(__VA_ARGS__)))
135
136 /**
137 * @brief Handle optional injection of function name as the first argument.
138 *
139 * Additionally, macro is handling the empty message case.
140 */
141 #define Z_LOG_STR(_level, ...) \
142 COND_CODE_1(UTIL_CAT(Z_LOG_FUNC_PREFIX_##_level), \
143 (Z_LOG_STR_WITH_PREFIX(__VA_ARGS__)), (__VA_ARGS__))
144
145 #define Z_LOG_LEVEL_CHECK(_level, _check_level, _default_level) \
146 (_level <= Z_LOG_RESOLVED_LEVEL(_check_level, _default_level))
147
148 #define Z_LOG_CONST_LEVEL_CHECK(_level) \
149 (IS_ENABLED(CONFIG_LOG) && \
150 (Z_LOG_LEVEL_CHECK(_level, CONFIG_LOG_OVERRIDE_LEVEL, LOG_LEVEL_NONE) \
151 || \
152 ((IS_ENABLED(CONFIG_LOG_OVERRIDE_LEVEL) == false) && \
153 (_level <= __log_level) && \
154 (_level <= CONFIG_LOG_MAX_LEVEL) \
155 ) \
156 ))
157
158 /*****************************************************************************/
159 /****************** Definitions used by minimal logging *********************/
160 /*****************************************************************************/
161 void z_log_minimal_hexdump_print(int level, const void *data, size_t size);
162 void z_log_minimal_vprintk(const char *fmt, va_list ap);
163 void z_log_minimal_printk(const char *fmt, ...);
164
165 #define Z_LOG_TO_PRINTK(_level, fmt, ...) do { \
166 z_log_minimal_printk("%c: " fmt "\n", \
167 z_log_minimal_level_to_char(_level), \
168 ##__VA_ARGS__); \
169 } while (false)
170
171 #define Z_LOG_TO_VPRINTK(_level, fmt, valist) do { \
172 z_log_minimal_printk("%c: ", z_log_minimal_level_to_char(_level)); \
173 z_log_minimal_vprintk(fmt, valist); \
174 z_log_minimal_printk("\n"); \
175 } while (false)
176
z_log_minimal_level_to_char(int level)177 static inline char z_log_minimal_level_to_char(int level)
178 {
179 switch (level) {
180 case LOG_LEVEL_ERR:
181 return 'E';
182 case LOG_LEVEL_WRN:
183 return 'W';
184 case LOG_LEVEL_INF:
185 return 'I';
186 case LOG_LEVEL_DBG:
187 return 'D';
188 default:
189 return '?';
190 }
191 }
192
193 #define Z_LOG_INST(_inst) COND_CODE_1(CONFIG_LOG, (_inst), NULL)
194
195 /*****************************************************************************/
196 /****************** Macros for standard logging ******************************/
197 /*****************************************************************************/
198 /** @internal
199 * @brief Generic logging macro.
200 *
201 * It checks against static levels (resolved at compile timer), runtime levels
202 * and modes and dispatch to relevant processing path.
203 *
204 * @param _level Log message severity level.
205 *
206 * @param _inst Set to 1 for instance specific log message. 0 otherwise.
207 *
208 * @param _source Pointer to static source descriptor object. NULL when runtime filtering
209 * is enabled.
210 *
211 * @param _dsource Pointer to dynamic source descriptor. NULL when runtime filtering
212 * is disabled.
213 *
214 * @param ... String with arguments.
215 */
216 #define Z_LOG2(_level, _inst, _source, _dsource, ...) do { \
217 if (!Z_LOG_CONST_LEVEL_CHECK(_level)) { \
218 break; \
219 } \
220 if (IS_ENABLED(CONFIG_LOG_MODE_MINIMAL)) { \
221 Z_LOG_TO_PRINTK(_level, __VA_ARGS__); \
222 break; \
223 } \
224 /* For instance logging check instance specific static level */ \
225 if (_inst != 0 && !IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) { \
226 if (_level > ((struct log_source_const_data *)_source)->level) { \
227 break; \
228 } \
229 } \
230 \
231 bool is_user_context = k_is_user_context(); \
232 if (!IS_ENABLED(CONFIG_LOG_FRONTEND) && IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) && \
233 !is_user_context && _level > Z_LOG_RUNTIME_FILTER((_dsource)->filters)) { \
234 break; \
235 } \
236 int _mode; \
237 void *_src = IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ? \
238 (void *)_dsource : (void *)_source; \
239 Z_LOG_MSG_CREATE(UTIL_NOT(IS_ENABLED(CONFIG_USERSPACE)), _mode, \
240 Z_LOG_LOCAL_DOMAIN_ID, _src, _level, NULL,\
241 0, __VA_ARGS__); \
242 (void)_mode; \
243 if (false) { \
244 /* Arguments checker present but never evaluated.*/ \
245 /* Placed here to ensure that __VA_ARGS__ are*/ \
246 /* evaluated once when log is enabled.*/ \
247 z_log_printf_arg_checker(__VA_ARGS__); \
248 } \
249 } while (false)
250
251 #define Z_LOG(_level, ...) \
252 Z_LOG2(_level, 0, __log_current_const_data, __log_current_dynamic_data, __VA_ARGS__)
253
254 #define Z_LOG_INSTANCE(_level, _inst, ...) \
255 Z_LOG2(_level, 1, \
256 COND_CODE_1(CONFIG_LOG_RUNTIME_FILTERING, (NULL), (Z_LOG_INST(_inst))), \
257 (struct log_source_dynamic_data *)COND_CODE_1( \
258 CONFIG_LOG_RUNTIME_FILTERING, \
259 (Z_LOG_INST(_inst)), (NULL)), \
260 __VA_ARGS__)
261
262 /*****************************************************************************/
263 /****************** Macros for hexdump logging *******************************/
264 /*****************************************************************************/
265 /** @internal
266 * @brief Generic logging macro.
267 *
268 * It checks against static levels (resolved at compile timer), runtime levels
269 * and modes and dispatch to relevant processing path.
270 *
271 * @param _level Log message severity level.
272 *
273 * @param _inst Set to 1 for instance specific log message. 0 otherwise.
274 *
275 * @param _source Pointer to static source descriptor object. NULL when runtime filtering
276 * is enabled.
277 *
278 * @param _dsource Pointer to dynamic source descriptor. NULL when runtime filtering
279 * is disabled.
280 *
281 * @param _data Hexdump data;
282 *
283 * @param _len Hexdump data length.
284 *
285 * @param ... String.
286 */
287 #define Z_LOG_HEXDUMP2(_level, _inst, _source, _dsource, _data, _len, ...) do { \
288 const char *_str = GET_ARG_N(1, __VA_ARGS__); \
289 if (!Z_LOG_CONST_LEVEL_CHECK(_level)) { \
290 break; \
291 } \
292 /* For instance logging check instance specific static level */ \
293 if (_inst & !IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) { \
294 if (_level > ((struct log_source_const_data *)_source)->level) { \
295 break; \
296 } \
297 } \
298 bool is_user_context = k_is_user_context(); \
299 uint32_t filters = IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ? \
300 (_dsource)->filters : 0;\
301 \
302 if (IS_ENABLED(CONFIG_LOG_MODE_MINIMAL)) { \
303 Z_LOG_TO_PRINTK(_level, "%s", _str); \
304 z_log_minimal_hexdump_print(_level, \
305 (const char *)_data, _len);\
306 break; \
307 } \
308 if (!IS_ENABLED(CONFIG_LOG_FRONTEND) && IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) && \
309 !is_user_context && _level > Z_LOG_RUNTIME_FILTER(filters)) { \
310 break; \
311 } \
312 int mode; \
313 void *_src = IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ? \
314 (void *)_dsource : (void *)_source; \
315 Z_LOG_MSG_CREATE(UTIL_NOT(IS_ENABLED(CONFIG_USERSPACE)), mode, \
316 Z_LOG_LOCAL_DOMAIN_ID, _src, _level, \
317 _data, _len, \
318 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
319 (), \
320 (COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
321 ("%s", __VA_ARGS__), (__VA_ARGS__)))));\
322 } while (false)
323
324 #define Z_LOG_HEXDUMP(_level, _data, _length, ...) \
325 Z_LOG_HEXDUMP2(_level, 0, \
326 __log_current_const_data, \
327 __log_current_dynamic_data, \
328 _data, _length, __VA_ARGS__)
329
330 #define Z_LOG_HEXDUMP_INSTANCE(_level, _inst, _data, _length, _str) \
331 Z_LOG_HEXDUMP2(_level, 1, \
332 COND_CODE_1(CONFIG_LOG_RUNTIME_FILTERING, (NULL), (Z_LOG_INST(_inst))), \
333 (struct log_source_dynamic_data *)COND_CODE_1( \
334 CONFIG_LOG_RUNTIME_FILTERING, \
335 (Z_LOG_INST(_inst)), (NULL)), \
336 _data, _length, _str)
337
338 /*****************************************************************************/
339 /****************** Filtering macros *****************************************/
340 /*****************************************************************************/
341
342 /** @brief Number of bits used to encode log level. */
343 #define LOG_LEVEL_BITS 3U
344
345 /** @brief Filter slot size. */
346 #define LOG_FILTER_SLOT_SIZE LOG_LEVEL_BITS
347
348 /** @brief Number of slots in one word. */
349 #define LOG_FILTERS_NUM_OF_SLOTS (32 / LOG_FILTER_SLOT_SIZE)
350
351 /** @brief Slot mask. */
352 #define LOG_FILTER_SLOT_MASK (BIT(LOG_FILTER_SLOT_SIZE) - 1U)
353
354 /** @brief Bit offset of a slot.
355 *
356 * @param _id Slot ID.
357 */
358 #define LOG_FILTER_SLOT_SHIFT(_id) (LOG_FILTER_SLOT_SIZE * (_id))
359
360 #define LOG_FILTER_SLOT_GET(_filters, _id) \
361 ((*(_filters) >> LOG_FILTER_SLOT_SHIFT(_id)) & LOG_FILTER_SLOT_MASK)
362
363 #define LOG_FILTER_SLOT_SET(_filters, _id, _filter) \
364 do { \
365 *(_filters) &= ~(LOG_FILTER_SLOT_MASK << \
366 LOG_FILTER_SLOT_SHIFT(_id)); \
367 *(_filters) |= ((_filter) & LOG_FILTER_SLOT_MASK) << \
368 LOG_FILTER_SLOT_SHIFT(_id); \
369 } while (false)
370
371 #define LOG_FILTER_AGGR_SLOT_IDX 0
372
373 #define LOG_FILTER_AGGR_SLOT_GET(_filters) \
374 LOG_FILTER_SLOT_GET(_filters, LOG_FILTER_AGGR_SLOT_IDX)
375
376 #define LOG_FILTER_FIRST_BACKEND_SLOT_IDX 1
377
378 /* Return aggregated (highest) level for all enabled backends, e.g. if there
379 * are 3 active backends, one backend is set to get INF logs from a module and
380 * two other backends are set for ERR, returned level is INF.
381 */
382 #define Z_LOG_RUNTIME_FILTER(_filter) \
383 LOG_FILTER_SLOT_GET(&_filter, LOG_FILTER_AGGR_SLOT_IDX)
384
385 /** @brief Log level value used to indicate log entry that should not be
386 * formatted (raw string).
387 */
388 #define LOG_LEVEL_INTERNAL_RAW_STRING LOG_LEVEL_NONE
389
390 TYPE_SECTION_START_EXTERN(struct log_source_const_data, log_const);
391 TYPE_SECTION_END_EXTERN(struct log_source_const_data, log_const);
392
393 /** @brief Create message for logging printk-like string or a raw string.
394 *
395 * Part of printk string processing is appending of carriage return after any
396 * new line character found in the string. If it is not desirable then @p _is_raw
397 * can be set to 1 to indicate raw string. This information is stored in the source
398 * field which is not used for its typical purpose in this case.
399 *
400 * @param _is_raw Set to 1 to indicate raw string, set to 0 to indicate printk.
401 * @param ... Format string with arguments.
402 */
403 #define Z_LOG_PRINTK(_is_raw, ...) do { \
404 if (!IS_ENABLED(CONFIG_LOG)) { \
405 break; \
406 } \
407 if (IS_ENABLED(CONFIG_LOG_MODE_MINIMAL)) { \
408 z_log_minimal_printk(__VA_ARGS__); \
409 break; \
410 } \
411 int _mode; \
412 if (0) {\
413 z_log_printf_arg_checker(__VA_ARGS__); \
414 } \
415 Z_LOG_MSG_CREATE(!IS_ENABLED(CONFIG_USERSPACE), _mode, \
416 Z_LOG_LOCAL_DOMAIN_ID, (uintptr_t)_is_raw, \
417 LOG_LEVEL_INTERNAL_RAW_STRING, NULL, 0, __VA_ARGS__);\
418 } while (0)
419
420 /** @brief Get index of the log source based on the address of the constant data
421 * associated with the source.
422 *
423 * @param data Address of the constant data.
424 *
425 * @return Source ID.
426 */
log_const_source_id(const struct log_source_const_data * data)427 static inline uint32_t log_const_source_id(
428 const struct log_source_const_data *data)
429 {
430 return ((const uint8_t *)data - (uint8_t *)TYPE_SECTION_START(log_const))/
431 sizeof(struct log_source_const_data);
432 }
433
434 TYPE_SECTION_START_EXTERN(struct log_source_dynamic_data, log_dynamic);
435 TYPE_SECTION_END_EXTERN(struct log_source_dynamic_data, log_dynamic);
436
437 /** @brief Creates name of variable and section for runtime log data.
438 *
439 * @param _name Name.
440 */
441 #define LOG_ITEM_DYNAMIC_DATA(_name) UTIL_CAT(log_dynamic_, _name)
442
443 #define LOG_INSTANCE_DYNAMIC_DATA(_module_name, _inst) \
444 LOG_ITEM_DYNAMIC_DATA(Z_LOG_INSTANCE_FULL_NAME(_module_name, _inst))
445
446 /** @brief Get index of the log source based on the address of the dynamic data
447 * associated with the source.
448 *
449 * @param data Address of the dynamic data.
450 *
451 * @return Source ID.
452 */
log_dynamic_source_id(struct log_source_dynamic_data * data)453 static inline uint32_t log_dynamic_source_id(struct log_source_dynamic_data *data)
454 {
455 return ((uint8_t *)data - (uint8_t *)TYPE_SECTION_START(log_dynamic))/
456 sizeof(struct log_source_dynamic_data);
457 }
458
459 /** @brief Dummy function to trigger log messages arguments type checking. */
460 static inline __printf_like(1, 2)
z_log_printf_arg_checker(const char * fmt,...)461 void z_log_printf_arg_checker(const char *fmt, ...)
462 {
463 ARG_UNUSED(fmt);
464 }
465
466 /**
467 * @brief Writes a generic log message to the logging v2.
468 *
469 * @note This function is intended to be used when porting other log systems.
470 *
471 * @param level Log level..
472 * @param fmt String to format.
473 * @param ap Pointer to arguments list.
474 */
log2_generic(uint8_t level,const char * fmt,va_list ap)475 static inline void log2_generic(uint8_t level, const char *fmt, va_list ap)
476 {
477 z_log_msg_runtime_vcreate(Z_LOG_LOCAL_DOMAIN_ID, NULL, level,
478 NULL, 0, 0, fmt, ap);
479 }
480
481 #ifdef __cplusplus
482 }
483 #endif
484
485 #endif /* ZEPHYR_INCLUDE_LOGGING_LOG_CORE_H_ */
486