1 /* 2 * Copyright (c) 2024 Nordic Semiconductor ASA 3 * SPDX-License-Identifier: Apache-2.0 4 */ 5 6 /** 7 * @file 8 * Custom logging 9 */ 10 11 #ifndef SOC_NORDIC_COMMON_ZEPHYR_CUSTOM_LOG_H_ 12 #define SOC_NORDIC_COMMON_ZEPHYR_CUSTOM_LOG_H_ 13 14 #include <zephyr/logging/log_frontend_stmesp.h> 15 #include <zephyr/sys/cbprintf_internal.h> 16 17 #ifdef __cplusplus 18 extern "C" { 19 #endif 20 21 /* Undef to override those macros. */ 22 #undef LOG_ERR 23 #undef LOG_WRN 24 #undef LOG_INF 25 #undef LOG_DBG 26 #undef LOG_INST_ERR 27 #undef LOG_INST_WRN 28 #undef LOG_INST_INF 29 #undef LOG_INST_DBG 30 31 /** @brief Optimized macro for log message with no arguments. 32 * 33 * In order to compress information, logging level is stringified and prepended 34 * to the string. 35 * 36 * @param _level Level. 37 * @param _module_const_source Constant module structure. 38 * @param _source Source structure (dynamic or constant). 39 * @param ... String (with no arguments). 40 */ 41 #define Z_LOG_STMESP_0(_level, _source, ...) \ 42 do { \ 43 if (!Z_LOG_LEVEL_ALL_CHECK(_level, __log_current_const_data, _source)) { \ 44 break; \ 45 } \ 46 LOG_FRONTEND_STMESP_LOG0(_source, STRINGIFY(_level) __VA_ARGS__); \ 47 } while (0) 48 49 /** @brief Determine if first argument is a numeric value that fits in 32 bit word. 50 * 51 * @return 1 if yes and 0 if not. 52 */ 53 #define Z_LOG_STMESP_1_ARG_CHECK(...) \ 54 COND_CODE_1(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \ 55 (Z_CBPRINTF_IS_WORD_NUM(GET_ARG_N(2, __VA_ARGS__, dummy))), (0)) 56 57 /** @brief Optimized macro for log message with 1 numeric argument. 58 * 59 * In order to compress information, logging level is stringified and prepended 60 * to the string. 61 * 62 * @param _level Level. 63 * @param _module_const_source Constant module structure. 64 * @param _source Source structure (dynamic or constant). 65 * @param ... String (with 1 argument). 66 */ 67 #define Z_LOG_STMESP_1(_level, _source, ...) \ 68 do { \ 69 /* Do turbo logging only if argument fits in 32 bit word. */ \ 70 if (!Z_LOG_STMESP_1_ARG_CHECK(__VA_ARGS__)) { \ 71 COND_CODE_1(CONFIG_LOG_FRONTEND_STMESP_TURBO_DROP_OTHERS, (), \ 72 (Z_LOG(_level, __VA_ARGS__))); \ 73 } \ 74 if (!Z_LOG_LEVEL_ALL_CHECK(_level, __log_current_const_data, _source)) { \ 75 break; \ 76 } \ 77 LOG_FRONTEND_STMESP_LOG1(_source, STRINGIFY(_level) __VA_ARGS__, dummy); \ 78 } while (0) 79 80 /** @brief Top level logging macro. 81 * 82 * Macro is using special approach for short log message (0 or 1 numeric argument) 83 * and proceeds with standard approach (or optionally drops) for remaining messages. 84 * 85 * @param _level Severity level. 86 * @param _source Pointer to a structure associated with the source. 87 * @param ... String with arguments. 88 */ 89 #define Z_LOG_STMESP(_level, _source, ...) \ 90 COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \ 91 (Z_LOG_STMESP_0(_level, _source, __VA_ARGS__)), ( \ 92 COND_CODE_1(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \ 93 (Z_LOG_STMESP_1(_level, _source, __VA_ARGS__)), \ 94 ( \ 95 if (!IS_ENABLED(CONFIG_LOG_FRONTEND_STMESP_TURBO_DROP_OTHERS)) { \ 96 Z_LOG(_level, __VA_ARGS__); \ 97 })))) 98 99 /* Overridden logging API macros. */ 100 #define LOG_ERR(...) Z_LOG_STMESP(LOG_LEVEL_ERR, Z_LOG_CURRENT_DATA(), __VA_ARGS__) 101 #define LOG_WRN(...) Z_LOG_STMESP(LOG_LEVEL_WRN, Z_LOG_CURRENT_DATA(), __VA_ARGS__) 102 #define LOG_INF(...) Z_LOG_STMESP(LOG_LEVEL_INF, Z_LOG_CURRENT_DATA(), __VA_ARGS__) 103 #define LOG_DBG(...) Z_LOG_STMESP(LOG_LEVEL_DBG, Z_LOG_CURRENT_DATA(), __VA_ARGS__) 104 105 #define LOG_INST_ERR(_inst, ...) Z_LOG_STMESP(LOG_LEVEL_ERR, Z_LOG_INST(_inst), __VA_ARGS__) 106 #define LOG_INST_WRN(_inst, ...) Z_LOG_STMESP(LOG_LEVEL_WRN, Z_LOG_INST(_inst), __VA_ARGS__) 107 #define LOG_INST_INF(_inst, ...) Z_LOG_STMESP(LOG_LEVEL_INF, Z_LOG_INST(_inst), __VA_ARGS__) 108 #define LOG_INST_DBG(_inst, ...) Z_LOG_STMESP(LOG_LEVEL_DBG, Z_LOG_INST(_inst), __VA_ARGS__) 109 110 #if CONFIG_LOG_FRONTEND_STMESP_TURBO_DROP_OTHERS 111 #undef LOG_RAW 112 #undef LOG_PRINTK 113 #undef LOG_HEXDUMP_ERR 114 #undef LOG_HEXDUMP_WRN 115 #undef LOG_HEXDUMP_INF 116 #undef LOG_HEXDUMP_DBG 117 #undef LOG_INST_HEXDUMP_ERR 118 #undef LOG_INST_HEXDUMP_WRN 119 #undef LOG_INST_HEXDUMP_INF 120 #undef LOG_INST_HEXDUMP_DBG 121 122 #define LOG_RAW(...) \ 123 do { \ 124 if (0) { \ 125 Z_LOG_PRINTK(1, __VA_ARGS__); \ 126 } \ 127 } while (0) 128 129 #define LOG_PRINTK(...) \ 130 do { \ 131 if (0) { \ 132 Z_LOG_PRINTK(1, __VA_ARGS__); \ 133 } \ 134 } while (0) 135 136 #define LOG_HEXDUMP_ERR(_data, _length, _str) (void)_data 137 #define LOG_HEXDUMP_WRN(_data, _length, _str) (void)_data 138 #define LOG_HEXDUMP_INF(_data, _length, _str) (void)_data 139 #define LOG_HEXDUMP_DBG(_data, _length, _str) (void)_data 140 #define LOG_INST_HEXDUMP_ERR(_inst, _data, _length, _str) (void)_data 141 #define LOG_INST_HEXDUMP_WRN(_inst, _data, _length, _str) (void)_data 142 #define LOG_INST_HEXDUMP_INF(_inst, _data, _length, _str) (void)_data 143 #define LOG_INST_HEXDUMP_DBG(_inst, _data, _length, _str) (void)_data 144 #endif 145 146 #ifdef __cplusplus 147 } 148 #endif 149 150 #endif /* SOC_NORDIC_COMMON_ZEPHYR_CUSTOM_LOG_H_ */ 151