Skip to content
Permalink
Browse files

log facility: make its arguments 64-bit compatible

Log arguments were hardcoded to u32_t values. On 64-bit systems, this
is rather restrictive. To make things clear, arguments now have their
own type, log_arg_t, which now can be adjusted in only one location
if need be.  It is currently defined as unsigned long whose effective
width is equivalent to u32_t on 32-bit systems, and u64_t on 64-bit
systems.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
  • Loading branch information...
Nicolas Pitre authored and aescolar committed May 21, 2019
1 parent 344737d commit 0567f161d8f59ea429ea344f11e1c23f18471df8
@@ -179,22 +179,22 @@ extern "C" {
log_0(_str, _src_level)

#define _LOG_INTERNAL_1(_src_level, _str, _arg0) \
log_1(_str, (u32_t)(_arg0), _src_level)
log_1(_str, (log_arg_t)(_arg0), _src_level)

#define _LOG_INTERNAL_2(_src_level, _str, _arg0, _arg1) \
log_2(_str, (u32_t)(_arg0), (u32_t)(_arg1), _src_level)
log_2(_str, (log_arg_t)(_arg0), (log_arg_t)(_arg1), _src_level)

#define _LOG_INTERNAL_3(_src_level, _str, _arg0, _arg1, _arg2) \
log_3(_str, (u32_t)(_arg0), (u32_t)(_arg1), (u32_t)(_arg2), _src_level)
log_3(_str, (log_arg_t)(_arg0), (log_arg_t)(_arg1), (log_arg_t)(_arg2), _src_level)

#define __LOG_ARG_CAST(_x) (u32_t)(_x),
#define __LOG_ARG_CAST(_x) (log_arg_t)(_x),

#define __LOG_ARGUMENTS(...) MACRO_MAP(__LOG_ARG_CAST, __VA_ARGS__)

#define _LOG_INTERNAL_LONG(_src_level, _str, ...) \
do { \
u32_t args[] = {__LOG_ARGUMENTS(__VA_ARGS__)}; \
log_n(_str, args, ARRAY_SIZE(args), _src_level); \
#define _LOG_INTERNAL_LONG(_src_level, _str, ...) \
do { \
log_arg_t args[] = {__LOG_ARGUMENTS(__VA_ARGS__)};\
log_n(_str, args, ARRAY_SIZE(args), _src_level); \
} while (false)

#define Z_LOG_LEVEL_CHECK(_level, _check_level, _default_level) \
@@ -442,7 +442,7 @@ void log_0(const char *str, struct log_msg_ids src_level);
* @param src_level Log identification.
*/
void log_1(const char *str,
u32_t arg1,
log_arg_t arg1,
struct log_msg_ids src_level);

/** @brief Standard log with two arguments.
@@ -453,8 +453,8 @@ void log_1(const char *str,
* @param src_level Log identification.
*/
void log_2(const char *str,
u32_t arg1,
u32_t arg2,
log_arg_t arg1,
log_arg_t arg2,
struct log_msg_ids src_level);

/** @brief Standard log with three arguments.
@@ -466,9 +466,9 @@ void log_2(const char *str,
* @param src_level Log identification.
*/
void log_3(const char *str,
u32_t arg1,
u32_t arg2,
u32_t arg3,
log_arg_t arg1,
log_arg_t arg2,
log_arg_t arg3,
struct log_msg_ids src_level);

/** @brief Standard log with arguments list.
@@ -479,7 +479,7 @@ void log_3(const char *str,
* @param src_level Log identification.
*/
void log_n(const char *str,
u32_t *args,
log_arg_t *args,
u32_t narg,
struct log_msg_ids src_level);

@@ -22,6 +22,12 @@ extern "C" {
* @{
*/

/** @brief Log argument type.
*
* Should preferably be equivalent to a native word size.
*/
typedef unsigned long log_arg_t;

/** @brief Maximum number of arguments in the standard log entry.
*
* It is limited by 4 bit nargs field in the log message.
@@ -123,15 +129,15 @@ struct log_msg_hdr {

/** @brief Data part of log message. */
union log_msg_head_data {
u32_t args[LOG_MSG_NARGS_SINGLE_CHUNK];
log_arg_t args[LOG_MSG_NARGS_SINGLE_CHUNK];
u8_t bytes[LOG_MSG_HEXDUMP_BYTES_SINGLE_CHUNK];
};

/** @brief Data part of extended log message. */
struct log_msg_ext_head_data {
struct log_msg_cont *next;
union log_msg_ext_head_data_data {
u32_t args[LOG_MSG_NARGS_HEAD_CHUNK];
log_arg_t args[LOG_MSG_NARGS_HEAD_CHUNK];
u8_t bytes[LOG_MSG_HEXDUMP_BYTES_HEAD_CHUNK];
} data;
};
@@ -155,7 +161,7 @@ BUILD_ASSERT_MSG((sizeof(union log_msg_head_data) ==
struct log_msg_cont {
struct log_msg_cont *next; /*!< Pointer to the next chunk. */
union log_msg_cont_data {
u32_t args[ARGS_CONT_MSG];
log_arg_t args[ARGS_CONT_MSG];
u8_t bytes[HEXDUMP_BYTES_CONT_MSG];
} payload;
};
@@ -261,7 +267,7 @@ u32_t log_msg_nargs_get(struct log_msg *msg);
* @return Argument value or 0 if arg_idx exceeds number of arguments in the
* message.
*/
u32_t log_msg_arg_get(struct log_msg *msg, u32_t arg_idx);
log_arg_t log_msg_arg_get(struct log_msg *msg, u32_t arg_idx);


/** @brief Gets pointer to the unformatted string from standard log message.
@@ -379,7 +385,7 @@ static inline struct log_msg *log_msg_create_0(const char *str)
* @return Pointer to allocated head of the message or NULL.
*/
static inline struct log_msg *log_msg_create_1(const char *str,
u32_t arg1)
log_arg_t arg1)
{
struct log_msg *msg = z_log_msg_std_alloc();

@@ -407,8 +413,8 @@ static inline struct log_msg *log_msg_create_1(const char *str,
* @return Pointer to allocated head of the message or NULL.
*/
static inline struct log_msg *log_msg_create_2(const char *str,
u32_t arg1,
u32_t arg2)
log_arg_t arg1,
log_arg_t arg2)
{
struct log_msg *msg = z_log_msg_std_alloc();

@@ -438,9 +444,9 @@ static inline struct log_msg *log_msg_create_2(const char *str,
* @return Pointer to allocated head of the message or NULL.
*/
static inline struct log_msg *log_msg_create_3(const char *str,
u32_t arg1,
u32_t arg2,
u32_t arg3)
log_arg_t arg1,
log_arg_t arg2,
log_arg_t arg3)
{
struct log_msg *msg = z_log_msg_std_alloc();

@@ -470,7 +476,7 @@ static inline struct log_msg *log_msg_create_3(const char *str,
* @return Pointer to allocated head of the message or NULL.
*/
struct log_msg *log_msg_create_n(const char *str,
u32_t *args,
log_arg_t *args,
u32_t nargs);

/**
@@ -225,7 +225,7 @@ void log_0(const char *str, struct log_msg_ids src_level)
}

void log_1(const char *str,
u32_t arg0,
log_arg_t arg0,
struct log_msg_ids src_level)
{
struct log_msg *msg = log_msg_create_1(str, arg0);
@@ -237,8 +237,8 @@ void log_1(const char *str,
}

void log_2(const char *str,
u32_t arg0,
u32_t arg1,
log_arg_t arg0,
log_arg_t arg1,
struct log_msg_ids src_level)
{
struct log_msg *msg = log_msg_create_2(str, arg0, arg1);
@@ -251,9 +251,9 @@ void log_2(const char *str,
}

void log_3(const char *str,
u32_t arg0,
u32_t arg1,
u32_t arg2,
log_arg_t arg0,
log_arg_t arg1,
log_arg_t arg2,
struct log_msg_ids src_level)
{
struct log_msg *msg = log_msg_create_3(str, arg0, arg1, arg2);
@@ -266,7 +266,7 @@ void log_3(const char *str,
}

void log_n(const char *str,
u32_t *args,
log_arg_t *args,
u32_t narg,
struct log_msg_ids src_level)
{
@@ -362,11 +362,11 @@ void log_generic(struct log_msg_ids src_level, const char *fmt, va_list ap)
}
}
} else {
u32_t args[LOG_MAX_NARGS];
log_arg_t args[LOG_MAX_NARGS];
u32_t nargs = count_args(fmt);

for (int i = 0; i < nargs; i++) {
args[i] = va_arg(ap, u32_t);
args[i] = va_arg(ap, log_arg_t);
}

log_n(fmt, args, nargs, src_level);
@@ -99,7 +99,7 @@ u32_t log_msg_nargs_get(struct log_msg *msg)
return msg->hdr.params.std.nargs;
}

static u32_t cont_arg_get(struct log_msg *msg, u32_t arg_idx)
static log_arg_t cont_arg_get(struct log_msg *msg, u32_t arg_idx)
{
struct log_msg_cont *cont;

@@ -119,9 +119,9 @@ static u32_t cont_arg_get(struct log_msg *msg, u32_t arg_idx)
return cont->payload.args[arg_idx];
}

u32_t log_msg_arg_get(struct log_msg *msg, u32_t arg_idx)
log_arg_t log_msg_arg_get(struct log_msg *msg, u32_t arg_idx)
{
u32_t arg;
log_arg_t arg;

/* Return early if requested argument not present in the message. */
if (arg_idx >= msg->hdr.params.std.nargs) {
@@ -186,33 +186,33 @@ static struct log_msg *msg_alloc(u32_t nargs)
return msg;
}

static void copy_args_to_msg(struct log_msg *msg, u32_t *args, u32_t nargs)
static void copy_args_to_msg(struct log_msg *msg, log_arg_t *args, u32_t nargs)
{
struct log_msg_cont *cont = msg->payload.ext.next;

if (nargs > LOG_MSG_NARGS_SINGLE_CHUNK) {
(void)memcpy(msg->payload.ext.data.args, args,
LOG_MSG_NARGS_HEAD_CHUNK * sizeof(u32_t));
LOG_MSG_NARGS_HEAD_CHUNK * sizeof(log_arg_t));
nargs -= LOG_MSG_NARGS_HEAD_CHUNK;
args += LOG_MSG_NARGS_HEAD_CHUNK;
} else {
(void)memcpy(msg->payload.single.args, args,
nargs * sizeof(u32_t));
nargs * sizeof(log_arg_t));
nargs = 0U;
}

while (nargs != 0U) {
u32_t cpy_args = MIN(nargs, ARGS_CONT_MSG);

(void)memcpy(cont->payload.args, args,
cpy_args * sizeof(u32_t));
cpy_args * sizeof(log_arg_t));
nargs -= cpy_args;
args += cpy_args;
cont = cont->next;
}
}

struct log_msg *log_msg_create_n(const char *str, u32_t *args, u32_t nargs)
struct log_msg *log_msg_create_n(const char *str, log_arg_t *args, u32_t nargs)
{
__ASSERT_NO_MSG(nargs < LOG_MAX_NARGS);

@@ -24,7 +24,7 @@ void test_log_std_msg(void)
"test assumes following setting");

u32_t used_slabs = k_mem_slab_num_used_get(&log_msg_pool);
u32_t args[] = {1, 2, 3, 4, 5, 6};
log_arg_t args[] = {1, 2, 3, 4, 5, 6};
struct log_msg *msg;

/* allocation of 0 argument fits in single buffer */

0 comments on commit 0567f16

Please sign in to comment.
You can’t perform that action at this time.