ODP: [Zephyr-devel] logging MACRO LOG_ERR/LOG_INF/LOG_DBG expansion issue


Chruściński, Krzysztof
 

Hi,

compiler is able to figure out things and remove code that is known not to be executed.

Code like this will disapear from the binary:

if ( 2 > 3) {
   foo();
}

It can even be more complex like here were constant comes from inline function:

static inline int f3(void)
{
    return 3;
}

if (2 > f3()) {
  foo();
}

regards,
Krzysztof

Od: devel@... <devel@...> w imieniu użytkownika qzhang via lists.zephyrproject.org <qzhang=ambiq.com@...>
Wysłane: piątek, 14 maja 2021 09:03
Do: devel@... <devel@...>
Temat: [Zephyr-devel] logging MACRO LOG_ERR/LOG_INF/LOG_DBG expansion issue
 
Hi,

I am new to zephyr, I am reading the logging subsystem code and find that 'LOG_ERR/LOG_INF/LOG_DBG ..., etc.' will be expanded finally by below MACRO

looks like even I disable the logging (not set CONFIG_LOG), below code is still remained in the code after the preprocessing, Is my understanding correct or not? does it cause the code bloat?  

/*****************************************************************************/
/****************** Macros for standard logging ******************************/
/*****************************************************************************/
#define Z_LOG2(_level, _source, _dsource, ...) do { \
if (!Z_LOG_CONST_LEVEL_CHECK(_level)) { \
break; \
} \
if (IS_ENABLED(CONFIG_LOG_MINIMAL)) { \
Z_LOG_TO_PRINTK(_level, __VA_ARGS__); \
break; \
} \
\
bool is_user_context = k_is_user_context(); \
uint32_t filters = IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ? \
(_dsource)->filters : 0;\
if (!LOG_CHECK_CTX_LVL_FILTER(is_user_context, _level, filters)) { \
break; \
} \
if (IS_ENABLED(CONFIG_LOG2)) { \
int _mode; \
void *_src = IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ? \
(void *)_dsource : (void *)_source; \
Z_LOG_MSG2_CREATE(UTIL_NOT(IS_ENABLED(CONFIG_USERSPACE)), _mode, \
  CONFIG_LOG_DOMAIN_ID, _src, _level, NULL,\
  0, __VA_ARGS__); \
} else { \
Z_LOG_INTERNAL(is_user_context, _level, \
_source, _dsource, __VA_ARGS__);\
} \
if (false) { \
/* Arguments checker present but never evaluated.*/ \
/* Placed here to ensure that __VA_ARGS__ are*/ \
/* evaluated once when log is enabled.*/ \
z_log_printf_arg_checker(__VA_ARGS__); \
} \
} while (false) 

Br
Quan

Join devel@lists.zephyrproject.org to automatically receive all group messages.