Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2021 Nordic Semiconductor ASA
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : : #include <zephyr/logging/log_internal.h>
7 : : #include <zephyr/logging/log_ctrl.h>
8 : : #include <zephyr/internal/syscall_handler.h>
9 : : #include <zephyr/init.h>
10 : : #include <zephyr/logging/log.h>
11 : : #include <zephyr/logging/log_link.h>
12 : : #include <zephyr/sys/iterable_sections.h>
13 : :
14 : : #include "log_cache.h"
15 : :
16 : : LOG_MODULE_REGISTER(log_mgmt);
17 : :
18 : : #ifndef CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_SIZE
19 : : #define CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_SIZE 1
20 : : #endif
21 : :
22 : : #ifndef CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_COUNT
23 : : #define CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_COUNT 1
24 : : #endif
25 : :
26 : : #ifndef CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_SIZE
27 : : #define CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_SIZE 1
28 : : #endif
29 : :
30 : : #ifndef CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_COUNT
31 : : #define CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_COUNT 1
32 : : #endif
33 : :
34 : : #define DCACHE_BUF_SIZE \
35 : : (CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_SIZE + sizeof(struct log_cache_entry)) * \
36 : : CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_COUNT
37 : :
38 : : #define SCACHE_BUF_SIZE \
39 : : (CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_SIZE + sizeof(struct log_cache_entry)) * \
40 : : CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_COUNT
41 : :
42 : : static uint8_t dname_cache_buffer[DCACHE_BUF_SIZE] __aligned(sizeof(uint32_t));
43 : : static uint8_t sname_cache_buffer[SCACHE_BUF_SIZE] __aligned(sizeof(uint32_t));
44 : :
45 : : static struct log_cache dname_cache;
46 : : static struct log_cache sname_cache;
47 : :
48 : : struct log_source_id {
49 : : uint8_t domain_id;
50 : : uint16_t source_id;
51 : : };
52 : :
53 : : union log_source_ids {
54 : : struct log_source_id id;
55 : : uintptr_t raw;
56 : : };
57 : :
58 : 0 : static bool domain_id_cmp(uintptr_t id0, uintptr_t id1)
59 : : {
60 : 0 : return id0 == id1;
61 : : }
62 : :
63 : 0 : static bool source_id_cmp(uintptr_t id0, uintptr_t id1)
64 : : {
65 : 0 : union log_source_ids s0 = { .raw = id0 };
66 : 0 : union log_source_ids s1 = { .raw = id1 };
67 : :
68 : 0 : return (s0.id.source_id == s1.id.source_id) &&
69 : : (s0.id.domain_id == s1.id.domain_id);
70 : : }
71 : :
72 : : /* Implementation of functions related to controlling logging sources and backends:
73 : : * - getting/setting source details like name, filtering
74 : : * - controlling backends filtering
75 : : */
76 : :
77 : : /** @brief Return link and relative domain id based on absolute domain id.
78 : : *
79 : : * @param[in] domain_id Absolute domain ID.
80 : : * @param[out] rel_domain_id Domain ID elative to the link domain ID as output.
81 : : *
82 : : * @return Link to which given domain belongs. NULL if link was not found.
83 : : */
84 : 0 : static const struct log_link *get_link_domain(uint8_t domain_id, uint8_t *rel_domain_id)
85 : : {
86 : 0 : uint8_t domain_max;
87 : :
88 [ # # # # ]: 0 : STRUCT_SECTION_FOREACH(log_link, link) {
89 : 0 : domain_max = link->ctrl_blk->domain_offset +
90 : 0 : link->ctrl_blk->domain_cnt;
91 [ # # ]: 0 : if (domain_id < domain_max) {
92 : :
93 : 0 : *rel_domain_id = domain_id - link->ctrl_blk->domain_offset;
94 : 0 : return link;
95 : : }
96 : : }
97 : :
98 : 0 : *rel_domain_id = 0;
99 : :
100 : 0 : return NULL;
101 : : }
102 : :
103 : : /** @brief Get source offset used for getting runtime filter.
104 : : *
105 : : * Runtime filters for each link are dynamically allocated as an array of
106 : : * filters for all domains in the link. In order to fetch link associated with
107 : : * given source an index in the array must be retrieved.
108 : : */
109 : 0 : static uint32_t get_source_offset(const struct log_link *link,
110 : : uint8_t rel_domain_id)
111 : : {
112 : 0 : uint32_t offset = 0;
113 : :
114 [ # # ]: 0 : for (uint8_t i = 0; i < rel_domain_id; i++) {
115 : 0 : offset += log_link_sources_count(link, i);
116 : : }
117 : :
118 : 0 : return offset;
119 : : }
120 : :
121 : 0 : uint32_t *z_log_link_get_dynamic_filter(uint8_t domain_id, uint32_t source_id)
122 : : {
123 : 0 : uint8_t rel_domain_id;
124 : 0 : const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
125 : 0 : uint32_t source_offset = 0;
126 : :
127 [ # # ]: 0 : __ASSERT_NO_MSG(link != NULL);
128 : :
129 : 0 : source_offset = get_source_offset(link, rel_domain_id);
130 : :
131 : 0 : return &link->ctrl_blk->filters[source_offset + source_id];
132 : : }
133 : :
134 : : #ifdef CONFIG_LOG_MULTIDOMAIN
135 : : static int link_filters_init(const struct log_link *link)
136 : : {
137 : : uint32_t total_cnt = get_source_offset(link, link->ctrl_blk->domain_cnt);
138 : :
139 : : link->ctrl_blk->filters = k_malloc(sizeof(uint32_t) * total_cnt);
140 : : if (link->ctrl_blk->filters == NULL) {
141 : : LOG_ERR("Failed to allocate buffer for runtime filtering.");
142 : : __ASSERT(0, "Failed to allocate buffer.");
143 : : return -ENOMEM;
144 : : }
145 : :
146 : : memset(link->ctrl_blk->filters, 0, sizeof(uint32_t) * total_cnt);
147 : : LOG_DBG("%s: heap used for filters:%d",
148 : : link->name, (int)(total_cnt * sizeof(uint32_t)));
149 : :
150 : : return 0;
151 : : }
152 : : #endif
153 : :
154 : 0 : static void cache_init(void)
155 : : {
156 : 0 : int err;
157 : 0 : static const struct log_cache_config dname_cache_config = {
158 : : .buf = dname_cache_buffer,
159 : : .buf_len = sizeof(dname_cache_buffer),
160 : : .item_size = CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_SIZE,
161 : : .cmp = domain_id_cmp
162 : : };
163 : 0 : static const struct log_cache_config sname_cache_config = {
164 : : .buf = sname_cache_buffer,
165 : : .buf_len = sizeof(sname_cache_buffer),
166 : : .item_size = CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_SIZE,
167 : : .cmp = source_id_cmp
168 : : };
169 : :
170 : 0 : err = log_cache_init(&dname_cache, &dname_cache_config);
171 [ # # ]: 0 : __ASSERT_NO_MSG(err == 0);
172 : :
173 : 0 : err = log_cache_init(&sname_cache, &sname_cache_config);
174 [ # # ]: 0 : __ASSERT_NO_MSG(err == 0);
175 : 0 : }
176 : :
177 : 0 : uint8_t z_log_ext_domain_count(void)
178 : : {
179 : 0 : uint8_t cnt = 0;
180 : :
181 [ # # # # ]: 0 : STRUCT_SECTION_FOREACH(log_link, link) {
182 : 0 : cnt += log_link_domains_count(link);
183 : : }
184 : :
185 : 0 : return cnt;
186 : : }
187 : :
188 : 0 : static uint16_t link_source_count(uint8_t domain_id)
189 : : {
190 : 0 : uint8_t rel_domain_id;
191 : 0 : const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
192 : :
193 [ # # ]: 0 : __ASSERT_NO_MSG(link != NULL);
194 : :
195 : 0 : return log_link_sources_count(link, rel_domain_id);
196 : : }
197 : :
198 : 0 : uint32_t log_src_cnt_get(uint32_t domain_id)
199 : : {
200 [ # # ]: 0 : if (z_log_is_local_domain(domain_id)) {
201 : 0 : return z_log_sources_count();
202 : : }
203 : :
204 : 0 : return link_source_count(domain_id);
205 : : }
206 : :
207 : : /* First check in cache if not there fetch from remote.
208 : : * When fetched from remote put in cache.
209 : : *
210 : : * @note Execution time depends on whether entry is in cache.
211 : : */
212 : 0 : static const char *link_source_name_get(uint8_t domain_id, uint32_t source_id)
213 : : {
214 : 0 : uint8_t *cached;
215 : 0 : size_t cache_size = sname_cache.item_size;
216 : 0 : union log_source_ids id = {
217 : : .id = {
218 : : .domain_id = domain_id,
219 : : .source_id = source_id
220 : : }
221 : : };
222 : :
223 : : /* If not in cache fetch from link and cache it. */
224 [ # # ]: 0 : if (!log_cache_get(&sname_cache, id.raw, &cached)) {
225 : 0 : uint8_t rel_domain_id;
226 : 0 : const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
227 : 0 : int err;
228 : :
229 [ # # ]: 0 : __ASSERT_NO_MSG(link != NULL);
230 : :
231 : 0 : err = log_link_get_source_name(link, rel_domain_id, source_id,
232 : : (char *)cached, &cache_size);
233 [ # # ]: 0 : if (err < 0) {
234 : 0 : log_cache_release(&sname_cache, cached);
235 : 0 : return NULL;
236 : : }
237 : :
238 : 0 : log_cache_put(&sname_cache, cached);
239 : : }
240 : :
241 : 0 : return (const char *)cached;
242 : : }
243 : :
244 : 0 : const char *log_source_name_get(uint32_t domain_id, uint32_t source_id)
245 : : {
246 [ # # ]: 0 : if (z_log_is_local_domain(domain_id)) {
247 : 0 : if (IS_ENABLED(CONFIG_LOG_FMT_SECTION_STRIP)) {
248 : : return "unknown";
249 : : }
250 [ # # ]: 0 : if (source_id < log_src_cnt_get(domain_id)) {
251 : 0 : return TYPE_SECTION_START(log_const)[source_id].name;
252 : : } else {
253 : : return NULL;
254 : : }
255 : : }
256 : :
257 : 0 : return link_source_name_get(domain_id, source_id);
258 : : }
259 : :
260 : : /* First check in cache if not there fetch from remote.
261 : : * When fetched from remote put in cache.
262 : : *
263 : : * @note Execution time depends on whether entry is in cache.
264 : : */
265 : 0 : static const char *link_domain_name_get(uint8_t domain_id)
266 : : {
267 : 0 : uint8_t *cached;
268 : 0 : size_t cache_size = dname_cache.item_size;
269 : 0 : uintptr_t id = (uintptr_t)domain_id;
270 : 0 : static const char *invalid_domain = "invalid";
271 : :
272 : : /* If not in cache fetch from link and cache it. */
273 [ # # ]: 0 : if (!log_cache_get(&dname_cache, id, &cached)) {
274 : 0 : uint8_t rel_domain_id;
275 : 0 : const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
276 : 0 : int err;
277 : :
278 [ # # ]: 0 : __ASSERT_NO_MSG(link != NULL);
279 : :
280 : 0 : err = log_link_get_domain_name(link, rel_domain_id, (char *)cached,
281 : : &cache_size);
282 [ # # ]: 0 : if (err < 0) {
283 : 0 : log_cache_release(&dname_cache, cached);
284 : 0 : return invalid_domain;
285 : : }
286 : :
287 : 0 : log_cache_put(&dname_cache, cached);
288 : : }
289 : :
290 : 0 : return (const char *)cached;
291 : : }
292 : :
293 : 0 : const char *log_domain_name_get(uint32_t domain_id)
294 : : {
295 [ # # ]: 0 : if (z_log_is_local_domain(domain_id)) {
296 : : return CONFIG_LOG_DOMAIN_NAME;
297 : : }
298 : :
299 : 0 : return link_domain_name_get(domain_id);
300 : : }
301 : :
302 : 0 : static uint8_t link_compiled_level_get(uint8_t domain_id, uint32_t source_id)
303 : : {
304 : 0 : uint8_t rel_domain_id;
305 : 0 : const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
306 : 0 : uint8_t level;
307 : :
308 [ # # ]: 0 : __ASSERT_NO_MSG(link != NULL);
309 : :
310 : 0 : return !log_link_get_levels(link, rel_domain_id, source_id, &level, NULL) ?
311 [ # # ]: 0 : level : 0;
312 : : }
313 : :
314 : 0 : uint8_t log_compiled_level_get(uint8_t domain_id, uint32_t source_id)
315 : : {
316 [ # # ]: 0 : if (z_log_is_local_domain(domain_id)) {
317 [ # # ]: 0 : if (source_id < log_src_cnt_get(domain_id)) {
318 : 0 : return TYPE_SECTION_START(log_const)[source_id].level;
319 : : } else {
320 : : return LOG_LEVEL_NONE;
321 : : }
322 : : }
323 : :
324 : 0 : return link_compiled_level_get(domain_id, source_id);
325 : : }
326 : :
327 : 0 : int z_log_link_set_runtime_level(uint8_t domain_id, uint16_t source_id, uint8_t level)
328 : : {
329 : 0 : uint8_t rel_domain_id;
330 : 0 : const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
331 : :
332 [ # # ]: 0 : __ASSERT_NO_MSG(link != NULL);
333 : :
334 : 0 : return log_link_set_runtime_level(link, rel_domain_id, source_id, level);
335 : : }
336 : :
337 : : static uint32_t *get_dynamic_filter(uint8_t domain_id, uint32_t source_id)
338 : : {
339 : : if (z_log_is_local_domain(domain_id)) {
340 : : return &TYPE_SECTION_START(log_dynamic)[source_id].filters;
341 : : }
342 : :
343 : : return z_log_link_get_dynamic_filter(domain_id, source_id);
344 : : }
345 : :
346 : 0 : void z_log_runtime_filters_init(void)
347 : : {
348 : : /*
349 : : * Initialize aggregated runtime filter levels (no backends are
350 : : * attached yet, so leave backend slots in each dynamic filter set
351 : : * alone for now).
352 : : *
353 : : * Each log source's aggregated runtime level is set to match its
354 : : * compile-time level. When backends are attached later on in
355 : : * log_init(), they'll be initialized to the same value.
356 : : */
357 [ # # ]: 0 : for (int i = 0; i < z_log_sources_count(); i++) {
358 : 0 : uint32_t *filters = z_log_dynamic_filters_get(i);
359 : 0 : uint8_t level;
360 : :
361 : : #ifdef CONFIG_LOG_RUNTIME_DEFAULT_LEVEL
362 : : /* Use configured runtime default level for initial filter */
363 : : level = CONFIG_LOG_RUNTIME_DEFAULT_LEVEL;
364 : : #else
365 : : /* Fall back to compile-time level */
366 : 0 : level = log_compiled_level_get(Z_LOG_LOCAL_DOMAIN_ID, i);
367 : : #endif
368 : 0 : level = MAX(level, CONFIG_LOG_OVERRIDE_LEVEL);
369 : 0 : LOG_FILTER_SLOT_SET(filters,
370 : : LOG_FILTER_AGGR_SLOT_IDX,
371 : : level);
372 : : }
373 : 0 : }
374 : :
375 : 0 : int log_source_id_get(const char *name)
376 : : {
377 : 0 : if (IS_ENABLED(CONFIG_LOG_FMT_SECTION_STRIP)) {
378 : : return -1;
379 : : }
380 : :
381 [ # # ]: 0 : for (int i = 0; i < log_src_cnt_get(Z_LOG_LOCAL_DOMAIN_ID); i++) {
382 : 0 : const char *sname = log_source_name_get(Z_LOG_LOCAL_DOMAIN_ID, i);
383 : :
384 [ # # # # ]: 0 : if ((sname != NULL) && (strcmp(sname, name) == 0)) {
385 : 0 : return i;
386 : : }
387 : : }
388 : : return -1;
389 : : }
390 : :
391 : : static uint32_t max_filter_get(uint32_t filters)
392 : : {
393 : : uint32_t max_filter = LOG_LEVEL_NONE;
394 : : int first_slot = LOG_FILTER_FIRST_BACKEND_SLOT_IDX;
395 : : int i;
396 : :
397 : : for (i = first_slot; i < LOG_FILTERS_NUM_OF_SLOTS; i++) {
398 : : uint32_t tmp_filter = LOG_FILTER_SLOT_GET(&filters, i);
399 : :
400 : : if (tmp_filter > max_filter) {
401 : : max_filter = tmp_filter;
402 : : }
403 : : }
404 : :
405 : : return max_filter;
406 : : }
407 : :
408 : : static void set_runtime_filter(uint8_t backend_id, uint8_t domain_id,
409 : : uint32_t source_id, uint32_t level)
410 : : {
411 : : uint32_t prev_max;
412 : : uint32_t new_max;
413 : : uint32_t *filters = get_dynamic_filter(domain_id, source_id);
414 : :
415 : : prev_max = LOG_FILTER_SLOT_GET(filters, LOG_FILTER_AGGR_SLOT_IDX);
416 : :
417 : : LOG_FILTER_SLOT_SET(filters, backend_id, level);
418 : :
419 : : /* Once current backend filter is updated recalculate
420 : : * aggregated maximal level
421 : : */
422 : : new_max = max_filter_get(*filters);
423 : :
424 : : LOG_FILTER_SLOT_SET(filters, LOG_FILTER_AGGR_SLOT_IDX, new_max);
425 : :
426 : : if (!z_log_is_local_domain(domain_id) && (new_max != prev_max)) {
427 : : (void)z_log_link_set_runtime_level(domain_id, source_id, level);
428 : : }
429 : : }
430 : :
431 : 0 : static uint32_t filter_get(uint8_t id, uint32_t domain_id, int16_t source_id, bool runtime)
432 : : {
433 : : /* source_id can originate from another domain over IPC, so the bound
434 : : * must hold without CONFIG_ASSERT.
435 : : */
436 [ # # ]: 0 : if (source_id >= (int16_t)log_src_cnt_get(domain_id)) {
437 : : return LOG_LEVEL_NONE;
438 : : }
439 : :
440 : 0 : if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) && runtime) {
441 : : if (source_id < 0) {
442 : : return LOG_LEVEL_DBG;
443 : : }
444 : :
445 : : return LOG_FILTER_SLOT_GET(get_dynamic_filter(domain_id, source_id), id);
446 : : }
447 : :
448 : 0 : return log_compiled_level_get(domain_id, source_id);
449 : : }
450 : :
451 : :
452 : 0 : uint32_t filter_set(int id, uint32_t domain_id, int16_t source_id, uint32_t level)
453 : : {
454 : 0 : if (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
455 : 0 : return log_compiled_level_get(domain_id, source_id);
456 : : }
457 : :
458 : : /* As in filter_get(): an out-of-range source would index the dynamic
459 : : * filter table out of bounds.
460 : : */
461 : : if (source_id >= (int16_t)log_src_cnt_get(domain_id)) {
462 : : return LOG_LEVEL_NONE;
463 : : }
464 : :
465 : : if (id < 0) {
466 : : uint32_t max = 0U;
467 : : size_t backend_cnt;
468 : :
469 : : if (IS_ENABLED(CONFIG_LOG_FRONTEND)) {
470 : : max = filter_set(LOG_FRONTEND_SLOT_ID, domain_id, source_id, level);
471 : : if (IS_ENABLED(CONFIG_LOG_FRONTEND_ONLY)) {
472 : : return max;
473 : : }
474 : : }
475 : :
476 : : STRUCT_SECTION_COUNT(log_backend, &backend_cnt);
477 : : for (size_t i = 0; i < backend_cnt; i++) {
478 : : uint32_t current = filter_set(log_backend_id_get(log_backend_get(i)),
479 : : domain_id, source_id, level);
480 : :
481 : : max = MAX(current, max);
482 : : }
483 : :
484 : : return max;
485 : : }
486 : :
487 : : level = MIN(level, MAX(filter_get(id, domain_id, source_id, false),
488 : : CONFIG_LOG_OVERRIDE_LEVEL));
489 : : set_runtime_filter(id, domain_id, source_id, level);
490 : :
491 : : return level;
492 : : }
493 : :
494 : 0 : uint32_t z_impl_log_filter_set(struct log_backend const *const backend,
495 : : uint32_t domain_id, int16_t source_id,
496 : : uint32_t level)
497 : : {
498 [ # # ]: 0 : int id = (backend == NULL) ? -1 : log_backend_id_get(backend);
499 : :
500 : 0 : return filter_set(id, domain_id, source_id, level);
501 : : }
502 : :
503 : 0 : uint32_t z_impl_log_frontend_filter_set(int16_t source_id, uint32_t level)
504 : : {
505 : 0 : return filter_set(LOG_FRONTEND_SLOT_ID, Z_LOG_LOCAL_DOMAIN_ID, source_id, level);
506 : : }
507 : :
508 : : #ifdef CONFIG_USERSPACE
509 : : uint32_t z_vrfy_log_filter_set(struct log_backend const *const backend,
510 : : uint32_t domain_id,
511 : : int16_t src_id,
512 : : uint32_t level)
513 : : {
514 : : K_OOPS(K_SYSCALL_VERIFY_MSG(backend == NULL,
515 : : "Setting per-backend filters from user mode is not supported"));
516 : : K_OOPS(K_SYSCALL_VERIFY_MSG(domain_id == Z_LOG_LOCAL_DOMAIN_ID,
517 : : "Invalid log domain_id"));
518 : : K_OOPS(K_SYSCALL_VERIFY_MSG((uint32_t)src_id < log_src_cnt_get(domain_id),
519 : : "Invalid log source id"));
520 : : K_OOPS(K_SYSCALL_VERIFY_MSG(
521 : : (level <= LOG_LEVEL_DBG),
522 : : "Invalid log level"));
523 : :
524 : : return z_impl_log_filter_set(NULL, domain_id, src_id, level);
525 : : }
526 : : #include <zephyr/syscalls/log_filter_set_mrsh.c>
527 : : #endif
528 : :
529 : : static void link_filter_set(const struct log_link *link,
530 : : struct log_backend const *const backend,
531 : : uint32_t level)
532 : : {
533 : : if (log_link_is_active(link) != 0) {
534 : : return;
535 : : }
536 : :
537 : : for (uint8_t d = link->ctrl_blk->domain_offset;
538 : : d < link->ctrl_blk->domain_offset + link->ctrl_blk->domain_cnt; d++) {
539 : : for (uint16_t s = 0; s < log_src_cnt_get(d); s++) {
540 : : log_filter_set(backend, d, s, level);
541 : : }
542 : : }
543 : : }
544 : :
545 : : static void backend_filter_set(struct log_backend const *const backend,
546 : : uint32_t level)
547 : : {
548 : : if (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
549 : : return;
550 : : }
551 : :
552 : : for (uint16_t s = 0; s < log_src_cnt_get(0); s++) {
553 : : log_filter_set(backend, 0, s, level);
554 : : }
555 : :
556 : : if (!IS_ENABLED(CONFIG_LOG_MULTIDOMAIN)) {
557 : : return;
558 : : }
559 : :
560 : : /* Set level in activated links. */
561 : : STRUCT_SECTION_FOREACH(log_link, link) {
562 : : link_filter_set(link, backend, level);
563 : : }
564 : : }
565 : :
566 : 0 : const struct log_backend *log_backend_get_by_name(const char *backend_name)
567 : : {
568 [ # # # # ]: 0 : STRUCT_SECTION_FOREACH(log_backend, backend) {
569 [ # # ]: 0 : if (strcmp(backend_name, backend->name) == 0) {
570 : 0 : return backend;
571 : : }
572 : : }
573 : :
574 : : return NULL;
575 : : }
576 : :
577 : 1 : void log_backend_enable(struct log_backend const *const backend,
578 : : void *ctx,
579 : : uint32_t level)
580 : : {
581 : 1 : backend->cb->level = level;
582 : 1 : backend_filter_set(backend, level);
583 : 1 : log_backend_activate(backend, ctx);
584 : :
585 : 1 : z_log_notify_backend_enabled();
586 : 1 : }
587 : :
588 : 0 : void log_backend_disable(struct log_backend const *const backend)
589 : : {
590 : 0 : if (log_backend_is_active(backend)) {
591 : : backend_filter_set(backend, LOG_LEVEL_NONE);
592 : : }
593 : :
594 : 0 : log_backend_deactivate(backend);
595 : 0 : }
596 : :
597 : 0 : uint32_t log_filter_get(struct log_backend const *const backend,
598 : : uint32_t domain_id, int16_t source_id, bool runtime)
599 : : {
600 [ # # ]: 0 : int id = (backend == NULL) ? -1 : log_backend_id_get(backend);
601 : :
602 : 0 : return filter_get(id, domain_id, source_id, runtime);
603 : : }
604 : :
605 : 0 : uint32_t log_frontend_filter_get(int16_t source_id, bool runtime)
606 : : {
607 : 0 : if (!IS_ENABLED(CONFIG_LOG_FRONTEND)) {
608 : 0 : return LOG_LEVEL_NONE;
609 : : }
610 : :
611 : : return filter_get(LOG_FRONTEND_SLOT_ID, Z_LOG_LOCAL_DOMAIN_ID, source_id, runtime);
612 : : }
613 : :
614 : 0 : void z_log_links_initiate(void)
615 : : {
616 : 0 : int err;
617 : :
618 : 0 : cache_init();
619 : :
620 [ # # # # ]: 0 : STRUCT_SECTION_FOREACH(log_link, link) {
621 : : #ifdef CONFIG_MPSC_PBUF
622 : : if (link->mpsc_pbuf) {
623 : : mpsc_pbuf_init(link->mpsc_pbuf, link->mpsc_pbuf_config);
624 : : }
625 : : #endif
626 : :
627 : 0 : err = log_link_initiate(link, NULL);
628 [ # # ]: 0 : __ASSERT(err == 0, "Failed to initialize link");
629 : : }
630 : 0 : }
631 : :
632 : : #ifdef CONFIG_LOG_MULTIDOMAIN
633 : : static void backends_link_init(const struct log_link *link)
634 : : {
635 : : for (int i = 0; i < log_backend_count_get(); i++) {
636 : : const struct log_backend *backend = log_backend_get(i);
637 : :
638 : :
639 : : if (!log_backend_is_active(backend)) {
640 : : continue;
641 : : }
642 : :
643 : : link_filter_set(link, backend, backend->cb->level);
644 : : }
645 : : }
646 : :
647 : : uint32_t z_log_links_activate(uint32_t active_mask, uint8_t *offset)
648 : : {
649 : : uint32_t mask = 0x1;
650 : : uint32_t out_mask = 0;
651 : :
652 : : /* Initiate offset to 1. */
653 : : if (*offset == 0) {
654 : : *offset = 1;
655 : : }
656 : :
657 : : STRUCT_SECTION_FOREACH(log_link, link) {
658 : : if (active_mask & mask) {
659 : : int err = log_link_activate(link);
660 : :
661 : : if (err == 0) {
662 : : uint8_t domain_cnt = log_link_domains_count(link);
663 : :
664 : : link->ctrl_blk->domain_offset = *offset;
665 : : link->ctrl_blk->domain_cnt = domain_cnt;
666 : : *offset += domain_cnt;
667 : : if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
668 : : link_filters_init(link);
669 : : backends_link_init(link);
670 : : }
671 : : } else {
672 : : __ASSERT_NO_MSG(err == -EINPROGRESS);
673 : : out_mask |= mask;
674 : : }
675 : : }
676 : :
677 : : mask <<= 1;
678 : : }
679 : :
680 : : return out_mask;
681 : : }
682 : : #endif
|