Skip to content

Commit 8cdca11

Browse files
nordic-krchnashif
authored andcommitted
logging: Replace custom macros with generic ones from util.h
Some of macros initially created in the logger has been moved to util.h. This commit replaces custom macros with the one from util.h Signed-off-by: Krzysztof Chruscinski <[email protected]>
1 parent 383f7be commit 8cdca11

File tree

3 files changed

+20
-54
lines changed

3 files changed

+20
-54
lines changed

include/logging/log.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ char *log_strdup(const char *str);
285285
#else
286286
#define _LOG_LEVEL_RESOLVE(...) \
287287
_LOG_EVAL(LOG_LEVEL, \
288-
(__LOG_ARG_2(__VA_ARGS__, LOG_LEVEL)), \
289-
(__LOG_ARG_2(__VA_ARGS__, CONFIG_LOG_DEFAULT_LEVEL)))
288+
(GET_ARG2(__VA_ARGS__, LOG_LEVEL)), \
289+
(GET_ARG2(__VA_ARGS__, CONFIG_LOG_DEFAULT_LEVEL)))
290290
#endif
291291

292292
/* Return first argument */
@@ -355,7 +355,7 @@ char *log_strdup(const char *str);
355355
#define LOG_MODULE_REGISTER(...) \
356356
_LOG_EVAL( \
357357
_LOG_LEVEL_RESOLVE(__VA_ARGS__), \
358-
(_LOG_MODULE_DATA_CREATE(_LOG_ARG1(__VA_ARGS__), \
358+
(_LOG_MODULE_DATA_CREATE(GET_ARG1(__VA_ARGS__), \
359359
_LOG_LEVEL_RESOLVE(__VA_ARGS__))),\
360360
()/*Empty*/ \
361361
) \
@@ -389,20 +389,20 @@ char *log_strdup(const char *str);
389389
*/
390390
#define LOG_MODULE_DECLARE(...) \
391391
extern const struct log_source_const_data \
392-
LOG_ITEM_CONST_DATA(_LOG_ARG1(__VA_ARGS__)); \
392+
LOG_ITEM_CONST_DATA(GET_ARG1(__VA_ARGS__)); \
393393
extern struct log_source_dynamic_data \
394-
LOG_ITEM_DYNAMIC_DATA(_LOG_ARG1(__VA_ARGS__)); \
394+
LOG_ITEM_DYNAMIC_DATA(GET_ARG1(__VA_ARGS__)); \
395395
\
396396
static const struct log_source_const_data * \
397397
__log_current_const_data __attribute__((unused)) = \
398398
_LOG_LEVEL_RESOLVE(__VA_ARGS__) ? \
399-
&LOG_ITEM_CONST_DATA(_LOG_ARG1(__VA_ARGS__)) : NULL; \
399+
&LOG_ITEM_CONST_DATA(GET_ARG1(__VA_ARGS__)) : NULL; \
400400
\
401401
static struct log_source_dynamic_data * \
402402
__log_current_dynamic_data __attribute__((unused)) = \
403403
(_LOG_LEVEL_RESOLVE(__VA_ARGS__) && \
404404
IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) ? \
405-
&LOG_ITEM_DYNAMIC_DATA(_LOG_ARG1(__VA_ARGS__)) : NULL;\
405+
&LOG_ITEM_DYNAMIC_DATA(GET_ARG1(__VA_ARGS__)) : NULL; \
406406
\
407407
static const u32_t __log_level __attribute__((unused)) = \
408408
_LOG_LEVEL_RESOLVE(__VA_ARGS__)

include/logging/log_core.h

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,14 @@ extern "C" {
3939
_LOG_RESOLVED_LEVEL1(_level, _default)
4040

4141
#define _LOG_RESOLVED_LEVEL1(_level, _default) \
42-
__LOG_RESOLVED_LEVEL2(_LOG_XXXX##_level, _level, _default)
42+
__COND_CODE(_LOG_XXXX##_level, (_level), (_default))
4343

4444
#define _LOG_XXXX0 _LOG_YYYY,
4545
#define _LOG_XXXX1 _LOG_YYYY,
4646
#define _LOG_XXXX2 _LOG_YYYY,
4747
#define _LOG_XXXX3 _LOG_YYYY,
4848
#define _LOG_XXXX4 _LOG_YYYY,
4949

50-
#define __LOG_RESOLVED_LEVEL2(one_or_two_args, _level, _default) \
51-
__LOG_ARG_2(one_or_two_args _level, _default)
52-
53-
#define LOG_DEBRACKET(...) __VA_ARGS__
54-
55-
#define __LOG_ARG_1(val, ...) val
56-
#define __LOG_ARG_2(ignore_this, val, ...) val
57-
#define __LOG_ARGS_LESS1(val, ...) __VA_ARGS__
58-
59-
#define __LOG_ARG_2_DEBRACKET(ignore_this, val, ...) LOG_DEBRACKET val
60-
6150
/**
6251
* @brief Macro for conditional code generation if provided log level allows.
6352
*
@@ -76,36 +65,13 @@ extern "C" {
7665
_LOG_EVAL1(_eval_level, _iftrue, _iffalse)
7766

7867
#define _LOG_EVAL1(_eval_level, _iftrue, _iffalse) \
79-
_LOG_EVAL2(_LOG_ZZZZ##_eval_level, _iftrue, _iffalse)
68+
__COND_CODE(_LOG_ZZZZ##_eval_level, _iftrue, _iffalse)
8069

8170
#define _LOG_ZZZZ1 _LOG_YYYY,
8271
#define _LOG_ZZZZ2 _LOG_YYYY,
8372
#define _LOG_ZZZZ3 _LOG_YYYY,
8473
#define _LOG_ZZZZ4 _LOG_YYYY,
8574

86-
#define _LOG_EVAL2(one_or_two_args, _iftrue, _iffalse) \
87-
__LOG_ARG_2_DEBRACKET(one_or_two_args _iftrue, _iffalse)
88-
89-
/**
90-
* @brief Macro for condition code generation.
91-
*
92-
* @param _eval Parameter evaluated against 0
93-
* @param _ifzero Code included if _eval is 0. Must be wrapped in brackets.
94-
* @param _ifnzero Code included if _eval is not 0.
95-
* Must be wrapped in brackets.
96-
*/
97-
98-
#define _LOG_Z_EVAL(_eval, _ifzero, _ifnzero) \
99-
_LOG_Z_EVAL1(_eval, _ifzero, _ifnzero)
100-
101-
#define _LOG_Z_EVAL1(_eval, _ifzero, _ifnzero) \
102-
_LOG_Z_EVAL2(_LOG_Z_ZZZZ##_eval, _ifzero, _ifnzero)
103-
104-
#define _LOG_Z_ZZZZ0 _LOG_Z_YYYY,
105-
106-
#define _LOG_Z_EVAL2(one_or_two_args, _ifzero, _ifnzero) \
107-
__LOG_ARG_2_DEBRACKET(one_or_two_args _ifzero, _ifnzero)
108-
10975
/** @brief Macro for getting log level for given module.
11076
*
11177
* It is evaluated to LOG_LEVEL if defined. Otherwise CONFIG_LOG_DEFAULT_LEVEL
@@ -156,7 +122,7 @@ extern "C" {
156122

157123
/**
158124
* @brief Macro for optional injection of function name as first argument of
159-
* formatted string. _LOG_Z_EVAL() macro is used to handle no arguments
125+
* formatted string. COND_CODE_0() macro is used to handle no arguments
160126
* case.
161127
*
162128
* The purpose of this macro is to prefix string literal with format
@@ -165,10 +131,10 @@ extern "C" {
165131
* used.
166132
*/
167133

168-
#define _LOG_STR(...) "%s: " __LOG_ARG_1(__VA_ARGS__), __func__\
169-
_LOG_Z_EVAL(NUM_VA_ARGS_LESS_1(__VA_ARGS__),\
134+
#define _LOG_STR(...) "%s: " GET_ARG1(__VA_ARGS__), __func__\
135+
COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__),\
170136
(),\
171-
(, __LOG_ARGS_LESS1(__VA_ARGS__))\
137+
(, GET_ARGS_LESS_1(__VA_ARGS__))\
172138
)
173139

174140

include/misc/util.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,20 @@ static inline s64_t arithmetic_shift_right(s64_t value, u8_t shift)
205205
*
206206
* This is based on same idea as @ref IS_ENABLED macro but as the result of
207207
* flag evaluation provided code is injected. Because preprocessor interprets
208-
* each comma as argument boundary, code must be provided in the brackets.
208+
* each comma as an argument boundary, code must be provided in the brackets.
209209
* Brackets are stripped away during macro processing.
210210
*
211211
* Usage example:
212212
*
213-
* #define MACRO(x) COND_CODE_1(CONFIG_FLAG, (u32_t x;), ())
213+
* \#define MACRO(x) COND_CODE_1(CONFIG_FLAG, (u32_t x;), ())
214214
*
215215
* It can be considered as alternative to:
216216
*
217-
* #if defined(CONFIG_FLAG) && (CONFIG_FLAG == 1)
218-
* #define MACRO(x) u32_t x;
219-
* #else
220-
* #define MACRO(x)
221-
* #endif
217+
* \#if defined(CONFIG_FLAG) && (CONFIG_FLAG == 1)
218+
* \#define MACRO(x) u32_t x;
219+
* \#else
220+
* \#define MACRO(x)
221+
* \#endif
222222
*
223223
* However, the advantage of that approach is that code is resolved in place
224224
* where it is used while \#if method resolves given macro when header is

0 commit comments

Comments
 (0)