Zephyr API Documentation  3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
log_backend.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_BACKEND_H_
7#define ZEPHYR_INCLUDE_LOGGING_LOG_BACKEND_H_
8
10#include <stdarg.h>
11#include <zephyr/sys/__assert.h>
12#include <zephyr/sys/util.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
27/* Forward declaration of the log_backend type. */
28struct log_backend;
29
30
47
50};
51
57 void *raw;
58};
59
64 void (*process)(const struct log_backend *const backend,
65 union log_msg_generic *msg);
66
67 void (*dropped)(const struct log_backend *const backend, uint32_t cnt);
68 void (*panic)(const struct log_backend *const backend);
69 void (*init)(const struct log_backend *const backend);
70 int (*is_ready)(const struct log_backend *const backend);
71 int (*format_set)(const struct log_backend *const backend,
72 uint32_t log_type);
73
74 void (*notify)(const struct log_backend *const backend,
75 enum log_backend_evt event,
76 union log_backend_evt_arg *arg);
77};
78
83 void *ctx;
85 bool active;
86
87 /* Initialization level. */
89};
90
95 const struct log_backend_api *api;
97 const char *name;
99};
100
110#define LOG_BACKEND_DEFINE(_name, _api, _autostart, ...) \
111 static struct log_backend_control_block UTIL_CAT(backend_cb_, _name) = \
112 { \
113 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
114 (), (.ctx = __VA_ARGS__,)) \
115 .id = 0, \
116 .active = false, \
117 }; \
118 static const STRUCT_SECTION_ITERABLE(log_backend, _name) = \
119 { \
120 .api = &_api, \
121 .cb = &UTIL_CAT(backend_cb_, _name), \
122 .name = STRINGIFY(_name), \
123 .autostart = _autostart \
124 }
125
126
137static inline void log_backend_init(const struct log_backend *const backend)
138{
139 __ASSERT_NO_MSG(backend != NULL);
140 if (backend->api->init) {
141 backend->api->init(backend);
142 }
143}
144
156static inline int log_backend_is_ready(const struct log_backend *const backend)
157{
158 __ASSERT_NO_MSG(backend != NULL);
159 if (backend->api->is_ready != NULL) {
160 return backend->api->is_ready(backend);
161 }
162
163 return 0;
164}
165
175static inline void log_backend_msg_process(const struct log_backend *const backend,
176 union log_msg_generic *msg)
177{
178 __ASSERT_NO_MSG(backend != NULL);
179 __ASSERT_NO_MSG(msg != NULL);
180 backend->api->process(backend, msg);
181}
182
191static inline void log_backend_dropped(const struct log_backend *const backend,
192 uint32_t cnt)
193{
194 __ASSERT_NO_MSG(backend != NULL);
195
196 if (backend->api->dropped != NULL) {
197 backend->api->dropped(backend, cnt);
198 }
199}
200
206static inline void log_backend_panic(const struct log_backend *const backend)
207{
208 __ASSERT_NO_MSG(backend != NULL);
209 backend->api->panic(backend);
210}
211
220static inline void log_backend_id_set(const struct log_backend *const backend,
221 uint8_t id)
222{
223 __ASSERT_NO_MSG(backend != NULL);
224 backend->cb->id = id;
225}
226
235static inline uint8_t log_backend_id_get(const struct log_backend *const backend)
236{
237 __ASSERT_NO_MSG(backend != NULL);
238 return backend->cb->id;
239}
240
248static inline const struct log_backend *log_backend_get(uint32_t idx)
249{
250 const struct log_backend *backend;
251
252 STRUCT_SECTION_GET(log_backend, idx, &backend);
253
254 return backend;
255}
256
262static inline int log_backend_count_get(void)
263{
264 int cnt;
265
267
268 return cnt;
269}
270
277static inline void log_backend_activate(const struct log_backend *const backend,
278 void *ctx)
279{
280 __ASSERT_NO_MSG(backend != NULL);
281 backend->cb->ctx = ctx;
282 backend->cb->active = true;
283}
284
290static inline void log_backend_deactivate(
291 const struct log_backend *const backend)
292{
293 __ASSERT_NO_MSG(backend != NULL);
294 backend->cb->active = false;
295}
296
304static inline bool log_backend_is_active(
305 const struct log_backend *const backend)
306{
307 __ASSERT_NO_MSG(backend != NULL);
308 return backend->cb->active;
309}
310
320static inline int log_backend_format_set(const struct log_backend *backend, uint32_t log_type)
321{
322 extern size_t log_format_table_size(void);
323
324 if ((size_t)log_type >= log_format_table_size()) {
325 return -EINVAL;
326 }
327
328 if (log_format_func_t_get(log_type) == NULL) {
329 return -EINVAL;
330 }
331
332 if (backend == NULL) {
333 return -EINVAL;
334 }
335
336 if (backend->api->format_set == NULL) {
337 return -ENOTSUP;
338 }
339
340 return backend->api->format_set(backend, log_type);
341}
342
350static inline void log_backend_notify(const struct log_backend *const backend,
351 enum log_backend_evt event,
352 union log_backend_evt_arg *arg)
353{
354 __ASSERT_NO_MSG(backend != NULL);
355
356 if (backend->api->notify) {
357 backend->api->notify(backend, event, arg);
358 }
359}
360
365#ifdef __cplusplus
366}
367#endif
368
369#endif /* ZEPHYR_INCLUDE_LOGGING_LOG_BACKEND_H_ */
#define STRUCT_SECTION_GET(struct_type, i, dst)
Get element from section.
Definition: iterable_sections.h:282
#define STRUCT_SECTION_COUNT(struct_type, dst)
Count elements in a section.
Definition: iterable_sections.h:291
static void log_backend_init(const struct log_backend *const backend)
Initialize or initiate the logging backend.
Definition: log_backend.h:137
log_backend_evt
Backend events.
Definition: log_backend.h:34
static void log_backend_deactivate(const struct log_backend *const backend)
Deactivate backend.
Definition: log_backend.h:290
static void log_backend_msg_process(const struct log_backend *const backend, union log_msg_generic *msg)
Process message.
Definition: log_backend.h:175
static void log_backend_activate(const struct log_backend *const backend, void *ctx)
Activate backend.
Definition: log_backend.h:277
static bool log_backend_is_active(const struct log_backend *const backend)
Check state of the backend.
Definition: log_backend.h:304
static void log_backend_id_set(const struct log_backend *const backend, uint8_t id)
Set backend id.
Definition: log_backend.h:220
static void log_backend_dropped(const struct log_backend *const backend, uint32_t cnt)
Notify backend about dropped log messages.
Definition: log_backend.h:191
static int log_backend_format_set(const struct log_backend *backend, uint32_t log_type)
Set logging format.
Definition: log_backend.h:320
static int log_backend_count_get(void)
Get number of backends.
Definition: log_backend.h:262
static void log_backend_panic(const struct log_backend *const backend)
Reconfigure backend to panic mode.
Definition: log_backend.h:206
static uint8_t log_backend_id_get(const struct log_backend *const backend)
Get backend id.
Definition: log_backend.h:235
static void log_backend_notify(const struct log_backend *const backend, enum log_backend_evt event, union log_backend_evt_arg *arg)
Notify a backend of an event.
Definition: log_backend.h:350
static const struct log_backend * log_backend_get(uint32_t idx)
Get backend.
Definition: log_backend.h:248
static int log_backend_is_ready(const struct log_backend *const backend)
Poll for backend readiness.
Definition: log_backend.h:156
@ LOG_BACKEND_EVT_PROCESS_THREAD_DONE
Event when process thread finishes processing.
Definition: log_backend.h:46
@ LOG_BACKEND_EVT_MAX
Maximum number of backend events.
Definition: log_backend.h:49
log_format_func_t log_format_func_t_get(uint32_t log_type)
Declaration of the get routine for function pointer table format_table.
#define EINVAL
Invalid argument.
Definition: errno.h:60
#define ENOTSUP
Unsupported value.
Definition: errno.h:114
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
Logger backend API.
Definition: log_backend.h:63
void(* panic)(const struct log_backend *const backend)
Definition: log_backend.h:68
void(* process)(const struct log_backend *const backend, union log_msg_generic *msg)
Definition: log_backend.h:64
void(* notify)(const struct log_backend *const backend, enum log_backend_evt event, union log_backend_evt_arg *arg)
Definition: log_backend.h:74
void(* dropped)(const struct log_backend *const backend, uint32_t cnt)
Definition: log_backend.h:67
int(* is_ready)(const struct log_backend *const backend)
Definition: log_backend.h:70
void(* init)(const struct log_backend *const backend)
Definition: log_backend.h:69
int(* format_set)(const struct log_backend *const backend, uint32_t log_type)
Definition: log_backend.h:71
Logger backend control block.
Definition: log_backend.h:82
uint8_t id
Definition: log_backend.h:84
bool active
Definition: log_backend.h:85
uint8_t level
Definition: log_backend.h:88
void * ctx
Definition: log_backend.h:83
Logger backend structure.
Definition: log_backend.h:94
const char * name
Definition: log_backend.h:97
const struct log_backend_api * api
Definition: log_backend.h:95
struct log_backend_control_block * cb
Definition: log_backend.h:96
bool autostart
Definition: log_backend.h:98
Argument(s) for backend events.
Definition: log_backend.h:55
void * raw
Unspecified argument(s).
Definition: log_backend.h:57
Definition: log_msg.h:117
Misc utilities.