1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef ZEPHYR_INCLUDE_LOGGING_LOG_INTERNAL_H_
7 #define ZEPHYR_INCLUDE_LOGGING_LOG_INTERNAL_H_
8 
9 #include <zephyr/types.h>
10 #include <zephyr/sys/__assert.h>
11 #include <zephyr/logging/log_core.h>
12 #include <zephyr/sys/mpsc_pbuf.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /* Header contains declarations of functions used internally in the logging,
19  * shared between various portions of logging subsystem. Functions are internal
20  * not intended to be used outside, including logging backends.
21  */
22 
23 /** @brief Structure wrapper to be used for memory section. */
24 struct log_mpsc_pbuf {
25 	struct mpsc_pbuf_buffer buf;
26 };
27 
28 /** @brief Structure wrapper to be used for memory section. */
29 struct log_msg_ptr {
30 	union log_msg_generic *msg;
31 };
32 
33 /** @brief Indicate to the log core that one log message has been dropped.
34  *
35  * @param buffered True if dropped message was already buffered and it is being
36  * dropped to free space for another message. False if message is being dropped
37  * because allocation failed.
38  */
39 void z_log_dropped(bool buffered);
40 
41 /** @brief Read and clear current drop indications counter.
42  *
43  * @return Dropped count.
44  */
45 uint32_t z_log_dropped_read_and_clear(void);
46 
47 /** @brief Check if there are any pending drop notifications.
48  *
49  * @retval true Pending unreported drop indications.
50  * @retval false No pending unreported drop indications.
51  */
52 bool z_log_dropped_pending(void);
53 
54 /** @brief Free allocated buffer.
55  *
56  * @param buf Buffer.
57  */
58 void z_log_free(void *buf);
59 
60 /* Initialize runtime filters */
61 void z_log_runtime_filters_init(void);
62 
63 /* Initialize links. */
64 void z_log_links_initiate(void);
65 
66 /* Activate links.
67  * Attemp to activate links,
68  *
69  * @param active_mask     Mask with links to activate. N bit set indicates that Nth
70  * link should be activated.
71  *
72  * @param[in, out] offset Offset assigned to domains. Initialize to 0 before first use.
73  *
74  * @return Mask with links that still remain inactive.
75  */
76 uint32_t z_log_links_activate(uint32_t active_mask, uint8_t *offset);
77 
78 /* Notify log_core that a backend was enabled. */
79 void z_log_notify_backend_enabled(void);
80 
81 /** @brief Get pointer to the filter set of the log source.
82  *
83  * @param source_id Source ID.
84  *
85  * @return Pointer to the filter set.
86  */
z_log_dynamic_filters_get(uint32_t source_id)87 static inline uint32_t *z_log_dynamic_filters_get(uint32_t source_id)
88 {
89 	return &TYPE_SECTION_START(log_dynamic)[source_id].filters;
90 }
91 
92 /** @brief Get number of registered sources. */
z_log_sources_count(void)93 static inline uint32_t z_log_sources_count(void)
94 {
95 	return log_const_source_id(TYPE_SECTION_END(log_const));
96 }
97 
98 /** @brief Return number of external domains.
99  *
100  * @return Number of external domains.
101  */
102 uint8_t z_log_ext_domain_count(void);
103 
104 /** @brief Initialize module for handling logging message. */
105 void z_log_msg_init(void);
106 
107 /** @brief Commit log message.
108  *
109  * @param msg Message.
110  */
111 void z_log_msg_commit(struct log_msg *msg);
112 
113 /** @brief Get pending log message.
114  *
115  * @param[out] backoff Recommended backoff needed to maintain ordering of processed
116  * messages. Used only when links are using dedicated buffers.
117  *
118  * @param Message or null if no pending messages.
119  */
120 union log_msg_generic *z_log_msg_claim(k_timeout_t *backoff);
121 
122 /** @brief Free message.
123  *
124  * @param msg Message.
125  */
126 void z_log_msg_free(union log_msg_generic *msg);
127 
128 /** @brief Check if there are any message pending.
129  *
130  * @retval true if at least one message is pending.
131  * @retval false if no message is pending.
132  */
133 bool z_log_msg_pending(void);
134 
z_log_notify_drop(const struct mpsc_pbuf_buffer * buffer,const union mpsc_pbuf_generic * item)135 static inline void z_log_notify_drop(const struct mpsc_pbuf_buffer *buffer,
136 				     const union mpsc_pbuf_generic *item)
137 {
138 	ARG_UNUSED(buffer);
139 	ARG_UNUSED(item);
140 
141 	z_log_dropped(true);
142 }
143 
144 /** @brief Get tag.
145  *
146  * @return Tag. Null if feature is disabled.
147  */
148 const char *z_log_get_tag(void);
149 
150 /** @brief Check if domain is local.
151  *
152  * @param domain_id Domain ID.
153  *
154  * @return True if domain is local.
155  */
z_log_is_local_domain(uint8_t domain_id)156 static inline bool z_log_is_local_domain(uint8_t domain_id)
157 {
158 	return !IS_ENABLED(CONFIG_LOG_MULTIDOMAIN) ||
159 			(domain_id == Z_LOG_LOCAL_DOMAIN_ID);
160 }
161 
162 /** @brief Get timestamp.
163  *
164  * @return Timestamp.
165  */
166 log_timestamp_t z_log_timestamp(void);
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif /* ZEPHYR_INCLUDE_LOGGING_LOG_INTERNAL_H_ */
173