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 <logging/log_msg.h>
10 #include <logging/log_core2.h>
11 #include <logging/log_instance.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <stdarg.h>
15 #include <syscall.h>
16 #include <sys/util.h>
17
18 #define LOG_LEVEL_NONE 0U
19 #define LOG_LEVEL_ERR 1U
20 #define LOG_LEVEL_WRN 2U
21 #define LOG_LEVEL_INF 3U
22 #define LOG_LEVEL_DBG 4U
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 #ifndef CONFIG_LOG
29 #define CONFIG_LOG_DEFAULT_LEVEL 0U
30 #define CONFIG_LOG_MAX_LEVEL 0U
31 #endif
32
33 #if !defined(CONFIG_LOG) || defined(CONFIG_LOG_MINIMAL)
34 #define CONFIG_LOG_DOMAIN_ID 0U
35 #endif
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_ZZZZ1 _LOG_YYYY,
88 #define _LOG_ZZZZ1U _LOG_YYYY,
89 #define _LOG_ZZZZ2 _LOG_YYYY,
90 #define _LOG_ZZZZ2U _LOG_YYYY,
91 #define _LOG_ZZZZ3 _LOG_YYYY,
92 #define _LOG_ZZZZ3U _LOG_YYYY,
93 #define _LOG_ZZZZ4 _LOG_YYYY,
94 #define _LOG_ZZZZ4U _LOG_YYYY,
95
96 /** @brief Macro for getting log level for given module.
97 *
98 * It is evaluated to LOG_LEVEL if defined. Otherwise CONFIG_LOG_DEFAULT_LEVEL
99 * is used.
100 */
101 #define _LOG_LEVEL() Z_LOG_RESOLVED_LEVEL(LOG_LEVEL, CONFIG_LOG_DEFAULT_LEVEL)
102
103 /**
104 * @def LOG_CONST_ID_GET
105 * @brief Macro for getting ID of the element of the section.
106 *
107 * @param _addr Address of the element.
108 */
109 #define LOG_CONST_ID_GET(_addr) \
110 COND_CODE_1(CONFIG_LOG, ((__log_level ? log_const_source_id(_addr) : 0)), (0))
111
112 /**
113 * @def LOG_CURRENT_MODULE_ID
114 * @brief Macro for getting ID of current module.
115 */
116 #define LOG_CURRENT_MODULE_ID() (__log_level != 0 ? \
117 log_const_source_id(__log_current_const_data) : 0U)
118
119 /**
120 * @def LOG_CURRENT_DYNAMIC_DATA_ADDR
121 * @brief Macro for getting address of dynamic structure of current module.
122 */
123 #define LOG_CURRENT_DYNAMIC_DATA_ADDR() (__log_level ? \
124 __log_current_dynamic_data : (struct log_source_dynamic_data *)0U)
125
126 /** @brief Macro for getting ID of the element of the section.
127 *
128 * @param _addr Address of the element.
129 */
130 #define LOG_DYNAMIC_ID_GET(_addr) \
131 COND_CODE_1(CONFIG_LOG, ((__log_level ? log_dynamic_source_id(_addr) : 0)), (0))
132
133 /* Set of defines that are set to 1 if function name prefix is enabled for given level. */
134 #define Z_LOG_FUNC_PREFIX_0U 0
135 #define Z_LOG_FUNC_PREFIX_1U COND_CODE_1(CONFIG_LOG_FUNC_NAME_PREFIX_ERR, (1), (0))
136 #define Z_LOG_FUNC_PREFIX_2U COND_CODE_1(CONFIG_LOG_FUNC_NAME_PREFIX_WRN, (1), (0))
137 #define Z_LOG_FUNC_PREFIX_3U COND_CODE_1(CONFIG_LOG_FUNC_NAME_PREFIX_INF, (1), (0))
138 #define Z_LOG_FUNC_PREFIX_4U COND_CODE_1(CONFIG_LOG_FUNC_NAME_PREFIX_DBG, (1), (0))
139
140 /**
141 * @brief Macro for optional injection of function name as first argument of
142 * formatted string. COND_CODE_0() macro is used to handle no arguments
143 * case.
144 *
145 * The purpose of this macro is to prefix string literal with format specifier
146 * for function name and inject function name as first argument. In order to
147 * handle string with no arguments _LOG_Z_EVAL is used.
148 */
149 #define Z_LOG_STR_WITH_PREFIX2(...) \
150 "%s: " GET_ARG_N(1, __VA_ARGS__), (const char *)__func__\
151 COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__),\
152 (),\
153 (, GET_ARGS_LESS_N(1, __VA_ARGS__))\
154 )
155
156 /* Macro handles case when no format string is provided: e.g. LOG_DBG().
157 * Handling of format string is deferred to the next level macro.
158 */
159 #define Z_LOG_STR_WITH_PREFIX(...) \
160 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
161 ("%s", (const char *)__func__), \
162 (Z_LOG_STR_WITH_PREFIX2(__VA_ARGS__)))
163
164 /**
165 * @brief Handle optional injection of function name as the first argument.
166 *
167 * Additionally, macro is handling the empty message case.
168 */
169 #define Z_LOG_STR(_level, ...) \
170 COND_CODE_1(UTIL_CAT(Z_LOG_FUNC_PREFIX_##_level), \
171 (Z_LOG_STR_WITH_PREFIX(__VA_ARGS__)), (__VA_ARGS__))
172
173 /******************************************************************************/
174 /****************** Internal macros for log frontend **************************/
175 /******************************************************************************/
176 /**@brief Second stage for Z_LOG_NARGS_POSTFIX */
177 #define Z_LOG_NARGS_POSTFIX_IMPL( \
178 _ignored, \
179 _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
180 _11, _12, _13, _14, N, ...) N
181
182 /**@brief Macro to get the postfix for further log message processing.
183 *
184 * Logs with more than 3 arguments are processed in a generic way.
185 *
186 * param[in] ... List of arguments
187 *
188 * @retval Postfix, number of arguments or _LONG when more than 3 arguments.
189 */
190 #define Z_LOG_NARGS_POSTFIX(...) \
191 Z_LOG_NARGS_POSTFIX_IMPL(__VA_ARGS__, LONG, LONG, LONG, LONG, LONG, \
192 LONG, LONG, LONG, LONG, LONG, LONG, LONG, 3, 2, 1, 0, ~)
193
194 #define Z_LOG_INTERNAL_X(N, ...) UTIL_CAT(_LOG_INTERNAL_, N)(__VA_ARGS__)
195
196 #define Z_LOG_INTERNAL2(is_user_context, _src_level, ...) do { \
197 if (is_user_context) { \
198 log_from_user(_src_level, __VA_ARGS__); \
199 } else if (IS_ENABLED(CONFIG_LOG_IMMEDIATE)) { \
200 log_string_sync(_src_level, __VA_ARGS__); \
201 } else { \
202 Z_LOG_INTERNAL_X(Z_LOG_NARGS_POSTFIX(__VA_ARGS__), \
203 _src_level, __VA_ARGS__); \
204 } \
205 } while (false)
206
207 #define Z_LOG_INTERNAL(is_user_context, _level, _source, _dsource, ...) do { \
208 uint16_t src_id = \
209 IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ? \
210 LOG_DYNAMIC_ID_GET(_dsource) : LOG_CONST_ID_GET(_source); \
211 struct log_msg_ids src_level = { \
212 .level = _level, \
213 .domain_id = CONFIG_LOG_DOMAIN_ID, \
214 .source_id = src_id \
215 }; \
216 Z_LOG_INTERNAL2(is_user_context, src_level, \
217 Z_LOG_STR(_level, __VA_ARGS__)); \
218 } while (0)
219
220 #define _LOG_INTERNAL_0(_src_level, _str) \
221 log_0(_str, _src_level)
222
223 #define _LOG_INTERNAL_1(_src_level, _str, _arg0) \
224 log_1(_str, (log_arg_t)(_arg0), _src_level)
225
226 #define _LOG_INTERNAL_2(_src_level, _str, _arg0, _arg1) \
227 log_2(_str, (log_arg_t)(_arg0), (log_arg_t)(_arg1), _src_level)
228
229 #define _LOG_INTERNAL_3(_src_level, _str, _arg0, _arg1, _arg2) \
230 log_3(_str, (log_arg_t)(_arg0), (log_arg_t)(_arg1), (log_arg_t)(_arg2), _src_level)
231
232 #define __LOG_ARG_CAST(_x) (log_arg_t)(_x)
233
234 #define __LOG_ARGUMENTS(...) FOR_EACH(__LOG_ARG_CAST, (,), __VA_ARGS__)
235
236 #define _LOG_INTERNAL_LONG(_src_level, _str, ...) \
237 do { \
238 log_arg_t args[] = {__LOG_ARGUMENTS(__VA_ARGS__)};\
239 log_n(_str, args, ARRAY_SIZE(args), _src_level); \
240 } while (false)
241
242 #define Z_LOG_LEVEL_CHECK(_level, _check_level, _default_level) \
243 (_level <= Z_LOG_RESOLVED_LEVEL(_check_level, _default_level))
244
245 #define Z_LOG_CONST_LEVEL_CHECK(_level) \
246 (IS_ENABLED(CONFIG_LOG) && \
247 (Z_LOG_LEVEL_CHECK(_level, CONFIG_LOG_OVERRIDE_LEVEL, LOG_LEVEL_NONE) \
248 || \
249 ((IS_ENABLED(CONFIG_LOG_OVERRIDE_LEVEL) == false) && \
250 (_level <= __log_level) && \
251 (_level <= CONFIG_LOG_MAX_LEVEL) \
252 ) \
253 ))
254
255 /*****************************************************************************/
256 /****************** Defiinitions used by minimal logging *********************/
257 /*****************************************************************************/
258 void z_log_minimal_hexdump_print(int level, const void *data, size_t size);
259 void z_log_minimal_vprintk(const char *fmt, va_list ap);
260 void z_log_minimal_printk(const char *fmt, ...);
261
262 #define Z_LOG_TO_PRINTK(_level, fmt, ...) do { \
263 z_log_minimal_printk("%c: " fmt "\n", \
264 z_log_minimal_level_to_char(_level), \
265 ##__VA_ARGS__); \
266 } while (false)
267
268 #define Z_LOG_TO_VPRINTK(_level, fmt, valist) do { \
269 z_log_minimal_printk("%c: ", z_log_minimal_level_to_char(_level)); \
270 z_log_minimal_vprintk(fmt, valist); \
271 z_log_minimal_printk("\n"); \
272 } while (false)
273
z_log_minimal_level_to_char(int level)274 static inline char z_log_minimal_level_to_char(int level)
275 {
276 switch (level) {
277 case LOG_LEVEL_ERR:
278 return 'E';
279 case LOG_LEVEL_WRN:
280 return 'W';
281 case LOG_LEVEL_INF:
282 return 'I';
283 case LOG_LEVEL_DBG:
284 return 'D';
285 default:
286 return '?';
287 }
288 }
289
290 #define Z_LOG_INST(_inst) COND_CODE_1(CONFIG_LOG, (_inst), NULL)
291
292 /*****************************************************************************/
293 /****************** Macros for standard logging ******************************/
294 /*****************************************************************************/
295 #define Z_LOG2(_level, _source, _dsource, ...) do { \
296 if (!Z_LOG_CONST_LEVEL_CHECK(_level)) { \
297 break; \
298 } \
299 if (IS_ENABLED(CONFIG_LOG_MINIMAL)) { \
300 Z_LOG_TO_PRINTK(_level, __VA_ARGS__); \
301 break; \
302 } \
303 \
304 bool is_user_context = k_is_user_context(); \
305 if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) && !is_user_context && \
306 _level > Z_LOG_RUNTIME_FILTER((_dsource)->filters)) { \
307 break; \
308 } \
309 if (IS_ENABLED(CONFIG_LOG2)) { \
310 int _mode; \
311 void *_src = IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ? \
312 (void *)_dsource : (void *)_source; \
313 Z_LOG_MSG2_CREATE(UTIL_NOT(IS_ENABLED(CONFIG_USERSPACE)), _mode, \
314 CONFIG_LOG_DOMAIN_ID, _src, _level, NULL,\
315 0, __VA_ARGS__); \
316 } else { \
317 Z_LOG_INTERNAL(is_user_context, _level, \
318 _source, _dsource, __VA_ARGS__);\
319 } \
320 if (false) { \
321 /* Arguments checker present but never evaluated.*/ \
322 /* Placed here to ensure that __VA_ARGS__ are*/ \
323 /* evaluated once when log is enabled.*/ \
324 z_log_printf_arg_checker(__VA_ARGS__); \
325 } \
326 } while (false)
327
328 #define Z_LOG(_level, ...) \
329 Z_LOG2(_level, __log_current_const_data, __log_current_dynamic_data, __VA_ARGS__)
330
331 #define Z_LOG_INSTANCE(_level, _inst, ...) \
332 Z_LOG2(_level, \
333 COND_CODE_1(CONFIG_LOG_RUNTIME_FILTERING, (NULL), (Z_LOG_INST(_inst))), \
334 (struct log_source_dynamic_data *)COND_CODE_1( \
335 CONFIG_LOG_RUNTIME_FILTERING, \
336 (Z_LOG_INST(_inst)), (NULL)), \
337 __VA_ARGS__)
338
339 /*****************************************************************************/
340 /****************** Macros for hexdump logging *******************************/
341 /*****************************************************************************/
342 #define Z_LOG_HEXDUMP2(_level, _source, _dsource, _data, _len, ...) do { \
343 const char *_str = GET_ARG_N(1, __VA_ARGS__); \
344 if (!Z_LOG_CONST_LEVEL_CHECK(_level)) { \
345 break; \
346 } \
347 bool is_user_context = k_is_user_context(); \
348 \
349 if (IS_ENABLED(CONFIG_LOG_MINIMAL)) { \
350 Z_LOG_TO_PRINTK(_level, "%s", _str); \
351 z_log_minimal_hexdump_print(_level, \
352 (const char *)_data, _len);\
353 break; \
354 } \
355 if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) && !is_user_context && \
356 _level > Z_LOG_RUNTIME_FILTER((_dsource)->filters)) { \
357 break; \
358 } \
359 if (IS_ENABLED(CONFIG_LOG2)) { \
360 int mode; \
361 void *_src = IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ? \
362 (void *)_dsource : (void *)_source; \
363 Z_LOG_MSG2_CREATE(UTIL_NOT(IS_ENABLED(CONFIG_USERSPACE)), mode, \
364 CONFIG_LOG_DOMAIN_ID, _src, _level, \
365 _data, _len, \
366 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
367 (), \
368 (COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
369 ("%s", __VA_ARGS__), (__VA_ARGS__)))));\
370 break; \
371 } \
372 uint16_t src_id = \
373 IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ? \
374 LOG_DYNAMIC_ID_GET(_dsource) : LOG_CONST_ID_GET(_source);\
375 struct log_msg_ids src_level = { \
376 .level = _level, \
377 .domain_id = CONFIG_LOG_DOMAIN_ID, \
378 .source_id = src_id, \
379 }; \
380 if (is_user_context) { \
381 log_hexdump_from_user(src_level, _str, \
382 (const char *)_data, _len); \
383 } else if (IS_ENABLED(CONFIG_LOG_IMMEDIATE)) { \
384 log_hexdump_sync(src_level, _str, (const char *)_data, _len); \
385 } else { \
386 log_hexdump(_str, (const char *)_data, _len, src_level); \
387 } \
388 } while (false)
389
390 #define Z_LOG_HEXDUMP(_level, _data, _length, ...) \
391 Z_LOG_HEXDUMP2(_level, \
392 __log_current_const_data, \
393 __log_current_dynamic_data, \
394 _data, _length, __VA_ARGS__)
395
396 #define Z_LOG_HEXDUMP_INSTANCE(_level, _inst, _data, _length, _str) \
397 Z_LOG_HEXDUMP2(_level, \
398 COND_CODE_1(CONFIG_LOG_RUNTIME_FILTERING, (NULL), (Z_LOG_INST(_inst))), \
399 (struct log_source_dynamic_data *)COND_CODE_1( \
400 CONFIG_LOG_RUNTIME_FILTERING, \
401 (Z_LOG_INST(_inst)), (NULL)), \
402 _data, _length, _str)
403
404 /*****************************************************************************/
405 /****************** Filtering macros *****************************************/
406 /*****************************************************************************/
407
408 /** @brief Number of bits used to encode log level. */
409 #define LOG_LEVEL_BITS 3U
410
411 /** @brief Filter slot size. */
412 #define LOG_FILTER_SLOT_SIZE LOG_LEVEL_BITS
413
414 /** @brief Number of slots in one word. */
415 #define LOG_FILTERS_NUM_OF_SLOTS (32 / LOG_FILTER_SLOT_SIZE)
416
417 /** @brief Slot mask. */
418 #define LOG_FILTER_SLOT_MASK (BIT(LOG_FILTER_SLOT_SIZE) - 1U)
419
420 /** @brief Bit offset of a slot.
421 *
422 * @param _id Slot ID.
423 */
424 #define LOG_FILTER_SLOT_SHIFT(_id) (LOG_FILTER_SLOT_SIZE * (_id))
425
426 #define LOG_FILTER_SLOT_GET(_filters, _id) \
427 ((*(_filters) >> LOG_FILTER_SLOT_SHIFT(_id)) & LOG_FILTER_SLOT_MASK)
428
429 #define LOG_FILTER_SLOT_SET(_filters, _id, _filter) \
430 do { \
431 *(_filters) &= ~(LOG_FILTER_SLOT_MASK << \
432 LOG_FILTER_SLOT_SHIFT(_id)); \
433 *(_filters) |= ((_filter) & LOG_FILTER_SLOT_MASK) << \
434 LOG_FILTER_SLOT_SHIFT(_id); \
435 } while (false)
436
437 #define LOG_FILTER_AGGR_SLOT_IDX 0
438
439 #define LOG_FILTER_AGGR_SLOT_GET(_filters) \
440 LOG_FILTER_SLOT_GET(_filters, LOG_FILTER_AGGR_SLOT_IDX)
441
442 #define LOG_FILTER_FIRST_BACKEND_SLOT_IDX 1
443
444 /* Return aggregated (highest) level for all enabled backends, e.g. if there
445 * are 3 active backends, one backend is set to get INF logs from a module and
446 * two other backends are set for ERR, returned level is INF.
447 */
448 #define Z_LOG_RUNTIME_FILTER(_filter) \
449 LOG_FILTER_SLOT_GET(&_filter, LOG_FILTER_AGGR_SLOT_IDX)
450
451 /** @brief Log level value used to indicate log entry that should not be
452 * formatted (raw string).
453 */
454 #define LOG_LEVEL_INTERNAL_RAW_STRING LOG_LEVEL_NONE
455
456 extern struct log_source_const_data __log_const_start[];
457 extern struct log_source_const_data __log_const_end[];
458
459 /** @brief Enum with possible actions for strdup operation. */
460 enum log_strdup_action {
461 LOG_STRDUP_SKIP, /**< None RAM string duplication. */
462 LOG_STRDUP_EXEC, /**< Always duplicate RAM strings. */
463 LOG_STRDUP_CHECK_EXEC/**< Duplicate RAM strings, if not dupl. before.*/
464 };
465
466 #define Z_LOG_PRINTK(...) do { \
467 if (IS_ENABLED(CONFIG_LOG_MINIMAL) || !IS_ENABLED(CONFIG_LOG2)) { \
468 z_log_minimal_printk(__VA_ARGS__); \
469 break; \
470 } \
471 int _mode; \
472 if (0) {\
473 z_log_printf_arg_checker(__VA_ARGS__); \
474 } \
475 Z_LOG_MSG2_CREATE(!IS_ENABLED(CONFIG_USERSPACE), _mode, \
476 CONFIG_LOG_DOMAIN_ID, NULL, \
477 LOG_LEVEL_INTERNAL_RAW_STRING, NULL, 0, __VA_ARGS__);\
478 } while (0)
479
480
481 /** @brief Get name of the log source.
482 *
483 * @param source_id Source ID.
484 * @return Name.
485 */
log_name_get(uint32_t source_id)486 static inline const char *log_name_get(uint32_t source_id)
487 {
488 return __log_const_start[source_id].name;
489 }
490
491 /** @brief Get compiled level of the log source.
492 *
493 * @param source_id Source ID.
494 * @return Level.
495 */
log_compiled_level_get(uint32_t source_id)496 static inline uint8_t log_compiled_level_get(uint32_t source_id)
497 {
498 return __log_const_start[source_id].level;
499 }
500
501 /** @brief Get index of the log source based on the address of the constant data
502 * associated with the source.
503 *
504 * @param data Address of the constant data.
505 *
506 * @return Source ID.
507 */
log_const_source_id(const struct log_source_const_data * data)508 static inline uint32_t log_const_source_id(
509 const struct log_source_const_data *data)
510 {
511 return ((uint8_t *)data - (uint8_t *)__log_const_start)/
512 sizeof(struct log_source_const_data);
513 }
514
515 /** @brief Get number of registered sources. */
log_sources_count(void)516 static inline uint32_t log_sources_count(void)
517 {
518 return log_const_source_id(__log_const_end);
519 }
520
521 extern struct log_source_dynamic_data __log_dynamic_start[];
522 extern struct log_source_dynamic_data __log_dynamic_end[];
523
524 /** @brief Creates name of variable and section for runtime log data.
525 *
526 * @param _name Name.
527 */
528 #define LOG_ITEM_DYNAMIC_DATA(_name) UTIL_CAT(log_dynamic_, _name)
529
530 #define LOG_INSTANCE_DYNAMIC_DATA(_module_name, _inst) \
531 LOG_ITEM_DYNAMIC_DATA(LOG_INSTANCE_FULL_NAME(_module_name, _inst))
532
533 /** @brief Get pointer to the filter set of the log source.
534 *
535 * @param source_id Source ID.
536 *
537 * @return Pointer to the filter set.
538 */
log_dynamic_filters_get(uint32_t source_id)539 static inline uint32_t *log_dynamic_filters_get(uint32_t source_id)
540 {
541 return &__log_dynamic_start[source_id].filters;
542 }
543
544 /** @brief Get index of the log source based on the address of the dynamic data
545 * associated with the source.
546 *
547 * @param data Address of the dynamic data.
548 *
549 * @return Source ID.
550 */
log_dynamic_source_id(struct log_source_dynamic_data * data)551 static inline uint32_t log_dynamic_source_id(struct log_source_dynamic_data *data)
552 {
553 return ((uint8_t *)data - (uint8_t *)__log_dynamic_start)/
554 sizeof(struct log_source_dynamic_data);
555 }
556
557 /* Initialize runtime filters */
558 void z_log_runtime_filters_init(void);
559
560 /* Notify log_core that a backend was enabled. */
561 void z_log_notify_backend_enabled(void);
562
563 /** @brief Dummy function to trigger log messages arguments type checking. */
564 static inline __printf_like(1, 2)
z_log_printf_arg_checker(const char * fmt,...)565 void z_log_printf_arg_checker(const char *fmt, ...)
566 {
567 ARG_UNUSED(fmt);
568 }
569
570 /** @brief Standard log with no arguments.
571 *
572 * @param str String.
573 * @param src_level Log identification.
574 */
575 void log_0(const char *str, struct log_msg_ids src_level);
576
577 /** @brief Standard log with one argument.
578 *
579 * @param str String.
580 * @param arg1 First argument.
581 * @param src_level Log identification.
582 */
583 void log_1(const char *str,
584 log_arg_t arg1,
585 struct log_msg_ids src_level);
586
587 /** @brief Standard log with two arguments.
588 *
589 * @param str String.
590 * @param arg1 First argument.
591 * @param arg2 Second argument.
592 * @param src_level Log identification.
593 */
594 void log_2(const char *str,
595 log_arg_t arg1,
596 log_arg_t arg2,
597 struct log_msg_ids src_level);
598
599 /** @brief Standard log with three arguments.
600 *
601 * @param str String.
602 * @param arg1 First argument.
603 * @param arg2 Second argument.
604 * @param arg3 Third argument.
605 * @param src_level Log identification.
606 */
607 void log_3(const char *str,
608 log_arg_t arg1,
609 log_arg_t arg2,
610 log_arg_t arg3,
611 struct log_msg_ids src_level);
612
613 /** @brief Standard log with arguments list.
614 *
615 * @param str String.
616 * @param args Array with arguments.
617 * @param narg Number of arguments in the array.
618 * @param src_level Log identification.
619 */
620 void log_n(const char *str,
621 log_arg_t *args,
622 uint32_t narg,
623 struct log_msg_ids src_level);
624
625 /** @brief Hexdump log.
626 *
627 * @param str String.
628 * @param data Data.
629 * @param length Data length.
630 * @param src_level Log identification.
631 */
632 void log_hexdump(const char *str, const void *data, uint32_t length,
633 struct log_msg_ids src_level);
634
635 /** @brief Process log message synchronously.
636 *
637 * @param src_level Log message details.
638 * @param fmt String to format.
639 * @param ... Variable list of arguments.
640 */
641 void log_string_sync(struct log_msg_ids src_level, const char *fmt, ...);
642
643 /** @brief Process log hexdump message synchronously.
644 *
645 * @param src_level Log message details.
646 * @param metadata Raw string associated with the data.
647 * @param data Data.
648 * @param len Data length.
649 */
650 void log_hexdump_sync(struct log_msg_ids src_level, const char *metadata,
651 const void *data, uint32_t len);
652
653 /**
654 * @brief Writes a generic log message to the log.
655 *
656 * @note This function is intended to be used when porting other log systems.
657 *
658 * @param src_level Log identification.
659 * @param fmt String to format.
660 * @param ap Poiner to arguments list.
661 * @param strdup_action Manages strdup activity.
662 */
663 void log_generic(struct log_msg_ids src_level, const char *fmt, va_list ap,
664 enum log_strdup_action strdup_action);
665
666 /**
667 * @brief Writes a generic log message to the logging v2.
668 *
669 * @note This function is intended to be used when porting other log systems.
670 *
671 * @param level Log level..
672 * @param fmt String to format.
673 * @param ap Poiner to arguments list.
674 */
log2_generic(uint8_t level,const char * fmt,va_list ap)675 static inline void log2_generic(uint8_t level, const char *fmt, va_list ap)
676 {
677 z_log_msg2_runtime_vcreate(CONFIG_LOG_DOMAIN_ID, NULL, level,
678 NULL, 0, fmt, ap);
679 }
680
681 /**
682 * @brief Returns number of arguments visible from format string.
683 *
684 * @note This function is intended to be used when porting other log systems.
685 *
686 * @param fmt Format string.
687 *
688 * @return Number of arguments.
689 */
690 uint32_t log_count_args(const char *fmt);
691
692 /**
693 * @brief Writes a generic log message to the log from user mode.
694 *
695 * @note This function is intended to be used internally
696 * by the logging subsystem.
697 */
698 void log_generic_from_user(struct log_msg_ids src_level,
699 const char *fmt, va_list ap);
700
701 /** @brief Check if address belongs to the memory pool used for transient.
702 *
703 * @param buf Buffer.
704 *
705 * @return True if address within the pool, false otherwise.
706 */
707 bool log_is_strdup(const void *buf);
708
709 /** @brief Free allocated buffer.
710 *
711 * @param buf Buffer.
712 */
713 void log_free(void *buf);
714
715 /**
716 * @brief Get current number of allocated buffers for string duplicates.
717 */
718 uint32_t log_get_strdup_pool_current_utilization(void);
719
720 /**
721 * @brief Get maximal number of simultaneously allocated buffers for string
722 * duplicates.
723 *
724 * Value can be used to determine pool size.
725 */
726 uint32_t log_get_strdup_pool_utilization(void);
727
728 /**
729 * @brief Get length of the longest string duplicated.
730 *
731 * Value can be used to determine buffer size in the string duplicates pool.
732 */
733 uint32_t log_get_strdup_longest_string(void);
734
735 /** @brief Indicate to the log core that one log message has been dropped.
736 *
737 * @param buffered True if dropped message was already buffered and it is being
738 * dropped to free space for another message. False if message is being dropped
739 * because allocation failed.
740 */
741 void z_log_dropped(bool buffered);
742
743 /** @brief Read and clear current drop indications counter.
744 *
745 * @return Dropped count.
746 */
747 uint32_t z_log_dropped_read_and_clear(void);
748
749 /** @brief Check if there are any pending drop notifications.
750 *
751 * @retval true Pending unreported drop indications.
752 * @retval false No pending unreported drop indications.
753 */
754 bool z_log_dropped_pending(void);
755
756 /** @brief Log a message from user mode context.
757 *
758 * @note This function is intended to be used internally
759 * by the logging subsystem.
760 *
761 * @param src_level Log identification.
762 * @param fmt String to format.
763 * @param ... Variable list of arguments.
764 */
765 void __printf_like(2, 3) log_from_user(struct log_msg_ids src_level,
766 const char *fmt, ...);
767
768 /* Internal function used by log_from_user(). */
769 __syscall void z_log_string_from_user(uint32_t src_level_val, const char *str);
770
771 /**
772 * @brief Create mask with occurences of a string format specifiers (%s).
773 *
774 * Result is stored as the mask (argument n is n'th bit). Bit is set if string
775 * format specifier was found.
776 *
777 * @param str String.
778 * @param nargs Number of arguments in the string.
779 *
780 * @return Mask with %s format specifiers found.
781 */
782 uint32_t z_log_get_s_mask(const char *str, uint32_t nargs);
783
784 /** @brief Log binary data (displayed as hexdump) from user mode context.
785 *
786 * @note This function is intended to be used internally
787 * by the logging subsystem.
788 *
789 * @param src_level Log identification.
790 * @param metadata Raw string associated with the data.
791 * @param data Data.
792 * @param len Data length.
793 */
794 void log_hexdump_from_user(struct log_msg_ids src_level, const char *metadata,
795 const void *data, uint32_t len);
796
797 /* Internal function used by log_hexdump_from_user(). */
798 __syscall void z_log_hexdump_from_user(uint32_t src_level_val,
799 const char *metadata,
800 const uint8_t *data, uint32_t len);
801
802 /******************************************************************************/
803 /********** Mocros _VA operate on var-args parameters. ***************/
804 /********* Intended to be used when porting other log systems. ***************/
805 /********* Shall be used in the log entry interface function. ***************/
806 /********* Speed optimized for up to three arguments number. ***************/
807 /******************************************************************************/
808 #define Z_LOG_VA(_level, _str, _valist, _argnum, _strdup_action)\
809 __LOG_VA(_level, \
810 __log_current_const_data, \
811 __log_current_dynamic_data, \
812 _str, _valist, _argnum, _strdup_action)
813
814 #define __LOG_VA(_level, _source, _dsource, _str, _valist, _argnum, _strdup_action) do { \
815 if (!Z_LOG_CONST_LEVEL_CHECK(_level)) { \
816 break; \
817 } \
818 if (IS_ENABLED(CONFIG_LOG_MINIMAL)) { \
819 Z_LOG_TO_VPRINTK(_level, _str, _valist); \
820 break; \
821 } \
822 \
823 bool is_user_context = k_is_user_context(); \
824 uint32_t filters = IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ? \
825 _dsource->filters : 0;\
826 if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) && !is_user_context && \
827 _level > Z_LOG_RUNTIME_FILTER(filters)) { \
828 break; \
829 } \
830 if (IS_ENABLED(CONFIG_LOG2)) { \
831 z_log_msg2_runtime_vcreate(CONFIG_LOG_DOMAIN_ID, _source, \
832 _level, NULL, 0, _str, _valist); \
833 break; \
834 } \
835 uint16_t _id = \
836 IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ? \
837 LOG_DYNAMIC_ID_GET(_dsource) : LOG_CONST_ID_GET(_source);\
838 struct log_msg_ids src_level = { \
839 .level = _level, \
840 .domain_id = CONFIG_LOG_DOMAIN_ID, \
841 .source_id = _id \
842 }; \
843 __LOG_INTERNAL_VA(is_user_context, \
844 src_level, \
845 _str, _valist, _argnum, \
846 _strdup_action); \
847 } while (false)
848
849 /**
850 * @brief Inline function to perform strdup, used in __LOG_INTERNAL_VA macro
851 *
852 * @note This function is intended to be used when porting other log systems.
853 *
854 * @param msk Bitmask marking all %s arguments.
855 * @param idx Index of actually processed argument.
856 * @param param Value of actually processed argument.
857 * @param action Action for strdup operation.
858 *
859 * @return Duplicated string or not changed param.
860 */
z_log_do_strdup(uint32_t msk,uint32_t idx,log_arg_t param,enum log_strdup_action action)861 static inline log_arg_t z_log_do_strdup(uint32_t msk, uint32_t idx,
862 log_arg_t param,
863 enum log_strdup_action action)
864 {
865 #ifndef CONFIG_LOG_MINIMAL
866 char *z_log_strdup(const char *str);
867
868 if (msk & (1 << idx)) {
869 const char *str = (const char *)param;
870 /* is_rodata(str) is not checked,
871 * because log_strdup does it.
872 * Hence, we will do only optional check
873 * if already not duplicated.
874 */
875 if (action == LOG_STRDUP_EXEC || !log_is_strdup(str)) {
876 param = (log_arg_t)z_log_strdup(str);
877 }
878 }
879 #else
880 ARG_UNUSED(msk);
881 ARG_UNUSED(idx);
882 ARG_UNUSED(action);
883 #endif
884 return param;
885 }
886
887 #define __LOG_INTERNAL_VA(is_user_context, _src_level, _str, _valist, \
888 _argnum, _strdup_action) \
889 do { \
890 if (is_user_context) { \
891 log_generic_from_user(_src_level, _str, _valist); \
892 } else if (IS_ENABLED(CONFIG_LOG_IMMEDIATE)) { \
893 log_generic(_src_level, _str, _valist, _strdup_action); \
894 } else if (_argnum == 0) { \
895 _LOG_INTERNAL_0(_src_level, _str); \
896 } else { \
897 uint32_t mask = (_strdup_action != LOG_STRDUP_SKIP) ? \
898 z_log_get_s_mask(_str, _argnum) \
899 : 0; \
900 \
901 if (_argnum == 1) { \
902 _LOG_INTERNAL_1(_src_level, _str, \
903 z_log_do_strdup(mask, 0, \
904 va_arg(_valist, log_arg_t), _strdup_action));\
905 } else if (_argnum == 2) { \
906 _LOG_INTERNAL_2(_src_level, _str, \
907 z_log_do_strdup(mask, 0, \
908 va_arg(_valist, log_arg_t), _strdup_action), \
909 z_log_do_strdup(mask, 1, \
910 va_arg(_valist, log_arg_t), _strdup_action));\
911 } else if (_argnum == 3) { \
912 _LOG_INTERNAL_3(_src_level, _str, \
913 z_log_do_strdup(mask, 0, \
914 va_arg(_valist, log_arg_t), _strdup_action), \
915 z_log_do_strdup(mask, 1, \
916 va_arg(_valist, log_arg_t), _strdup_action), \
917 z_log_do_strdup(mask, 2, \
918 va_arg(_valist, log_arg_t), _strdup_action));\
919 } else { \
920 log_generic(_src_level, _str, _valist, _strdup_action);\
921 } \
922 } \
923 } while (false)
924
925 #include <syscalls/log_core.h>
926
927 #ifdef __cplusplus
928 }
929 #endif
930
931 #endif /* ZEPHYR_INCLUDE_LOGGING_LOG_CORE_H_ */
932