Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
log_msg.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef ZEPHYR_INCLUDE_LOGGING_LOG_MSG_H_
7#define ZEPHYR_INCLUDE_LOGGING_LOG_MSG_H_
8
9#include <sys/atomic.h>
10#include <sys/util.h>
11#include <string.h>
12#include <logging/log_msg2.h>
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
29typedef unsigned long log_arg_t;
30
35#define LOG_MAX_NARGS 15
36
38#ifdef CONFIG_64BIT
39#define LOG_MSG_NARGS_SINGLE_CHUNK 4U
40#else
41#define LOG_MSG_NARGS_SINGLE_CHUNK 3U
42#endif
43
45#define LOG_MSG_NARGS_HEAD_CHUNK \
46 (LOG_MSG_NARGS_SINGLE_CHUNK - (sizeof(void *)/sizeof(log_arg_t)))
47
50#define LOG_MSG_HEXDUMP_BYTES_SINGLE_CHUNK \
51 (LOG_MSG_NARGS_SINGLE_CHUNK * sizeof(log_arg_t))
52
56#define LOG_MSG_HEXDUMP_BYTES_HEAD_CHUNK \
57 (LOG_MSG_HEXDUMP_BYTES_SINGLE_CHUNK - sizeof(void *))
58
62#define HEXDUMP_BYTES_CONT_MSG \
63 (sizeof(struct log_msg) - sizeof(void *))
64
65#define ARGS_CONT_MSG (HEXDUMP_BYTES_CONT_MSG / sizeof(log_arg_t))
66
68#define LOG_MSG_TYPE_STD 0U
69
71#define LOG_MSG_TYPE_HEXDUMP 1
72
74#define COMMON_PARAM_HDR() \
75 uint16_t type : 1; \
76 uint16_t ext : 1
77
79#define LOG_MSG_HEXDUMP_LENGTH_BITS 14
80
82#define LOG_MSG_HEXDUMP_MAX_LENGTH (BIT(LOG_MSG_HEXDUMP_LENGTH_BITS) - 1)
83
89};
90
95};
96
102};
103
108};
109
121};
122
127};
128
136};
137
139struct log_msg {
140 struct log_msg *next;
142 const char *str;
147};
148
156};
157
160 struct log_msg head;
162};
163
166
174void log_msg_get(struct log_msg *msg);
175
183void log_msg_put(struct log_msg *msg);
184
191static inline uint32_t log_msg_domain_id_get(struct log_msg *msg)
192{
193 return msg->hdr.ids.domain_id;
194}
195
202static inline uint32_t log_msg_source_id_get(struct log_msg *msg)
203{
204 return msg->hdr.ids.source_id;
205}
206
213static inline uint32_t log_msg_level_get(struct log_msg *msg)
214{
215 return msg->hdr.ids.level;
216}
217
224static inline uint32_t log_msg_timestamp_get(struct log_msg *msg)
225{
226 return msg->hdr.timestamp;
227}
228
236static inline bool log_msg_is_std(struct log_msg *msg)
237{
238 return (msg->hdr.params.generic.type == LOG_MSG_TYPE_STD);
239}
240
248
258
259
266const char *log_msg_str_get(struct log_msg *msg);
267
283 const uint8_t *data,
284 uint32_t length);
285
294 uint8_t *data,
295 size_t *length,
296 size_t offset);
297
306 uint8_t *data,
307 size_t *length,
308 size_t offset);
309
311
317
322static inline struct log_msg *z_log_msg_std_alloc(void)
323{
324 struct log_msg *msg = (struct log_msg *)log_msg_chunk_alloc();
325
326 if (msg != NULL) {
327 /* all fields reset to 0, reference counter to 1 */
328 msg->hdr.ref_cnt = 1;
329 msg->hdr.params.raw = 0U;
331
332 if (IS_ENABLED(CONFIG_USERSPACE)) {
333 /* it may be used in msg_free() function. */
334 msg->hdr.ids.level = 0;
335 msg->hdr.ids.domain_id = 0;
336 msg->hdr.ids.source_id = 0;
337 }
338 }
339
340 return msg;
341}
342
351static inline struct log_msg *log_msg_create_0(const char *str)
352{
353 struct log_msg *msg = z_log_msg_std_alloc();
354
355 if (msg != NULL) {
356 msg->str = str;
357 }
358
359 return msg;
360}
361
375static inline struct log_msg *log_msg_create_1(const char *str,
376 log_arg_t arg1)
377{
378 struct log_msg *msg = z_log_msg_std_alloc();
379
380 if (msg != NULL) {
381 msg->str = str;
382 msg->hdr.params.std.nargs = 1U;
383 msg->payload.single.args[0] = arg1;
384 }
385
386 return msg;
387}
388
403static inline struct log_msg *log_msg_create_2(const char *str,
404 log_arg_t arg1,
405 log_arg_t arg2)
406{
407 struct log_msg *msg = z_log_msg_std_alloc();
408
409 if (msg != NULL) {
410 msg->str = str;
411 msg->hdr.params.std.nargs = 2U;
412 msg->payload.single.args[0] = arg1;
413 msg->payload.single.args[1] = arg2;
414 }
415
416 return msg;
417}
418
434static inline struct log_msg *log_msg_create_3(const char *str,
435 log_arg_t arg1,
436 log_arg_t arg2,
437 log_arg_t arg3)
438{
439 struct log_msg *msg = z_log_msg_std_alloc();
440
441 if (msg != NULL) {
442 msg->str = str;
443 msg->hdr.params.std.nargs = 3U;
444 msg->payload.single.args[0] = arg1;
445 msg->payload.single.args[1] = arg2;
446 msg->payload.single.args[2] = arg3;
447 }
448
449 return msg;
450}
451
466struct log_msg *log_msg_create_n(const char *str,
467 log_arg_t *args,
468 uint32_t nargs);
469
474
479
484
485
490#ifdef __cplusplus
491}
492#endif
493
494#endif /* ZEPHYR_INCLUDE_LOGGING_LOG_MSG_H_ */
int atomic_t
Definition: atomic.h:21
void log_msg_pool_init(void)
Function for initialization of the log message pool.
static uint32_t log_msg_level_get(struct log_msg *msg)
Get severity level of the message.
Definition: log_msg.h:213
#define LOG_MSG_NARGS_SINGLE_CHUNK
Number of arguments in the log entry which fits in one chunk.
Definition: log_msg.h:41
#define LOG_MSG_HEXDUMP_LENGTH_BITS
Number of bits used for storing length of hexdump log message.
Definition: log_msg.h:79
#define LOG_MSG_HEXDUMP_BYTES_HEAD_CHUNK
Number of bytes in the first chunk of hexdump message if message consists of more than one chunk.
Definition: log_msg.h:56
static uint32_t log_msg_timestamp_get(struct log_msg *msg)
Get timestamp of the message.
Definition: log_msg.h:224
#define COMMON_PARAM_HDR()
Common part of log message header.
Definition: log_msg.h:74
static struct log_msg * log_msg_create_3(const char *str, log_arg_t arg1, log_arg_t arg2, log_arg_t arg3)
Create standard log message with three arguments.
Definition: log_msg.h:434
static uint32_t log_msg_domain_id_get(struct log_msg *msg)
Get domain ID of the message.
Definition: log_msg.h:191
void log_msg_hexdump_data_put(struct log_msg *msg, uint8_t *data, size_t *length, size_t offset)
Put data into hexdump log message.
unsigned long log_arg_t
Log argument type.
Definition: log_msg.h:29
const char * log_msg_str_get(struct log_msg *msg)
Gets pointer to the unformatted string from standard log message.
#define HEXDUMP_BYTES_CONT_MSG
Number of bytes that can be stored in chunks following head chunk in hexdump log message.
Definition: log_msg.h:62
static uint32_t log_msg_source_id_get(struct log_msg *msg)
Get source ID (module or instance) of the message.
Definition: log_msg.h:202
union log_msg_chunk * log_msg_no_space_handle(void)
uint32_t log_msg_nargs_get(struct log_msg *msg)
Returns number of arguments in standard log message.
void log_msg_hexdump_data_get(struct log_msg *msg, uint8_t *data, size_t *length, size_t offset)
Get data from hexdump log message.
static bool log_msg_is_std(struct log_msg *msg)
Check if message is of standard type.
Definition: log_msg.h:236
struct log_msg * log_msg_hexdump_create(const char *str, const uint8_t *data, uint32_t length)
Allocates chunks for hexdump message and copies the data.
static struct log_msg * log_msg_create_2(const char *str, log_arg_t arg1, log_arg_t arg2)
Create standard log message with two arguments.
Definition: log_msg.h:403
static struct log_msg * log_msg_create_0(const char *str)
Create standard log message with no arguments.
Definition: log_msg.h:351
void log_msg_put(struct log_msg *msg)
Function for indicating that message is no longer in use.
uint32_t log_msg_mem_get_used(void)
Get number of used blocks from the log mem pool.
log_arg_t log_msg_arg_get(struct log_msg *msg, uint32_t arg_idx)
Gets argument from standard log message.
#define LOG_MSG_HEXDUMP_BYTES_SINGLE_CHUNK
Maximal amount of bytes in the hexdump entry which fits in one chunk.
Definition: log_msg.h:50
void log_msg_get(struct log_msg *msg)
Function for indicating that message is in use.
#define ARGS_CONT_MSG
Definition: log_msg.h:65
#define LOG_MSG_NARGS_HEAD_CHUNK
Number of arguments in the head of extended standard log message..
Definition: log_msg.h:45
struct log_msg * log_msg_create_n(const char *str, log_arg_t *args, uint32_t nargs)
Create standard log message with variable number of arguments.
union log_msg_chunk * log_msg_chunk_alloc(void)
Allocate single chunk from the pool.
uint32_t log_msg_mem_get_free(void)
Get number of free blocks from the log mem pool.
#define LOG_MSG_TYPE_STD
Flag indicating standard log message.
Definition: log_msg.h:68
static struct log_msg * log_msg_create_1(const char *str, log_arg_t arg1)
Create standard log message with one argument.
Definition: log_msg.h:375
uint32_t log_msg_mem_get_max_used(void)
Get max used blocks from the log mem pool.
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition: util_macro.h:101
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
Chunks following message head when message is extended.
Definition: log_msg.h:150
union log_msg_cont::log_msg_cont_data payload
struct log_msg_cont * next
Definition: log_msg.h:151
Data part of extended log message.
Definition: log_msg.h:130
union log_msg_ext_head_data::log_msg_ext_head_data_data data
struct log_msg_cont * next
Definition: log_msg.h:131
Definition: log_msg.h:92
uint16_t reserved
Definition: log_msg.h:94
uint16_t type
Definition: log_msg.h:93
Definition: log_msg.h:111
uint32_t timestamp
Definition: log_msg.h:120
atomic_t ref_cnt
Definition: log_msg.h:112
union log_msg_hdr::log_msg_hdr_params params
struct log_msg_ids ids
Definition: log_msg.h:119
Definition: log_msg.h:105
uint16_t length
Definition: log_msg.h:107
Part of log message header identifying source and level.
Definition: log_msg.h:85
uint16_t level
Definition: log_msg.h:86
uint16_t domain_id
Definition: log_msg.h:87
uint16_t source_id
Definition: log_msg.h:88
Definition: log_msg.h:98
uint16_t reserved
Definition: log_msg.h:100
uint16_t type
Definition: log_msg.h:99
uint16_t nargs
Definition: log_msg.h:101
Log message structure.
Definition: log_msg.h:139
union log_msg::log_msg_data payload
const char * str
Definition: log_msg.h:142
struct log_msg_hdr hdr
Definition: log_msg.h:141
struct log_msg * next
Definition: log_msg.h:140
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
Definition: log_msg.h:143
struct log_msg_ext_head_data ext
Definition: log_msg.h:145
union log_msg_head_data single
Definition: log_msg.h:144
Log message.
Definition: log_msg.h:159
struct log_msg head
Definition: log_msg.h:160
struct log_msg_cont cont
Definition: log_msg.h:161
Definition: log_msg.h:152
uint8_t bytes[(sizeof(struct log_msg) - sizeof(void *))]
Definition: log_msg.h:154
log_arg_t args[((sizeof(struct log_msg) - sizeof(void *))/sizeof(log_arg_t))]
Definition: log_msg.h:153
log_arg_t args[(3U -(sizeof(void *)/sizeof(log_arg_t)))]
Definition: log_msg.h:133
uint8_t bytes[((3U *sizeof(log_arg_t)) - sizeof(void *))]
Definition: log_msg.h:134
Definition: log_msg.h:113
uint16_t raw
Definition: log_msg.h:117
struct log_msg_generic_hdr generic
Definition: log_msg.h:114
struct log_msg_hexdump_hdr hexdump
Definition: log_msg.h:116
struct log_msg_std_hdr std
Definition: log_msg.h:115
Data part of log message.
Definition: log_msg.h:124
uint8_t bytes[(3U *sizeof(log_arg_t))]
Definition: log_msg.h:126
log_arg_t args[3U]
Definition: log_msg.h:125
Misc utilities.