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 
domain_id_cmp(uintptr_t id0,uintptr_t id1)58 static bool domain_id_cmp(uintptr_t id0, uintptr_t id1)
59 {
60 	return id0 == id1;
61 }
62 
source_id_cmp(uintptr_t id0,uintptr_t id1)63 static bool source_id_cmp(uintptr_t id0, uintptr_t id1)
64 {
65 	union log_source_ids s0 = { .raw = id0 };
66 	union log_source_ids s1 = { .raw = id1 };
67 
68 	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	Aboslute 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  */
get_link_domain(uint8_t domain_id,uint8_t * rel_domain_id)84 static const struct log_link *get_link_domain(uint8_t domain_id, uint8_t *rel_domain_id)
85 {
86 	uint8_t domain_max;
87 
88 	STRUCT_SECTION_FOREACH(log_link, link) {
89 		domain_max = link->ctrl_blk->domain_offset +
90 				link->ctrl_blk->domain_cnt;
91 		if (domain_id < domain_max) {
92 
93 			*rel_domain_id = domain_id - link->ctrl_blk->domain_offset;
94 			return link;
95 		}
96 	}
97 
98 	*rel_domain_id = 0;
99 
100 	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  */
get_source_offset(const struct log_link * link,uint8_t rel_domain_id)109 static uint32_t get_source_offset(const struct log_link *link,
110 				  uint8_t rel_domain_id)
111 {
112 	uint32_t offset = 0;
113 
114 	for (uint8_t i = 0; i < rel_domain_id; i++) {
115 		offset += log_link_sources_count(link, i);
116 	}
117 
118 	return offset;
119 }
120 
z_log_link_get_dynamic_filter(uint8_t domain_id,uint32_t source_id)121 uint32_t *z_log_link_get_dynamic_filter(uint8_t domain_id, uint32_t source_id)
122 {
123 	uint8_t rel_domain_id;
124 	const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
125 	uint32_t source_offset = 0;
126 
127 	__ASSERT_NO_MSG(link != NULL);
128 
129 	source_offset = get_source_offset(link, rel_domain_id);
130 
131 	return &link->ctrl_blk->filters[source_offset + source_id];
132 }
133 
134 #ifdef CONFIG_LOG_MULTIDOMAIN
link_filters_init(const struct log_link * link)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 
cache_init(void)154 static void cache_init(void)
155 {
156 	int err;
157 	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 	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 	err = log_cache_init(&dname_cache, &dname_cache_config);
171 	__ASSERT_NO_MSG(err == 0);
172 
173 	err = log_cache_init(&sname_cache, &sname_cache_config);
174 	__ASSERT_NO_MSG(err == 0);
175 }
176 
z_log_ext_domain_count(void)177 uint8_t z_log_ext_domain_count(void)
178 {
179 	uint8_t cnt = 0;
180 
181 	STRUCT_SECTION_FOREACH(log_link, link) {
182 		cnt += log_link_domains_count(link);
183 	}
184 
185 	return cnt;
186 }
187 
link_source_count(uint8_t domain_id)188 static uint16_t link_source_count(uint8_t domain_id)
189 {
190 	uint8_t rel_domain_id;
191 	const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
192 
193 	__ASSERT_NO_MSG(link != NULL);
194 
195 	return log_link_sources_count(link, rel_domain_id);
196 }
197 
log_src_cnt_get(uint32_t domain_id)198 uint32_t log_src_cnt_get(uint32_t domain_id)
199 {
200 	if (z_log_is_local_domain(domain_id)) {
201 		return z_log_sources_count();
202 	}
203 
204 	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  */
link_source_name_get(uint8_t domain_id,uint32_t source_id)212 static const char *link_source_name_get(uint8_t domain_id, uint32_t source_id)
213 {
214 	uint8_t *cached;
215 	size_t cache_size = sname_cache.item_size;
216 	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 	if (!log_cache_get(&sname_cache, id.raw, &cached)) {
225 		uint8_t rel_domain_id;
226 		const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
227 		int err;
228 
229 		__ASSERT_NO_MSG(link != NULL);
230 
231 		err = log_link_get_source_name(link, rel_domain_id, source_id,
232 					       cached, &cache_size);
233 		if (err < 0) {
234 			return NULL;
235 		}
236 
237 		log_cache_put(&sname_cache, cached);
238 	}
239 
240 	return (const char *)cached;
241 }
242 
log_source_name_get(uint32_t domain_id,uint32_t source_id)243 const char *log_source_name_get(uint32_t domain_id, uint32_t source_id)
244 {
245 	if (z_log_is_local_domain(domain_id)) {
246 		if (source_id < log_src_cnt_get(domain_id)) {
247 			return TYPE_SECTION_START(log_const)[source_id].name;
248 		} else {
249 			return NULL;
250 		}
251 	}
252 
253 	return link_source_name_get(domain_id, source_id);
254 }
255 
256 /* First check in cache if not there fetch from remote.
257  * When fetched from remote put in cache.
258  *
259  * @note Execution time depends on whether entry is in cache.
260  */
link_domain_name_get(uint8_t domain_id)261 static const char *link_domain_name_get(uint8_t domain_id)
262 {
263 	uint8_t *cached;
264 	size_t cache_size = dname_cache.item_size;
265 	uintptr_t id = (uintptr_t)domain_id;
266 	static const char *invalid_domain = "invalid";
267 
268 	/* If not in cache fetch from link and cache it. */
269 	if (!log_cache_get(&dname_cache, id, &cached)) {
270 		uint8_t rel_domain_id;
271 		const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
272 		int err;
273 
274 		__ASSERT_NO_MSG(link != NULL);
275 
276 		err = log_link_get_domain_name(link, rel_domain_id, cached, &cache_size);
277 		if (err < 0) {
278 			log_cache_release(&dname_cache, cached);
279 			return invalid_domain;
280 		}
281 
282 		log_cache_put(&dname_cache, cached);
283 	}
284 
285 	return (const char *)cached;
286 }
287 
log_domain_name_get(uint32_t domain_id)288 const char *log_domain_name_get(uint32_t domain_id)
289 {
290 	if (z_log_is_local_domain(domain_id)) {
291 		return CONFIG_LOG_DOMAIN_NAME;
292 	}
293 
294 	return link_domain_name_get(domain_id);
295 }
296 
link_compiled_level_get(uint8_t domain_id,uint32_t source_id)297 static uint8_t link_compiled_level_get(uint8_t domain_id, uint32_t source_id)
298 {
299 	uint8_t rel_domain_id;
300 	const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
301 	uint8_t level;
302 
303 	__ASSERT_NO_MSG(link != NULL);
304 
305 	return !log_link_get_levels(link, rel_domain_id, source_id, &level, NULL) ?
306 		level : 0;
307 }
308 
log_compiled_level_get(uint8_t domain_id,uint32_t source_id)309 uint8_t log_compiled_level_get(uint8_t domain_id, uint32_t source_id)
310 {
311 	if (z_log_is_local_domain(domain_id)) {
312 		if (source_id < log_src_cnt_get(domain_id)) {
313 			return TYPE_SECTION_START(log_const)[source_id].level;
314 		} else {
315 			return LOG_LEVEL_NONE;
316 		}
317 	}
318 
319 	return link_compiled_level_get(domain_id, source_id);
320 }
321 
z_log_link_set_runtime_level(uint8_t domain_id,uint16_t source_id,uint8_t level)322 int z_log_link_set_runtime_level(uint8_t domain_id, uint16_t source_id, uint8_t level)
323 {
324 	uint8_t rel_domain_id;
325 	const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
326 
327 	__ASSERT_NO_MSG(link != NULL);
328 
329 	return log_link_set_runtime_level(link, rel_domain_id, source_id, level);
330 }
331 
get_dynamic_filter(uint8_t domain_id,uint32_t source_id)332 static uint32_t *get_dynamic_filter(uint8_t domain_id, uint32_t source_id)
333 {
334 	if (z_log_is_local_domain(domain_id)) {
335 		return &TYPE_SECTION_START(log_dynamic)[source_id].filters;
336 	}
337 
338 	return z_log_link_get_dynamic_filter(domain_id, source_id);
339 }
340 
z_log_runtime_filters_init(void)341 void z_log_runtime_filters_init(void)
342 {
343 	/*
344 	 * Initialize aggregated runtime filter levels (no backends are
345 	 * attached yet, so leave backend slots in each dynamic filter set
346 	 * alone for now).
347 	 *
348 	 * Each log source's aggregated runtime level is set to match its
349 	 * compile-time level. When backends are attached later on in
350 	 * log_init(), they'll be initialized to the same value.
351 	 */
352 	for (int i = 0; i < z_log_sources_count(); i++) {
353 		uint32_t *filters = z_log_dynamic_filters_get(i);
354 		uint8_t level = log_compiled_level_get(Z_LOG_LOCAL_DOMAIN_ID, i);
355 
356 		level = MAX(level, CONFIG_LOG_OVERRIDE_LEVEL);
357 		LOG_FILTER_SLOT_SET(filters,
358 				    LOG_FILTER_AGGR_SLOT_IDX,
359 				    level);
360 	}
361 }
362 
log_source_id_get(const char * name)363 int log_source_id_get(const char *name)
364 {
365 	for (int i = 0; i < log_src_cnt_get(Z_LOG_LOCAL_DOMAIN_ID); i++) {
366 		const char *sname = log_source_name_get(Z_LOG_LOCAL_DOMAIN_ID, i);
367 
368 		if ((sname != NULL) && (strcmp(sname, name) == 0)) {
369 			return i;
370 		}
371 	}
372 	return -1;
373 }
374 
max_filter_get(uint32_t filters)375 static uint32_t max_filter_get(uint32_t filters)
376 {
377 	uint32_t max_filter = LOG_LEVEL_NONE;
378 	int first_slot = LOG_FILTER_FIRST_BACKEND_SLOT_IDX;
379 	int i;
380 
381 	for (i = first_slot; i < LOG_FILTERS_NUM_OF_SLOTS; i++) {
382 		uint32_t tmp_filter = LOG_FILTER_SLOT_GET(&filters, i);
383 
384 		if (tmp_filter > max_filter) {
385 			max_filter = tmp_filter;
386 		}
387 	}
388 
389 	return max_filter;
390 }
391 
set_runtime_filter(uint8_t backend_id,uint8_t domain_id,uint32_t source_id,uint32_t level)392 static void set_runtime_filter(uint8_t backend_id, uint8_t domain_id,
393 			       uint32_t source_id, uint32_t level)
394 {
395 	uint32_t prev_max;
396 	uint32_t new_max;
397 	uint32_t *filters = get_dynamic_filter(domain_id, source_id);
398 
399 	prev_max = LOG_FILTER_SLOT_GET(filters, LOG_FILTER_AGGR_SLOT_IDX);
400 
401 	LOG_FILTER_SLOT_SET(filters, backend_id, level);
402 
403 	/* Once current backend filter is updated recalculate
404 	 * aggregated maximal level
405 	 */
406 	new_max = max_filter_get(*filters);
407 
408 	LOG_FILTER_SLOT_SET(filters, LOG_FILTER_AGGR_SLOT_IDX, new_max);
409 
410 	if (!z_log_is_local_domain(domain_id) && (new_max != prev_max)) {
411 		(void)z_log_link_set_runtime_level(domain_id, source_id, level);
412 	}
413 }
414 
filter_get(uint8_t id,uint32_t domain_id,int16_t source_id,bool runtime)415 static uint32_t filter_get(uint8_t id, uint32_t domain_id, int16_t source_id, bool runtime)
416 {
417 	__ASSERT_NO_MSG(source_id < log_src_cnt_get(domain_id));
418 
419 	if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) && runtime) {
420 		if (source_id < 0) {
421 			return LOG_LEVEL_DBG;
422 		}
423 
424 		return LOG_FILTER_SLOT_GET(get_dynamic_filter(domain_id, source_id), id);
425 	}
426 
427 	return log_compiled_level_get(domain_id, source_id);
428 }
429 
430 
filter_set(int id,uint32_t domain_id,int16_t source_id,uint32_t level)431 uint32_t filter_set(int id, uint32_t domain_id, int16_t source_id, uint32_t level)
432 {
433 	if (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
434 		return log_compiled_level_get(domain_id, source_id);
435 	}
436 
437 	__ASSERT_NO_MSG(source_id < log_src_cnt_get(domain_id));
438 
439 
440 	if (id < 0) {
441 		uint32_t max = 0U;
442 		size_t backend_cnt;
443 
444 		if (IS_ENABLED(CONFIG_LOG_FRONTEND)) {
445 			max = filter_set(LOG_FRONTEND_SLOT_ID, domain_id, source_id, level);
446 			if (IS_ENABLED(CONFIG_LOG_FRONTEND_ONLY)) {
447 				return max;
448 			}
449 		}
450 
451 		STRUCT_SECTION_COUNT(log_backend, &backend_cnt);
452 		for (size_t i = 0; i < backend_cnt; i++) {
453 			uint32_t current = filter_set(i, domain_id, source_id, level);
454 
455 			max = MAX(current, max);
456 		}
457 
458 		return max;
459 	}
460 
461 	level = MIN(level, MAX(filter_get(id, domain_id, source_id, false),
462 			       CONFIG_LOG_OVERRIDE_LEVEL));
463 	set_runtime_filter(id, domain_id, source_id, level);
464 
465 	return level;
466 }
467 
z_impl_log_filter_set(struct log_backend const * const backend,uint32_t domain_id,int16_t source_id,uint32_t level)468 uint32_t z_impl_log_filter_set(struct log_backend const *const backend,
469 			       uint32_t domain_id, int16_t source_id,
470 			       uint32_t level)
471 {
472 	int id = (backend == NULL) ? -1 : log_backend_id_get(backend);
473 
474 	return filter_set(id, domain_id, source_id, level);
475 }
476 
z_impl_log_frontend_filter_set(int16_t source_id,uint32_t level)477 uint32_t z_impl_log_frontend_filter_set(int16_t source_id, uint32_t level)
478 {
479 	return filter_set(LOG_FRONTEND_SLOT_ID, Z_LOG_LOCAL_DOMAIN_ID, source_id, level);
480 }
481 
482 #ifdef CONFIG_USERSPACE
z_vrfy_log_filter_set(struct log_backend const * const backend,uint32_t domain_id,int16_t src_id,uint32_t level)483 uint32_t z_vrfy_log_filter_set(struct log_backend const *const backend,
484 			    uint32_t domain_id,
485 			    int16_t src_id,
486 			    uint32_t level)
487 {
488 	K_OOPS(K_SYSCALL_VERIFY_MSG(backend == NULL,
489 		"Setting per-backend filters from user mode is not supported"));
490 	K_OOPS(K_SYSCALL_VERIFY_MSG(domain_id == Z_LOG_LOCAL_DOMAIN_ID,
491 		"Invalid log domain_id"));
492 	K_OOPS(K_SYSCALL_VERIFY_MSG(src_id < (int16_t)log_src_cnt_get(domain_id),
493 		"Invalid log source id"));
494 	K_OOPS(K_SYSCALL_VERIFY_MSG(
495 		(level <= LOG_LEVEL_DBG),
496 		"Invalid log level"));
497 
498 	return z_impl_log_filter_set(NULL, domain_id, src_id, level);
499 }
500 #include <syscalls/log_filter_set_mrsh.c>
501 #endif
502 
link_filter_set(const struct log_link * link,struct log_backend const * const backend,uint32_t level)503 static void link_filter_set(const struct log_link *link,
504 			    struct log_backend const *const backend,
505 			    uint32_t level)
506 {
507 	if (log_link_is_active(link) != 0) {
508 		return;
509 	}
510 
511 	for (uint8_t d = link->ctrl_blk->domain_offset;
512 	     d < link->ctrl_blk->domain_offset + link->ctrl_blk->domain_cnt; d++) {
513 		for (uint16_t s = 0; s < log_src_cnt_get(d); s++) {
514 			log_filter_set(backend, d, s, level);
515 		}
516 	}
517 }
518 
backend_filter_set(struct log_backend const * const backend,uint32_t level)519 static void backend_filter_set(struct log_backend const *const backend,
520 			       uint32_t level)
521 {
522 	if (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
523 		return;
524 	}
525 
526 	for (uint16_t s = 0; s < log_src_cnt_get(0); s++) {
527 		log_filter_set(backend, 0, s, level);
528 	}
529 
530 	if (!IS_ENABLED(CONFIG_LOG_MULTIDOMAIN)) {
531 		return;
532 	}
533 
534 	/* Set level in activated links. */
535 	STRUCT_SECTION_FOREACH(log_link, link) {
536 		link_filter_set(link, backend, level);
537 	}
538 }
539 
log_backend_get_by_name(const char * backend_name)540 const struct log_backend *log_backend_get_by_name(const char *backend_name)
541 {
542 	STRUCT_SECTION_FOREACH(log_backend, backend) {
543 		if (strcmp(backend_name, backend->name) == 0) {
544 			return backend;
545 		}
546 	}
547 
548 	return NULL;
549 }
550 
log_backend_enable(struct log_backend const * const backend,void * ctx,uint32_t level)551 void log_backend_enable(struct log_backend const *const backend,
552 			void *ctx,
553 			uint32_t level)
554 {
555 	/* As first slot in filtering mask is reserved, backend ID has offset.*/
556 	uint32_t id = LOG_FILTER_FIRST_BACKEND_SLOT_IDX;
557 
558 	id += backend - log_backend_get(0);
559 
560 	log_backend_id_set(backend, id);
561 	backend->cb->level = level;
562 	backend_filter_set(backend, level);
563 	log_backend_activate(backend, ctx);
564 
565 	z_log_notify_backend_enabled();
566 }
567 
log_backend_disable(struct log_backend const * const backend)568 void log_backend_disable(struct log_backend const *const backend)
569 {
570 	if (log_backend_is_active(backend)) {
571 		backend_filter_set(backend, LOG_LEVEL_NONE);
572 	}
573 
574 	log_backend_deactivate(backend);
575 }
576 
log_filter_get(struct log_backend const * const backend,uint32_t domain_id,int16_t source_id,bool runtime)577 uint32_t log_filter_get(struct log_backend const *const backend,
578 			uint32_t domain_id, int16_t source_id, bool runtime)
579 {
580 	int id = (backend == NULL) ? -1 : log_backend_id_get(backend);
581 
582 	return filter_get(id, domain_id, source_id, runtime);
583 }
584 
log_frontend_filter_get(int16_t source_id,bool runtime)585 uint32_t log_frontend_filter_get(int16_t source_id, bool runtime)
586 {
587 	if (!IS_ENABLED(CONFIG_LOG_FRONTEND)) {
588 		return LOG_LEVEL_NONE;
589 	}
590 
591 	return filter_get(LOG_FRONTEND_SLOT_ID, Z_LOG_LOCAL_DOMAIN_ID, source_id, runtime);
592 }
593 
z_log_links_initiate(void)594 void z_log_links_initiate(void)
595 {
596 	int err;
597 
598 	cache_init();
599 
600 	STRUCT_SECTION_FOREACH(log_link, link) {
601 #ifdef CONFIG_MPSC_PBUF
602 		if (link->mpsc_pbuf) {
603 			mpsc_pbuf_init(link->mpsc_pbuf, link->mpsc_pbuf_config);
604 		}
605 #endif
606 
607 		err = log_link_initiate(link, NULL);
608 		__ASSERT(err == 0, "Failed to initialize link");
609 	}
610 }
611 
612 #ifdef CONFIG_LOG_MULTIDOMAIN
backends_link_init(const struct log_link * link)613 static void backends_link_init(const struct log_link *link)
614 {
615 	for (int i = 0; i < log_backend_count_get(); i++) {
616 		const struct log_backend *backend = log_backend_get(i);
617 
618 
619 		if (!log_backend_is_active(backend)) {
620 			continue;
621 		}
622 
623 		link_filter_set(link, backend, backend->cb->level);
624 	}
625 }
626 
z_log_links_activate(uint32_t active_mask,uint8_t * offset)627 uint32_t z_log_links_activate(uint32_t active_mask, uint8_t *offset)
628 {
629 	uint32_t mask = 0x1;
630 	uint32_t out_mask = 0;
631 
632 	/* Initiate offset to 1. */
633 	if (*offset == 0) {
634 		*offset = 1;
635 	}
636 
637 	STRUCT_SECTION_FOREACH(log_link, link) {
638 		if (active_mask & mask) {
639 			int err = log_link_activate(link);
640 
641 			if (err == 0) {
642 				uint8_t domain_cnt = log_link_domains_count(link);
643 
644 				link->ctrl_blk->domain_offset = *offset;
645 				link->ctrl_blk->domain_cnt = domain_cnt;
646 				*offset += domain_cnt;
647 				if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
648 					link_filters_init(link);
649 					backends_link_init(link);
650 				}
651 			} else {
652 				__ASSERT_NO_MSG(err == -EINPROGRESS);
653 				out_mask |= mask;
654 			}
655 		}
656 
657 		mask <<= 1;
658 	}
659 
660 	return out_mask;
661 }
662 #endif
663