@@ -182,7 +182,7 @@ extern "C" {
182182 * @retval false Drop that message.
183183 */
184184#define Z_LOG_DYNAMIC_LEVEL_CHECK (_level , _source ) \
185- (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) || k_is_user_context() || \
185+ (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) || \
186186 ((_level) <= Z_LOG_RUNTIME_FILTER(((struct log_source_dynamic_data *)_source)->filters)))
187187
188188/** @brief Check if message shall be created.
@@ -199,7 +199,30 @@ extern "C" {
199199#define Z_LOG_LEVEL_ALL_CHECK (_level , _inst , _source ) \
200200 (Z_LOG_CONST_LEVEL_CHECK(_level) && \
201201 Z_LOG_STATIC_INST_LEVEL_CHECK(_level, _inst, _source) && \
202- Z_LOG_DYNAMIC_LEVEL_CHECK(_level, _source))
202+ COND_CODE_0(IS_ENABLED(CONFIG_USERSPACE), \
203+ (Z_LOG_DYNAMIC_LEVEL_CHECK(_level, _source)), \
204+ (true)) \
205+ )
206+
207+ /** @brief Check if the message shall be created, otherwise break.
208+ *
209+ * Aggregate all checks into a single one. Calls break to skip message creation. Userspace safe.
210+ *
211+ * @param _level Log level.
212+ * @param _inst 1 is source is the instance of a module.
213+ * @param _source Data associated with the source.
214+ */
215+ #define Z_LOG_LEVEL_ALL_CHECK_BREAK (_level , _inst , _source ) \
216+ if (!Z_LOG_LEVEL_ALL_CHECK((_level), (_inst), (_source))) { \
217+ break; \
218+ } \
219+ IF_ENABLED(CONFIG_USERSPACE, ( \
220+ if (!k_is_user_context()) { \
221+ compiler_barrier(); \
222+ if (!Z_LOG_DYNAMIC_LEVEL_CHECK((_level), (_source))) { \
223+ break; \
224+ } \
225+ }))
203226
204227/** @brief Get current module data that is used for source id retrieving.
205228 *
@@ -291,9 +314,7 @@ static inline char z_log_minimal_level_to_char(int level)
291314#define Z_LOG2 (_level , _inst , _source , ...) \
292315 TOOLCHAIN_DISABLE_CLANG_WARNING(TOOLCHAIN_WARNING_USED_BUT_MARKED_UNUSED) \
293316 do { \
294- if (!Z_LOG_LEVEL_ALL_CHECK(_level, _inst, _source)) { \
295- break; \
296- } \
317+ Z_LOG_LEVEL_ALL_CHECK_BREAK(_level, _inst, _source) \
297318 if (IS_ENABLED(CONFIG_LOG_MODE_MINIMAL)) { \
298319 Z_LOG_TO_PRINTK(_level, __VA_ARGS__); \
299320 break; \
@@ -346,9 +367,7 @@ static inline char z_log_minimal_level_to_char(int level)
346367#define Z_LOG_HEXDUMP2 (_level , _inst , _source , _data , _len , ...) \
347368 TOOLCHAIN_DISABLE_CLANG_WARNING(TOOLCHAIN_WARNING_USED_BUT_MARKED_UNUSED) \
348369 do { \
349- if (!Z_LOG_LEVEL_ALL_CHECK(_level, _inst, _source)) { \
350- break; \
351- } \
370+ Z_LOG_LEVEL_ALL_CHECK_BREAK(_level, _inst, _source) \
352371 const char *_str = GET_ARG_N(1, __VA_ARGS__); \
353372 if (IS_ENABLED(CONFIG_LOG_MODE_MINIMAL)) { \
354373 Z_LOG_TO_PRINTK(_level, "%s", _str); \
0 commit comments