Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
log.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_LOGGING_LOG_H_
8#define ZEPHYR_INCLUDE_LOGGING_LOG_H_
9
11#include <logging/log_core.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
40#define LOG_ERR(...) Z_LOG(LOG_LEVEL_ERR, __VA_ARGS__)
41
51#define LOG_WRN(...) Z_LOG(LOG_LEVEL_WRN, __VA_ARGS__)
52
61#define LOG_INF(...) Z_LOG(LOG_LEVEL_INF, __VA_ARGS__)
62
71#define LOG_DBG(...) Z_LOG(LOG_LEVEL_DBG, __VA_ARGS__)
72
82#define LOG_PRINTK(...) Z_LOG_PRINTK(__VA_ARGS__)
83
96#define LOG_INST_ERR(_log_inst, ...) \
97 Z_LOG_INSTANCE(LOG_LEVEL_ERR, _log_inst, __VA_ARGS__)
98
112#define LOG_INST_WRN(_log_inst, ...) \
113 Z_LOG_INSTANCE(LOG_LEVEL_WRN, _log_inst, __VA_ARGS__)
114
127#define LOG_INST_INF(_log_inst, ...) \
128 Z_LOG_INSTANCE(LOG_LEVEL_INF, _log_inst, __VA_ARGS__)
129
142#define LOG_INST_DBG(_log_inst, ...) \
143 Z_LOG_INSTANCE(LOG_LEVEL_DBG, _log_inst, __VA_ARGS__)
144
155#define LOG_HEXDUMP_ERR(_data, _length, _str) \
156 Z_LOG_HEXDUMP(LOG_LEVEL_ERR, _data, _length, _str)
157
168#define LOG_HEXDUMP_WRN(_data, _length, _str) \
169 Z_LOG_HEXDUMP(LOG_LEVEL_WRN, _data, _length, _str)
170
180#define LOG_HEXDUMP_INF(_data, _length, _str) \
181 Z_LOG_HEXDUMP(LOG_LEVEL_INF, _data, _length, _str)
182
192#define LOG_HEXDUMP_DBG(_data, _length, _str) \
193 Z_LOG_HEXDUMP(LOG_LEVEL_DBG, _data, _length, _str)
194
209#define LOG_INST_HEXDUMP_ERR(_log_inst, _data, _length, _str) \
210 Z_LOG_HEXDUMP_INSTANCE(LOG_LEVEL_ERR, _log_inst, _data, _length, _str)
211
224#define LOG_INST_HEXDUMP_WRN(_log_inst, _data, _length, _str) \
225 Z_LOG_HEXDUMP_INSTANCE(LOG_LEVEL_WRN, _log_inst, _data, _length, _str)
226
238#define LOG_INST_HEXDUMP_INF(_log_inst, _data, _length, _str) \
239 Z_LOG_HEXDUMP_INSTANCE(LOG_LEVEL_INF, _log_inst, _data, _length, _str)
240
252#define LOG_INST_HEXDUMP_DBG(_log_inst, _data, _length, _str) \
253 Z_LOG_HEXDUMP_INSTANCE(LOG_LEVEL_DBG, _log_inst, _data, _length, _str)
254
266void z_log_printk(const char *fmt, va_list ap);
267static inline void log_printk(const char *fmt, va_list ap)
268{
269 z_log_printk(fmt, ap);
270}
271
289char *z_log_strdup(const char *str);
290static inline char *log_strdup(const char *str)
291{
292 if (IS_ENABLED(CONFIG_LOG_MINIMAL) || IS_ENABLED(CONFIG_LOG2)) {
293 return (char *)str;
294 }
295
296 return z_log_strdup(str);
297}
298
299#ifdef __cplusplus
300}
301#define LOG_IN_CPLUSPLUS 1
302#endif
303/* Macro expects that optionally on second argument local log level is provided.
304 * If provided it is returned, otherwise default log level is returned or
305 * LOG_LEVEL, if it was locally defined.
306 */
307#if !defined(CONFIG_LOG)
308#define _LOG_LEVEL_RESOLVE(...) LOG_LEVEL_NONE
309#else
310#define _LOG_LEVEL_RESOLVE(...) \
311 Z_LOG_EVAL(LOG_LEVEL, \
312 (GET_ARG_N(2, __VA_ARGS__, LOG_LEVEL)), \
313 (GET_ARG_N(2, __VA_ARGS__, CONFIG_LOG_DEFAULT_LEVEL)))
314#endif
315
316/* Return first argument */
317#define _LOG_ARG1(arg1, ...) arg1
318
319#define _LOG_MODULE_CONST_DATA_CREATE(_name, _level) \
320 IF_ENABLED(LOG_IN_CPLUSPLUS, (extern)) \
321 const struct log_source_const_data LOG_ITEM_CONST_DATA(_name) \
322 __attribute__ ((section("." STRINGIFY(LOG_ITEM_CONST_DATA(_name))))) \
323 __attribute__((used)) = { \
324 .name = STRINGIFY(_name), \
325 .level = _level \
326 }
327
328#define _LOG_MODULE_DYNAMIC_DATA_CREATE(_name) \
329 struct log_source_dynamic_data LOG_ITEM_DYNAMIC_DATA(_name) \
330 __attribute__ ((section("." STRINGIFY( \
331 LOG_ITEM_DYNAMIC_DATA(_name)))) \
332 ) \
333 __attribute__((used))
334
335#define _LOG_MODULE_DYNAMIC_DATA_COND_CREATE(_name) \
336 IF_ENABLED(CONFIG_LOG_RUNTIME_FILTERING, \
337 (_LOG_MODULE_DYNAMIC_DATA_CREATE(_name);))
338
339#define _LOG_MODULE_DATA_CREATE(_name, _level) \
340 _LOG_MODULE_CONST_DATA_CREATE(_name, _level); \
341 _LOG_MODULE_DYNAMIC_DATA_COND_CREATE(_name)
342
375#define LOG_MODULE_REGISTER(...) \
376 Z_LOG_EVAL( \
377 _LOG_LEVEL_RESOLVE(__VA_ARGS__), \
378 (_LOG_MODULE_DATA_CREATE(GET_ARG_N(1, __VA_ARGS__), \
379 _LOG_LEVEL_RESOLVE(__VA_ARGS__))),\
380 ()/*Empty*/ \
381 ) \
382 LOG_MODULE_DECLARE(__VA_ARGS__)
383
410#define LOG_MODULE_DECLARE(...) \
411 extern const struct log_source_const_data \
412 LOG_ITEM_CONST_DATA(GET_ARG_N(1, __VA_ARGS__)); \
413 extern struct log_source_dynamic_data \
414 LOG_ITEM_DYNAMIC_DATA(GET_ARG_N(1, __VA_ARGS__)); \
415 \
416 static const struct log_source_const_data * \
417 __log_current_const_data __unused = \
418 _LOG_LEVEL_RESOLVE(__VA_ARGS__) ? \
419 &LOG_ITEM_CONST_DATA(GET_ARG_N(1, __VA_ARGS__)) : \
420 NULL; \
421 \
422 static struct log_source_dynamic_data * \
423 __log_current_dynamic_data __unused = \
424 (_LOG_LEVEL_RESOLVE(__VA_ARGS__) && \
425 IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) ? \
426 &LOG_ITEM_DYNAMIC_DATA(GET_ARG_N(1, __VA_ARGS__)) : \
427 NULL; \
428 \
429 static const uint32_t __log_level __unused = \
430 _LOG_LEVEL_RESOLVE(__VA_ARGS__)
431
439#define LOG_LEVEL_SET(level) static const uint32_t __log_level __unused = \
440 Z_LOG_RESOLVED_LEVEL(level, 0)
441
442/*
443 * Eclipse CDT parser is sometimes confused by logging API code and freezes the
444 * whole IDE. Following lines hides LOG_x macros from CDT.
445 */
446#if defined(__CDT_PARSER__)
447#undef LOG_ERR
448#undef LOG_WRN
449#undef LOG_INF
450#undef LOG_DBG
451
452#undef LOG_HEXDUMP_ERR
453#undef LOG_HEXDUMP_WRN
454#undef LOG_HEXDUMP_INF
455#undef LOG_HEXDUMP_DBG
456
457#define LOG_ERR(...) (void) 0
458#define LOG_WRN(...) (void) 0
459#define LOG_DBG(...) (void) 0
460#define LOG_INF(...) (void) 0
461
462#define LOG_HEXDUMP_ERR(...) (void) 0
463#define LOG_HEXDUMP_WRN(...) (void) 0
464#define LOG_HEXDUMP_DBG(...) (void) 0
465#define LOG_HEXDUMP_INF(...) (void) 0
466#endif
467
472#endif /* ZEPHYR_INCLUDE_LOGGING_LOG_H_ */
static char * log_strdup(const char *str)
Definition: log.h:290
static void log_printk(const char *fmt, va_list ap)
Definition: log.h:267
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition: util_macro.h:101