libcoap 4.3.5-develop-ea01661
Loading...
Searching...
No Matches
coap_debug.h
Go to the documentation of this file.
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
17#ifndef COAP_DEBUG_H_
18#define COAP_DEBUG_H_
19
27#include "coap_pdu.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#ifndef COAP_DEBUG_FD
37#define COAP_DEBUG_FD stdout
38#endif
39
40#ifndef COAP_ERR_FD
44#define COAP_ERR_FD stderr
45#endif
46
47#ifndef COAP_MAX_LOGGING_LEVEL
48#define COAP_MAX_LOGGING_LEVEL 8
49#endif /* ! COAP_MAX_LOGGING_LEVEL */
50
56typedef enum {
57 COAP_LOG_EMERG = 0, /* 0 */
60 COAP_LOG_ERR, /* 3 */
67#define COAP_LOG_CIPHERS COAP_LOG_DTLS_BASE /* For backward compatability */
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
85coap_no_log(void) { }
86
87#define coap_log_emerg(...) coap_log(COAP_LOG_EMERG, __VA_ARGS__)
88
89#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_ALERT)
90#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#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#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#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#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#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#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
179
186
193
200
206const char *coap_log_level_desc(coap_log_t level);
207
215typedef void (*coap_log_handler_t)(coap_log_t level, const char *message);
216
224
230const char *coap_package_name(void);
231
237const char *coap_package_version(void);
238
244const char *coap_package_build(void);
245
257#if (defined(__GNUC__))
258void coap_log_impl(coap_log_t level,
259 const char *format, ...) __attribute__((format(printf, 2, 3)));
260#else
261void 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
272void 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 */
290#define coap_log(level, ...) do { \
291 if ((level) < (coap_get_log_level() + 1)) \
292 coap_log_impl((level), __VA_ARGS__); \
293 } while(0)
294#endif /* !WITH_CONTIKI */
295#endif
296
297#ifndef coap_dtls_log
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
318void coap_set_show_pdu_output(int use_fprintf);
319
327void coap_enable_pdu_data_output(int enable_data);
328
339void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu);
340
347
357char *coap_string_tls_version(char *buffer, size_t bufsize);
358
367char *coap_string_tls_support(char *buffer, size_t bufsize);
368
378size_t coap_print_addr(const coap_address_t *address,
379 unsigned char *buffer, size_t size);
380
391const char *coap_print_ip_addr(const coap_address_t *address,
392 char *buffer, size_t size);
393
409int coap_debug_set_packet_loss(const char *loss_level);
410
411#ifdef __cplusplus
412}
413#endif
414
415#endif /* COAP_DEBUG_H_ */
int coap_debug_set_packet_loss(const char *loss_level)
Set the packet loss level for testing.
Pre-defined constants that reflect defaults for CoAP.
void coap_set_log_handler(coap_log_handler_t handler)
Add a custom log callback handler.
const char * coap_log_level_desc(coap_log_t level)
Get the current logging description.
Definition coap_debug.c:124
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:103
char * coap_string_tls_version(char *buffer, size_t bufsize)
Build a string containing the current (D)TLS library linked with and built for version.
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition coap_debug.c:786
void coap_enable_pdu_data_output(int enable_data)
Defines whether the data is to be output or not for the coap_show_pdu() function.
Definition coap_debug.c:98
char * coap_string_tls_support(char *buffer, size_t bufsize)
Build a string containing the current (D)TLS library support.
coap_log_t
Logging type.
Definition coap_debug.h:56
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
Definition coap_notls.c:176
const char * coap_package_version(void)
Get the library package version.
Definition coap_debug.c:79
void coap_set_log_level(coap_log_t level)
Sets the log level to the specified value.
Definition coap_debug.c:108
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
Definition coap_notls.c:171
void coap_log_impl(coap_log_t level, const char *format,...)
Writes the given text to COAP_ERR_FD (for level <= COAP_LOG_CRIT) or COAP_DEBUG_FD (for level >= COAP...
size_t coap_print_addr(const coap_address_t *address, unsigned char *buffer, size_t size)
Print the address into the defined buffer.
Definition coap_debug.c:241
const char * coap_package_build(void)
Get the library package build.
Definition coap_debug.c:84
const char * coap_print_ip_addr(const coap_address_t *address, char *buffer, size_t size)
Print the IP address into the defined buffer.
Definition coap_debug.c:417
void coap_show_tls_version(coap_log_t level)
Display the current (D)TLS library linked with and built for version.
void coap_set_show_pdu_output(int use_fprintf)
Defines the output mode for the coap_show_pdu() function.
Definition coap_debug.c:93
COAP_STATIC_INLINE void coap_no_log(void)
Definition coap_debug.h:85
const char * coap_package_name(void)
Get the library package name.
Definition coap_debug.c:74
void(* coap_log_handler_t)(coap_log_t level, const char *message)
Logging callback handler definition.
Definition coap_debug.h:215
@ COAP_LOG_INFO
Definition coap_debug.h:63
@ COAP_LOG_OSCORE
Definition coap_debug.h:65
@ COAP_LOG_EMERG
Definition coap_debug.h:57
@ COAP_LOG_DTLS_BASE
Definition coap_debug.h:66
@ COAP_LOG_NOTICE
Definition coap_debug.h:62
@ COAP_LOG_DEBUG
Definition coap_debug.h:64
@ COAP_LOG_ALERT
Definition coap_debug.h:58
@ COAP_LOG_CRIT
Definition coap_debug.h:59
@ COAP_LOG_ERR
Definition coap_debug.h:60
@ COAP_LOG_WARN
Definition coap_debug.h:61
#define COAP_STATIC_INLINE
Definition libcoap.h:57
Multi-purpose address abstraction.
structure for CoAP PDUs