1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef ZEPHYR_INCLUDE_LOGGING_LOG_CTRL_H_
7 #define ZEPHYR_INCLUDE_LOGGING_LOG_CTRL_H_
8 
9 #include <zephyr/kernel.h>
10 #include <zephyr/logging/log_backend.h>
11 #include <zephyr/logging/log_msg.h>
12 #include <zephyr/logging/log_internal.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /**
19  * @brief Logger
20  * @defgroup logger Logger system
21  * @ingroup logging
22  * @{
23  * @}
24  */
25 
26 /**
27  * @brief Logger control API
28  * @defgroup log_ctrl Logger control API
29  * @ingroup logger
30  * @{
31  */
32 
33 typedef log_timestamp_t (*log_timestamp_get_t)(void);
34 
35 /** @brief Function system initialization of the logger.
36  *
37  * Function is called during start up to allow logging before user can
38  * explicitly initialize the logger.
39  */
40 void log_core_init(void);
41 
42 /**
43  * @brief Function for user initialization of the logger.
44  *
45  */
46 void log_init(void);
47 
48 /**
49  * @brief Function for providing thread which is processing logs.
50  *
51  * See CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD.
52  *
53  * @note Function has asserts and has no effect when CONFIG_LOG_PROCESS_THREAD is set.
54  *
55  * @param process_tid Process thread id. Used to wake up the thread.
56  */
57 void log_thread_set(k_tid_t process_tid);
58 
59 /**
60  * @brief Function for providing timestamp function.
61  *
62  * @param timestamp_getter	Timestamp function.
63  * @param freq			Timestamping frequency.
64  *
65  * @return 0 on success or error.
66  */
67 int log_set_timestamp_func(log_timestamp_get_t timestamp_getter,
68 			   uint32_t freq);
69 
70 /**
71  * @brief Switch the logger subsystem to the panic mode.
72  *
73  * Returns immediately if the logger is already in the panic mode.
74  *
75  * @details On panic the logger subsystem informs all backends about panic mode.
76  *          Backends must switch to blocking mode or halt. All pending logs
77  *          are flushed after switching to panic mode. In panic mode, all log
78  *          messages must be processed in the context of the call.
79  */
80 __syscall void log_panic(void);
81 
82 /**
83  * @brief Process one pending log message.
84  *
85  * @retval true There is more messages pending to be processed.
86  * @retval false No messages pending.
87  */
88 __syscall bool log_process(void);
89 
90 /**
91  * @brief Return number of buffered log messages.
92  *
93  * @return Number of currently buffered log messages.
94  */
95 __syscall uint32_t log_buffered_cnt(void);
96 
97 /** @brief Get number of independent logger sources (modules and instances)
98  *
99  * @param domain_id Domain ID.
100  *
101  * @return Number of sources.
102  */
103 uint32_t log_src_cnt_get(uint32_t domain_id);
104 
105 
106 /** @brief Get name of the source (module or instance).
107  *
108  * @param domain_id Domain ID.
109  * @param source_id Source ID.
110  *
111  * @return Source name or NULL if invalid arguments.
112  */
113 const char *log_source_name_get(uint32_t domain_id, uint32_t source_id);
114 
115 /** @brief Return number of domains present in the system.
116  *
117  * There will be at least one local domain.
118  *
119  * @return Number of domains.
120  */
log_domains_count(void)121 static inline uint8_t log_domains_count(void)
122 {
123 	return 1 + (IS_ENABLED(CONFIG_LOG_MULTIDOMAIN) ? z_log_ext_domain_count() : 0);
124 }
125 
126 /** @brief Get name of the domain.
127  *
128  * @param domain_id Domain ID.
129  *
130  * @return Domain name.
131  */
132 const char *log_domain_name_get(uint32_t domain_id);
133 
134 /**
135  * @brief Function for finding source ID based on source name.
136  *
137  * @param name Source name
138  *
139  * @return Source ID or negative number when source ID is not found.
140  */
141 int log_source_id_get(const char *name);
142 
143 /**
144  * @brief Get source filter for the provided backend.
145  *
146  * @param backend	Backend instance.
147  * @param domain_id	ID of the domain.
148  * @param source_id	Source (module or instance) ID.
149  * @param runtime	True for runtime filter or false for compiled in.
150  *
151  * @return		Severity level.
152  */
153 uint32_t log_filter_get(struct log_backend const *const backend,
154 			uint32_t domain_id, int16_t source_id, bool runtime);
155 
156 /**
157  * @brief Set filter on given source for the provided backend.
158  *
159  * @param backend	Backend instance. NULL for all backends.
160  * @param domain_id	ID of the domain.
161  * @param source_id	Source (module or instance) ID.
162  * @param level		Severity level.
163  *
164  * @return Actual level set which may be limited by compiled level. If filter
165  *	   was set for all backends then maximal level that was set is returned.
166  */
167 __syscall uint32_t log_filter_set(struct log_backend const *const backend,
168 				  uint32_t domain_id, int16_t source_id,
169 				  uint32_t level);
170 
171 /**
172  *
173  * @brief Enable backend with initial maximum filtering level.
174  *
175  * @param backend	Backend instance.
176  * @param ctx		User context.
177  * @param level		Severity level.
178  */
179 void log_backend_enable(struct log_backend const *const backend,
180 			void *ctx,
181 			uint32_t level);
182 
183 /**
184  *
185  * @brief Disable backend.
186  *
187  * @param backend	Backend instance.
188  */
189 void log_backend_disable(struct log_backend const *const backend);
190 
191 /**
192  * @brief Get backend by name.
193  *
194  * @param[in] backend_name Name of the backend as defined by the LOG_BACKEND_DEFINE.
195  *
196  * @retval Pointer to the backend instance if found, NULL if backend is not found.
197  */
198 const struct log_backend *log_backend_get_by_name(const char *backend_name);
199 
200 /** @brief Sets logging format for all active backends.
201  *
202  * @param log_type Log format.
203  *
204  * @retval Pointer to the last backend that failed, NULL for success.
205  */
206 const struct log_backend *log_format_set_all_active_backends(size_t log_type);
207 
208 /**
209  * @brief Check if there is pending data to be processed by the logging subsystem.
210  *
211  * Function can be used to determine if all logs have been flushed. Function
212  * returns false when deferred mode is not enabled.
213  *
214  * @retval true There is pending data.
215  * @retval false No pending data to process.
216  */
log_data_pending(void)217 static inline bool log_data_pending(void)
218 {
219 	return IS_ENABLED(CONFIG_LOG_MODE_DEFERRED) ? z_log_msg_pending() : false;
220 }
221 
222 /**
223  * @brief Configure tag used to prefix each message.
224  *
225  * @param tag Tag.
226  *
227  * @retval 0 on successful operation.
228  * @retval -ENOTSUP if feature is disabled.
229  * @retval -ENOMEM if string is longer than the buffer capacity. Tag will be trimmed.
230  */
231 int log_set_tag(const char *tag);
232 
233 /**
234  * @brief Get current memory usage.
235  *
236  * @param[out] buf_size Capacity of the buffer used for storing log messages.
237  * @param[out] usage Number of bytes currently containing pending log messages.
238  *
239  * @retval -EINVAL if logging mode does not use the buffer.
240  * @retval 0 successfully collected usage data.
241  */
242 int log_mem_get_usage(uint32_t *buf_size, uint32_t *usage);
243 
244 /**
245  * @brief Get maximum memory usage.
246  *
247  * Requires CONFIG_LOG_MEM_UTILIZATION option.
248  *
249  * @param[out] max Maximum number of bytes used for pending log messages.
250  *
251  * @retval -EINVAL if logging mode does not use the buffer.
252  * @retval -ENOTSUP if instrumentation is not enabled.
253  * not been enabled.
254  *
255  * @retval 0 successfully collected usage data.
256  */
257 int log_mem_get_max_usage(uint32_t *max);
258 
259 #if defined(CONFIG_LOG) && !defined(CONFIG_LOG_MODE_MINIMAL)
260 #define LOG_CORE_INIT() log_core_init()
261 #define LOG_PANIC() log_panic()
262 #if defined(CONFIG_LOG_FRONTEND_ONLY)
263 #define LOG_INIT() 0
264 #define LOG_PROCESS() false
265 #else /* !CONFIG_LOG_FRONTEND_ONLY */
266 #define LOG_INIT() log_init()
267 #define LOG_PROCESS() log_process()
268 #endif /* !CONFIG_LOG_FRONTEND_ONLY */
269 #else
270 #define LOG_CORE_INIT() do { } while (false)
271 #define LOG_INIT() 0
272 #define LOG_PANIC() /* Empty */
273 #define LOG_PROCESS() false
274 #endif
275 
276 #include <syscalls/log_ctrl.h>
277 
278 /**
279  * @}
280  */
281 
282 #ifdef __cplusplus
283 }
284 #endif
285 
286 #endif /* ZEPHYR_INCLUDE_LOGGING_LOG_CTRL_H_ */
287