Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
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
9#include <logging/log_msg.h>
10#include <logging/log_msg2.h>
11#include <stdarg.h>
12#include <sys/__assert.h>
13#include <sys/util.h>
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
26/* Forward declaration of the log_backend type. */
27struct log_backend;
28
33 void (*process)(const struct log_backend *const backend,
34 union log_msg2_generic *msg);
35
36 void (*put)(const struct log_backend *const backend,
37 struct log_msg *msg);
38 void (*put_sync_string)(const struct log_backend *const backend,
39 struct log_msg_ids src_level, uint32_t timestamp,
40 const char *fmt, va_list ap);
41 void (*put_sync_hexdump)(const struct log_backend *const backend,
42 struct log_msg_ids src_level, uint32_t timestamp,
43 const char *metadata, const uint8_t *data, uint32_t len);
44
45 void (*dropped)(const struct log_backend *const backend, uint32_t cnt);
46 void (*panic)(const struct log_backend *const backend);
47 void (*init)(const struct log_backend *const backend);
48};
49
54 void *ctx;
56 bool active;
57};
58
63 const struct log_backend_api *api;
65 const char *name;
67};
68
69extern const struct log_backend __log_backends_start[];
70extern const struct log_backend __log_backends_end[];
71
81#define LOG_BACKEND_DEFINE(_name, _api, _autostart, ...) \
82 static struct log_backend_control_block UTIL_CAT(backend_cb_, _name) = \
83 { \
84 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
85 (), (.ctx = __VA_ARGS__,)) \
86 .id = 0, \
87 .active = false, \
88 }; \
89 static const STRUCT_SECTION_ITERABLE(log_backend, _name) = \
90 { \
91 .api = &_api, \
92 .cb = &UTIL_CAT(backend_cb_, _name), \
93 .name = STRINGIFY(_name), \
94 .autostart = _autostart \
95 }
96
97
104static inline void log_backend_put(const struct log_backend *const backend,
105 struct log_msg *msg)
106{
107 __ASSERT_NO_MSG(backend != NULL);
108 __ASSERT_NO_MSG(msg != NULL);
109 backend->api->put(backend, msg);
110}
111
112static inline void log_backend_msg2_process(
113 const struct log_backend *const backend,
114 union log_msg2_generic *msg)
115{
116 __ASSERT_NO_MSG(backend != NULL);
117 __ASSERT_NO_MSG(msg != NULL);
118 backend->api->process(backend, msg);
119}
120
121
132 const struct log_backend *const backend,
133 struct log_msg_ids src_level,
134 uint32_t timestamp, const char *fmt,
135 va_list ap)
136{
137 __ASSERT_NO_MSG(backend != NULL);
138
139 if (backend->api->put_sync_string) {
140 backend->api->put_sync_string(backend, src_level,
141 timestamp, fmt, ap);
142 }
143}
144
156 const struct log_backend *const backend,
157 struct log_msg_ids src_level,
158 uint32_t timestamp, const char *metadata,
159 const uint8_t *data, uint32_t len)
160{
161 __ASSERT_NO_MSG(backend != NULL);
162
163 if (backend->api->put_sync_hexdump) {
164 backend->api->put_sync_hexdump(backend, src_level, timestamp,
165 metadata, data, len);
166 }
167}
168
177static inline void log_backend_dropped(const struct log_backend *const backend,
178 uint32_t cnt)
179{
180 __ASSERT_NO_MSG(backend != NULL);
181
182 if (backend->api->dropped != NULL) {
183 backend->api->dropped(backend, cnt);
184 }
185}
186
192static inline void log_backend_panic(const struct log_backend *const backend)
193{
194 __ASSERT_NO_MSG(backend != NULL);
195 backend->api->panic(backend);
196}
197
206static inline void log_backend_id_set(const struct log_backend *const backend,
207 uint8_t id)
208{
209 __ASSERT_NO_MSG(backend != NULL);
210 backend->cb->id = id;
211}
212
221static inline uint8_t log_backend_id_get(const struct log_backend *const backend)
222{
223 __ASSERT_NO_MSG(backend != NULL);
224 return backend->cb->id;
225}
226
234static inline const struct log_backend *log_backend_get(uint32_t idx)
235{
236 return &__log_backends_start[idx];
237}
238
244static inline int log_backend_count_get(void)
245{
246 return __log_backends_end - __log_backends_start;
247}
248
255static inline void log_backend_activate(const struct log_backend *const backend,
256 void *ctx)
257{
258 __ASSERT_NO_MSG(backend != NULL);
259 backend->cb->ctx = ctx;
260 backend->cb->active = true;
261}
262
268static inline void log_backend_deactivate(
269 const struct log_backend *const backend)
270{
271 __ASSERT_NO_MSG(backend != NULL);
272 backend->cb->active = false;
273}
274
282static inline bool log_backend_is_active(
283 const struct log_backend *const backend)
284{
285 __ASSERT_NO_MSG(backend != NULL);
286 return backend->cb->active;
287}
288
293#ifdef __cplusplus
294}
295#endif
296
297#endif /* ZEPHYR_INCLUDE_LOGGING_LOG_BACKEND_H_ */
void
Definition: eswifi_shell.c:15
static void log_backend_deactivate(const struct log_backend *const backend)
Deactivate backend.
Definition: log_backend.h:268
static void log_backend_activate(const struct log_backend *const backend, void *ctx)
Activate backend.
Definition: log_backend.h:255
static bool log_backend_is_active(const struct log_backend *const backend)
Check state of the backend.
Definition: log_backend.h:282
static void log_backend_put(const struct log_backend *const backend, struct log_msg *msg)
Put message with log entry to the backend.
Definition: log_backend.h:104
static void log_backend_msg2_process(const struct log_backend *const backend, union log_msg2_generic *msg)
Definition: log_backend.h:112
static void log_backend_put_sync_string(const struct log_backend *const backend, struct log_msg_ids src_level, uint32_t timestamp, const char *fmt, va_list ap)
Synchronously process log message.
Definition: log_backend.h:131
static void log_backend_put_sync_hexdump(const struct log_backend *const backend, struct log_msg_ids src_level, uint32_t timestamp, const char *metadata, const uint8_t *data, uint32_t len)
Synchronously process log hexdump_message.
Definition: log_backend.h:155
static void log_backend_id_set(const struct log_backend *const backend, uint8_t id)
Set backend id.
Definition: log_backend.h:206
static void log_backend_dropped(const struct log_backend *const backend, uint32_t cnt)
Notify backend about dropped log messages.
Definition: log_backend.h:177
static int log_backend_count_get(void)
Get number of backends.
Definition: log_backend.h:244
static void log_backend_panic(const struct log_backend *const backend)
Reconfigure backend to panic mode.
Definition: log_backend.h:192
static uint8_t log_backend_id_get(const struct log_backend *const backend)
Get backend id.
Definition: log_backend.h:221
static const struct log_backend * log_backend_get(uint32_t idx)
Get backend.
Definition: log_backend.h:234
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
Logger backend API.
Definition: log_backend.h:32
void(* panic)(const struct log_backend *const backend)
Definition: log_backend.h:46
void(* put_sync_string)(const struct log_backend *const backend, struct log_msg_ids src_level, uint32_t timestamp, const char *fmt, va_list ap)
Definition: log_backend.h:38
void(* dropped)(const struct log_backend *const backend, uint32_t cnt)
Definition: log_backend.h:45
void(* process)(const struct log_backend *const backend, union log_msg2_generic *msg)
Definition: log_backend.h:33
void(* put_sync_hexdump)(const struct log_backend *const backend, struct log_msg_ids src_level, uint32_t timestamp, const char *metadata, const uint8_t *data, uint32_t len)
Definition: log_backend.h:41
void(* init)(const struct log_backend *const backend)
Definition: log_backend.h:47
void(* put)(const struct log_backend *const backend, struct log_msg *msg)
Definition: log_backend.h:36
Logger backend control block.
Definition: log_backend.h:53
uint8_t id
Definition: log_backend.h:55
bool active
Definition: log_backend.h:56
void * ctx
Definition: log_backend.h:54
Logger backend structure.
Definition: log_backend.h:62
const char * name
Definition: log_backend.h:65
const struct log_backend_api * api
Definition: log_backend.h:63
struct log_backend_control_block * cb
Definition: log_backend.h:64
bool autostart
Definition: log_backend.h:66
Part of log message header identifying source and level.
Definition: log_msg.h:85
Log message structure.
Definition: log_msg.h:139
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
Definition: log_msg2.h:114
Misc utilities.