/src/libcoap/include/coap3/coap_debug.h
Line | Count | Source |
1 | | /* |
2 | | * coap_debug.h -- debug utilities |
3 | | * |
4 | | * Copyright (C) 2010-2011,2014-2025 Olaf Bergmann <bergmann@tzi.org> |
5 | | * |
6 | | * SPDX-License-Identifier: BSD-2-Clause |
7 | | * |
8 | | * This file is part of the CoAP library libcoap. Please see README for terms |
9 | | * of use. |
10 | | */ |
11 | | |
12 | | /** |
13 | | * @file coap_debug.h |
14 | | * @brief CoAP Logging support |
15 | | */ |
16 | | |
17 | | #ifndef COAP_DEBUG_H_ |
18 | | #define COAP_DEBUG_H_ |
19 | | |
20 | | /** |
21 | | * @ingroup application_api |
22 | | * @defgroup logging Logging Support |
23 | | * API for logging support |
24 | | * @{ |
25 | | */ |
26 | | |
27 | | #include "coap_pdu.h" |
28 | | |
29 | | #ifdef __cplusplus |
30 | | extern "C" { |
31 | | #endif |
32 | | |
33 | | #ifndef COAP_DEBUG_FD |
34 | | /** |
35 | | * Used for output for @c COAP_LOG_OSCORE to @c COAP_LOG_ERR. |
36 | | */ |
37 | 9.29k | #define COAP_DEBUG_FD stdout |
38 | | #endif |
39 | | |
40 | | #ifndef COAP_ERR_FD |
41 | | /** |
42 | | * Used for output for @c COAP_LOG_CRIT to @c COAP_LOG_EMERG. |
43 | | */ |
44 | 521 | #define COAP_ERR_FD stderr |
45 | | #endif |
46 | | |
47 | | #ifndef COAP_MAX_LOGGING_LEVEL |
48 | 11.0k | #define COAP_MAX_LOGGING_LEVEL 8 |
49 | | #endif /* ! COAP_MAX_LOGGING_LEVEL */ |
50 | | |
51 | | /** |
52 | | * Logging type. These should be used where possible in the code instead |
53 | | * of the syslog definitions, or alternatively use the coap_log_*() functions |
54 | | * to reduce line length. |
55 | | */ |
56 | | typedef enum { |
57 | | COAP_LOG_EMERG = 0, /* 0 */ |
58 | | COAP_LOG_ALERT, /* 1 */ |
59 | | COAP_LOG_CRIT, /* 2 */ |
60 | | COAP_LOG_ERR, /* 3 */ |
61 | | COAP_LOG_WARN, /* 4 */ |
62 | | COAP_LOG_NOTICE, /* 5 */ |
63 | | COAP_LOG_INFO, /* 6 */ |
64 | | COAP_LOG_DEBUG, /* 7 */ |
65 | | COAP_LOG_OSCORE, /* 8 */ |
66 | | COAP_LOG_DTLS_BASE, |
67 | | #define COAP_LOG_CIPHERS COAP_LOG_DTLS_BASE /* For backward compatability */ |
68 | | } coap_log_t; |
69 | | |
70 | | /* |
71 | | * These have the same values, but can be used in #if tests for better |
72 | | * readability |
73 | | */ |
74 | | #define _COAP_LOG_EMERG 0 |
75 | | #define _COAP_LOG_ALERT 1 |
76 | | #define _COAP_LOG_CRIT 2 |
77 | | #define _COAP_LOG_ERR 3 |
78 | | #define _COAP_LOG_WARN 4 |
79 | | #define _COAP_LOG_NOTICE 5 |
80 | | #define _COAP_LOG_INFO 6 |
81 | | #define _COAP_LOG_DEBUG 7 |
82 | | #define _COAP_LOG_OSCORE 8 |
83 | | |
84 | | COAP_STATIC_INLINE void |
85 | 0 | coap_no_log(void) { }Unexecuted instantiation: coap_debug.c:coap_no_log Unexecuted instantiation: coap_encode.c:coap_no_log Unexecuted instantiation: coap_net.c:coap_no_log Unexecuted instantiation: coap_netif.c:coap_no_log Unexecuted instantiation: coap_notls.c:coap_no_log Unexecuted instantiation: coap_option.c:coap_no_log Unexecuted instantiation: coap_oscore.c:coap_no_log Unexecuted instantiation: coap_pdu.c:coap_no_log Unexecuted instantiation: coap_proxy.c:coap_no_log Unexecuted instantiation: coap_prng.c:coap_no_log Unexecuted instantiation: coap_resource.c:coap_no_log Unexecuted instantiation: coap_session.c:coap_no_log Unexecuted instantiation: coap_sha1.c:coap_no_log Unexecuted instantiation: coap_str.c:coap_no_log Unexecuted instantiation: coap_strm_posix.c:coap_no_log Unexecuted instantiation: coap_subscribe.c:coap_no_log Unexecuted instantiation: coap_threadsafe.c:coap_no_log Unexecuted instantiation: coap_time.c:coap_no_log Unexecuted instantiation: coap_uri.c:coap_no_log Unexecuted instantiation: coap_ws.c:coap_no_log Unexecuted instantiation: oscore.c:coap_no_log Unexecuted instantiation: oscore_cbor.c:coap_no_log Unexecuted instantiation: oscore_context.c:coap_no_log Unexecuted instantiation: oscore_cose.c:coap_no_log Unexecuted instantiation: oscore_crypto.c:coap_no_log Unexecuted instantiation: coap_address.c:coap_no_log Unexecuted instantiation: coap_async.c:coap_no_log Unexecuted instantiation: coap_block.c:coap_no_log Unexecuted instantiation: coap_cache.c:coap_no_log Unexecuted instantiation: coap_dgrm_posix.c:coap_no_log Unexecuted instantiation: coap_dtls.c:coap_no_log Unexecuted instantiation: coap_hashkey.c:coap_no_log Unexecuted instantiation: coap_io.c:coap_no_log Unexecuted instantiation: coap_io_posix.c:coap_no_log Unexecuted instantiation: coap_layers.c:coap_no_log Unexecuted instantiation: coap_mem.c:coap_no_log |
86 | | |
87 | 0 | #define coap_log_emerg(...) coap_log(COAP_LOG_EMERG, __VA_ARGS__) |
88 | | |
89 | | #if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_ALERT) |
90 | 0 | #define coap_log_alert(...) coap_log(COAP_LOG_ALERT, __VA_ARGS__) |
91 | | #else |
92 | | #define coap_log_alert(...) coap_no_log() |
93 | | #endif |
94 | | |
95 | | #if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_CRIT) |
96 | 0 | #define coap_log_crit(...) coap_log(COAP_LOG_CRIT, __VA_ARGS__) |
97 | | #else |
98 | | #define coap_log_crit(...) coap_no_log() |
99 | | #endif |
100 | | |
101 | | #if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_ERR) |
102 | 444 | #define coap_log_err(...) coap_log(COAP_LOG_ERR, __VA_ARGS__) |
103 | | #else |
104 | | #define coap_log_err(...) coap_no_log() |
105 | | #endif |
106 | | |
107 | | #if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_WARN) |
108 | 633k | #define coap_log_warn(...) coap_log(COAP_LOG_WARN, __VA_ARGS__) |
109 | | #else |
110 | | #define coap_log_warn(...) coap_no_log() |
111 | | #endif |
112 | | |
113 | | #if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_INFO) |
114 | 0 | #define coap_log_info(...) coap_log(COAP_LOG_INFO, __VA_ARGS__) |
115 | | #else |
116 | | #define coap_log_info(...) coap_no_log() |
117 | | #endif |
118 | | |
119 | | #if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_NOTICE) |
120 | | #define coap_log_notice(...) coap_log(COAP_LOG_NOTICE, __VA_ARGS__) |
121 | | #else |
122 | | #define coap_log_notice(...) coap_no_log() |
123 | | #endif |
124 | | |
125 | | #if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG) |
126 | 11.9k | #define coap_log_debug(...) coap_log(COAP_LOG_DEBUG, __VA_ARGS__) |
127 | | #else |
128 | | #define coap_log_debug(...) coap_no_log() |
129 | | #endif |
130 | | |
131 | | #if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE) |
132 | 0 | #define coap_log_oscore(...) coap_log(COAP_LOG_OSCORE, __VA_ARGS__) |
133 | | #else |
134 | | #define coap_log_oscore(...) coap_no_log() |
135 | | #endif |
136 | | |
137 | | /* |
138 | | * These entries are left here for backward compatability in applications |
139 | | * (which should really "#include <syslog.h>"). |
140 | | * and MUST NOT be used anywhere within the libcoap code. |
141 | | * |
142 | | * If clashes occur during a particilar OS port, they can be safely deleted. |
143 | | * |
144 | | * In a future update, they will get removed. |
145 | | */ |
146 | | #if !defined(RIOT_VERSION) && !defined(WITH_LWIP) && !defined(WITH_CONTIKI) |
147 | | #ifndef LOG_EMERG |
148 | | # define LOG_EMERG COAP_LOG_EMERG |
149 | | #endif |
150 | | #ifndef LOG_ALERT |
151 | | # define LOG_ALERT COAP_LOG_ALERT |
152 | | #endif |
153 | | #ifndef LOG_CRIT |
154 | | # define LOG_CRIT COAP_LOG_CRIT |
155 | | #endif |
156 | | #ifndef LOG_ERR |
157 | | # define LOG_ERR COAP_LOG_ERR |
158 | | #endif |
159 | | #ifndef LOG_WARNING |
160 | | # define LOG_WARNING COAP_LOG_WARN |
161 | | #endif |
162 | | #ifndef LOG_NOTICE |
163 | | # define LOG_NOTICE COAP_LOG_NOTICE |
164 | | #endif |
165 | | #ifndef LOG_INFO |
166 | | # define LOG_INFO COAP_LOG_INFO |
167 | | #endif |
168 | | #ifndef LOG_DEBUG |
169 | | # define LOG_DEBUG COAP_LOG_DEBUG |
170 | | #endif |
171 | | #endif /* ! RIOT_VERSION && ! WITH_LWIP && ! WITH_CONTIKI */ |
172 | | |
173 | | /** |
174 | | * Get the current logging level. |
175 | | * |
176 | | * @return One of the COAP_LOG_* values. |
177 | | */ |
178 | | coap_log_t coap_get_log_level(void); |
179 | | |
180 | | /** |
181 | | * Sets the log level to the specified value. |
182 | | * |
183 | | * @param level One of the COAP_LOG_* values. |
184 | | */ |
185 | | void coap_set_log_level(coap_log_t level); |
186 | | |
187 | | /** |
188 | | * Sets the (D)TLS logging level to the specified @p level. |
189 | | * |
190 | | * @param level One of the COAP_LOG_* values. |
191 | | */ |
192 | | void coap_dtls_set_log_level(coap_log_t level); |
193 | | |
194 | | /** |
195 | | * Get the current (D)TLS logging. |
196 | | * |
197 | | * @return One of the COAP_LOG_* values. |
198 | | */ |
199 | | coap_log_t coap_dtls_get_log_level(void); |
200 | | |
201 | | /** |
202 | | * Get the current logging description. |
203 | | * |
204 | | * @return Ascii representation of logging level. |
205 | | */ |
206 | | const char *coap_log_level_desc(coap_log_t level); |
207 | | |
208 | | /** |
209 | | * Logging callback handler definition. |
210 | | * |
211 | | * @param level One of the COAP_LOG_* values, or if used for (D)TLS logging, |
212 | | * COAP_LOG_DTLS_BASE + one of the COAP_LOG_* values. |
213 | | * @param message Zero-terminated string message to log. |
214 | | */ |
215 | | typedef void (*coap_log_handler_t)(coap_log_t level, const char *message); |
216 | | |
217 | | /** |
218 | | * Add a custom log callback handler. |
219 | | * |
220 | | * @param handler The logging handler to use or @p NULL to use default handler. |
221 | | * This handler will be used for both CoAP and (D)TLS logging. |
222 | | */ |
223 | | void coap_set_log_handler(coap_log_handler_t handler); |
224 | | |
225 | | /** |
226 | | * Get the library package name. |
227 | | * |
228 | | * @return Zero-terminated string with the name of this library. |
229 | | */ |
230 | | const char *coap_package_name(void); |
231 | | |
232 | | /** |
233 | | * Get the library package version. |
234 | | * |
235 | | * @return Zero-terminated string with the library version. |
236 | | */ |
237 | | const char *coap_package_version(void); |
238 | | |
239 | | /** |
240 | | * Get the library package build. |
241 | | * |
242 | | * @return Zero-terminated string with the library build. |
243 | | */ |
244 | | const char *coap_package_build(void); |
245 | | |
246 | | /** |
247 | | * Writes the given text to @c COAP_ERR_FD (for @p level <= @c COAP_LOG_CRIT) or |
248 | | * @c COAP_DEBUG_FD (for @p level >= @c COAP_LOG_ERR). The text is output only |
249 | | * when @p level is below or equal to the log level that set by |
250 | | * coap_set_log_level(). |
251 | | * |
252 | | * Internal function. |
253 | | * |
254 | | * @param level One of the COAP_LOG_* values. |
255 | | & @param format The format string to use. |
256 | | */ |
257 | | #if (defined(__GNUC__)) |
258 | | void coap_log_impl(coap_log_t level, |
259 | | const char *format, ...) __attribute__((format(printf, 2, 3))); |
260 | | #else |
261 | | void coap_log_impl(coap_log_t level, const char *format, ...); |
262 | | #endif |
263 | | |
264 | | #ifndef coap_log |
265 | | #ifdef WITH_CONTIKI |
266 | | #include <stdio.h> |
267 | | |
268 | | #ifndef LOG_CONF_LEVEL_COAP |
269 | | #define LOG_CONF_LEVEL_COAP 0 /* = LOG_LEVEL_NONE */ |
270 | | #endif |
271 | | |
272 | | void coap_print_contiki_prefix(coap_log_t level); |
273 | | |
274 | | #define coap_log(level, ...) do { \ |
275 | | if (LOG_CONF_LEVEL_COAP && \ |
276 | | ((int)((level)) <= (int)coap_get_log_level())) { \ |
277 | | coap_print_contiki_prefix(level); \ |
278 | | printf(__VA_ARGS__); \ |
279 | | } \ |
280 | | } while(0) |
281 | | #else /* !WITH_CONTIKI */ |
282 | | /** |
283 | | * Logging function. |
284 | | * Writes the given text to @c COAP_ERR_FD (for @p level <= @c COAP_LOG_CRIT) or @c |
285 | | * COAP_DEBUG_FD (for @p level >= @c COAP_LOG_ERR). The text is output only when |
286 | | * @p level is below or equal to the log level that set by coap_set_log_level(). |
287 | | * |
288 | | * @param level One of the COAP_LOG_* values. |
289 | | */ |
290 | 646k | #define coap_log(level, ...) do { \ |
291 | 646k | if ((level) < (coap_get_log_level() + 1)) \ |
292 | 646k | coap_log_impl((level), __VA_ARGS__); \ |
293 | 646k | } while(0) |
294 | | #endif /* !WITH_CONTIKI */ |
295 | | #endif |
296 | | |
297 | | #ifndef coap_dtls_log |
298 | | /** |
299 | | * Logging function. |
300 | | * Writes the given text to @c COAP_ERR_FD (for @p level <= @c COAP_LOG_CRIT) or @c |
301 | | * COAP_DEBUG_FD (for @p level >= @c COAP_LOG_ERR). The text is output only when |
302 | | * @p level is below or equal to the log level that set by coap_dtls_set_log_level(). |
303 | | * |
304 | | * @param level One of the COAP_LOG_* values. |
305 | | */ |
306 | | #define coap_dtls_log(level, ...) do { \ |
307 | | if ((int)((level))<=(int)coap_dtls_get_log_level()) \ |
308 | | coap_log_impl((level)+COAP_LOG_DTLS_BASE, __VA_ARGS__); \ |
309 | | } while(0) |
310 | | #endif |
311 | | |
312 | | /** |
313 | | * Defines the output mode for the coap_show_pdu() function. |
314 | | * |
315 | | * @param use_fprintf @p 1 if the output is to use fprintf() (the default) |
316 | | * @p 0 if the output is to use coap_log(). |
317 | | */ |
318 | | void coap_set_show_pdu_output(int use_fprintf); |
319 | | |
320 | | /** |
321 | | * Defines whether the data is to be output or not for the |
322 | | * coap_show_pdu() function. |
323 | | * |
324 | | * @param enable_data @p 1 if the data is to be output (the default) |
325 | | * @p 0 if the data detail is not to be output. |
326 | | */ |
327 | | void coap_enable_pdu_data_output(int enable_data); |
328 | | |
329 | | /** |
330 | | * Display the contents of the specified @p pdu. |
331 | | * Note: The output method of coap_show_pdu() is dependent on the setting of |
332 | | * coap_set_show_pdu_output(). |
333 | | * Note: Data may, or may not be output depending on the setting of |
334 | | * coap_enable_pdu_data_output(). |
335 | | * |
336 | | * @param level The required minimum logging level. |
337 | | * @param pdu The PDU to decode. |
338 | | */ |
339 | | void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu); |
340 | | |
341 | | /** |
342 | | * Display the current (D)TLS library linked with and built for version. |
343 | | * |
344 | | * @param level The required minimum logging level. |
345 | | */ |
346 | | void coap_show_tls_version(coap_log_t level); |
347 | | |
348 | | /** |
349 | | * Build a string containing the current (D)TLS library linked with and |
350 | | * built for version. |
351 | | * |
352 | | * @param buffer The buffer to put the string into. |
353 | | * @param bufsize The size of the buffer to put the string into. |
354 | | * |
355 | | * @return A pointer to the provided buffer. |
356 | | */ |
357 | | char *coap_string_tls_version(char *buffer, size_t bufsize); |
358 | | |
359 | | /** |
360 | | * Build a string containing the current (D)TLS library support |
361 | | * |
362 | | * @param buffer The buffer to put the string into. |
363 | | * @param bufsize The size of the buffer to put the string into. |
364 | | * |
365 | | * @return A pointer to the provided buffer. |
366 | | */ |
367 | | char *coap_string_tls_support(char *buffer, size_t bufsize); |
368 | | |
369 | | /** |
370 | | * Print the address into the defined buffer. |
371 | | * |
372 | | * @param address The address to print. |
373 | | * @param buffer The buffer to print into. |
374 | | * @param size The size of the buffer to print into. |
375 | | * |
376 | | * @return The amount written into the buffer. |
377 | | */ |
378 | | size_t coap_print_addr(const coap_address_t *address, |
379 | | unsigned char *buffer, size_t size); |
380 | | |
381 | | /** |
382 | | * Print the IP address into the defined buffer. |
383 | | * |
384 | | * @param address The address to print. |
385 | | * @param buffer The buffer to print into. |
386 | | * @param size The size of the buffer to print into. |
387 | | * |
388 | | * @return The pointer to provided buffer with as much of the IP address added |
389 | | * as possible. |
390 | | */ |
391 | | const char *coap_print_ip_addr(const coap_address_t *address, |
392 | | char *buffer, size_t size); |
393 | | |
394 | | /** @} */ |
395 | | |
396 | | /** |
397 | | * Set the packet loss level for testing. This can be in one of two forms. |
398 | | * |
399 | | * Percentage : 0% to 100%. Use the specified probability. |
400 | | * 0% is send all packets, 100% is drop all packets. |
401 | | * |
402 | | * List: A comma separated list of numbers or number ranges that are the |
403 | | * packets to drop. |
404 | | * |
405 | | * @param loss_level The defined loss level (percentage or list). |
406 | | * |
407 | | * @return @c 1 If loss level set, @c 0 if there is an error. |
408 | | */ |
409 | | int coap_debug_set_packet_loss(const char *loss_level); |
410 | | |
411 | | #ifdef __cplusplus |
412 | | } |
413 | | #endif |
414 | | |
415 | | #endif /* COAP_DEBUG_H_ */ |