libcoap 4.3.4-develop-c081bb6
coap_debug.h
Go to the documentation of this file.
1/*
2 * coap_debug.h -- debug utilities
3 *
4 * Copyright (C) 2010-2011,2014-2024 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#ifndef COAP_DEBUG_FD
31#define COAP_DEBUG_FD stdout
32#endif
33
34#ifndef COAP_ERR_FD
38#define COAP_ERR_FD stderr
39#endif
40
41#ifndef COAP_MAX_LOGGING_LEVEL
42#define COAP_MAX_LOGGING_LEVEL 8
43#endif /* ! COAP_MAX_LOGGING_LEVEL */
44
50typedef enum {
51 COAP_LOG_EMERG = 0, /* 0 */
54 COAP_LOG_ERR, /* 3 */
61#define COAP_LOG_CIPHERS COAP_LOG_DTLS_BASE /* For backward compatability */
63
64/*
65 * These have the same values, but can be used in #if tests for better
66 * readability
67 */
68#define _COAP_LOG_EMERG 0
69#define _COAP_LOG_ALERT 1
70#define _COAP_LOG_CRIT 2
71#define _COAP_LOG_ERR 3
72#define _COAP_LOG_WARN 4
73#define _COAP_LOG_NOTICE 5
74#define _COAP_LOG_INFO 6
75#define _COAP_LOG_DEBUG 7
76#define _COAP_LOG_OSCORE 8
77
79coap_no_log(void) { }
80
81#define coap_log_emerg(...) coap_log(COAP_LOG_EMERG, __VA_ARGS__)
82
83#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_ALERT)
84#define coap_log_alert(...) coap_log(COAP_LOG_ALERT, __VA_ARGS__)
85#else
86#define coap_log_alert(...) coap_no_log()
87#endif
88
89#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_CRIT)
90#define coap_log_crit(...) coap_log(COAP_LOG_CRIT, __VA_ARGS__)
91#else
92#define coap_log_crit(...) coap_no_log()
93#endif
94
95#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_ERR)
96#define coap_log_err(...) coap_log(COAP_LOG_ERR, __VA_ARGS__)
97#else
98#define coap_log_err(...) coap_no_log()
99#endif
100
101#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_WARN)
102#define coap_log_warn(...) coap_log(COAP_LOG_WARN, __VA_ARGS__)
103#else
104#define coap_log_warn(...) coap_no_log()
105#endif
106
107#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_INFO)
108#define coap_log_info(...) coap_log(COAP_LOG_INFO, __VA_ARGS__)
109#else
110#define coap_log_info(...) coap_no_log()
111#endif
112
113#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_NOTICE)
114#define coap_log_notice(...) coap_log(COAP_LOG_NOTICE, __VA_ARGS__)
115#else
116#define coap_log_notice(...) coap_no_log()
117#endif
118
119#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG)
120#define coap_log_debug(...) coap_log(COAP_LOG_DEBUG, __VA_ARGS__)
121#else
122#define coap_log_debug(...) coap_no_log()
123#endif
124
125#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE)
126#define coap_log_oscore(...) coap_log(COAP_LOG_OSCORE, __VA_ARGS__)
127#else
128#define coap_log_oscore(...) coap_no_log()
129#endif
130
131/*
132 * These entries are left here for backward compatability in applications
133 * (which should really "#include <syslog.h>").
134 * and MUST NOT be used anywhere within the libcoap code.
135 *
136 * If clashes occur during a particilar OS port, they can be safely deleted.
137 *
138 * In a future update, they will get removed.
139 */
140#if !defined(RIOT_VERSION) && !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
141#ifndef LOG_EMERG
142# define LOG_EMERG COAP_LOG_EMERG
143#endif
144#ifndef LOG_ALERT
145# define LOG_ALERT COAP_LOG_ALERT
146#endif
147#ifndef LOG_CRIT
148# define LOG_CRIT COAP_LOG_CRIT
149#endif
150#ifndef LOG_ERR
151# define LOG_ERR COAP_LOG_ERR
152#endif
153#ifndef LOG_WARNING
154# define LOG_WARNING COAP_LOG_WARN
155#endif
156#ifndef LOG_NOTICE
157# define LOG_NOTICE COAP_LOG_NOTICE
158#endif
159#ifndef LOG_INFO
160# define LOG_INFO COAP_LOG_INFO
161#endif
162#ifndef LOG_DEBUG
163# define LOG_DEBUG COAP_LOG_DEBUG
164#endif
165#endif /* ! RIOT_VERSION && ! WITH_LWIP && ! WITH_CONTIKI */
166
173
180
187
194
200const char *coap_log_level_desc(coap_log_t level);
201
209typedef void (*coap_log_handler_t)(coap_log_t level, const char *message);
210
218
224const char *coap_package_name(void);
225
231const char *coap_package_version(void);
232
238const char *coap_package_build(void);
239
251#if (defined(__GNUC__))
252void coap_log_impl(coap_log_t level,
253 const char *format, ...) __attribute__((format(printf, 2, 3)));
254#else
255void coap_log_impl(coap_log_t level, const char *format, ...);
256#endif
257
258#ifndef coap_log
259#ifdef WITH_CONTIKI
260#include <stdio.h>
261
262#ifndef LOG_CONF_LEVEL_COAP
263#define LOG_CONF_LEVEL_COAP 0 /* = LOG_LEVEL_NONE */
264#endif
265
266void coap_print_contiki_prefix(coap_log_t level);
267
268#define coap_log(level, ...) do { \
269 if (LOG_CONF_LEVEL_COAP && \
270 ((int)((level)) <= (int)coap_get_log_level())) { \
271 coap_print_contiki_prefix(level); \
272 printf(__VA_ARGS__); \
273 } \
274 } while(0)
275#else /* !WITH_CONTIKI */
284#define coap_log(level, ...) do { \
285 if ((level) < (coap_get_log_level() + 1)) \
286 coap_log_impl((level), __VA_ARGS__); \
287 } while(0)
288#endif /* !WITH_CONTIKI */
289#endif
290
291#ifndef coap_dtls_log
300#define coap_dtls_log(level, ...) do { \
301 if ((int)((level))<=(int)coap_dtls_get_log_level()) \
302 coap_log_impl((level)+COAP_LOG_DTLS_BASE, __VA_ARGS__); \
303 } while(0)
304#endif
305
306#include "coap_pdu.h"
307
314void coap_set_show_pdu_output(int use_fprintf);
315
324void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu);
325
332
342char *coap_string_tls_version(char *buffer, size_t bufsize);
343
352char *coap_string_tls_support(char *buffer, size_t bufsize);
353
363size_t coap_print_addr(const coap_address_t *address,
364 unsigned char *buffer, size_t size);
365
376const char *coap_print_ip_addr(const coap_address_t *address,
377 char *buffer, size_t size);
378
394int coap_debug_set_packet_loss(const char *loss_level);
395
396#endif /* COAP_DEBUG_H_ */
int coap_debug_set_packet_loss(const char *loss_level)
Set the packet loss level for testing.
Definition: coap_debug.c:1334
Pre-defined constants that reflect defaults for CoAP.
void coap_set_log_handler(coap_log_handler_t handler)
Add a custom log callback handler.
Definition: coap_debug.c:1267
const char * coap_log_level_desc(coap_log_t level)
Get the current logging description.
Definition: coap_debug.c:116
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:95
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.
Definition: coap_debug.c:1135
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition: coap_debug.c:778
char * coap_string_tls_support(char *buffer, size_t bufsize)
Build a string containing the current (D)TLS library support.
Definition: coap_debug.c:1237
coap_log_t
Logging type.
Definition: coap_debug.h:50
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
Definition: coap_notls.c:140
const char * coap_package_version(void)
Get the library package version.
Definition: coap_debug.c:76
void coap_set_log_level(coap_log_t level)
Sets the log level to the specified value.
Definition: coap_debug.c:100
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
Definition: coap_notls.c:135
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...
Definition: coap_debug.c:1272
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:233
const char * coap_package_build(void)
Get the library package build.
Definition: coap_debug.c:81
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:409
void coap_show_tls_version(coap_log_t level)
Display the current (D)TLS library linked with and built for version.
Definition: coap_debug.c:1128
void coap_set_show_pdu_output(int use_fprintf)
Defines the output mode for the coap_show_pdu() function.
Definition: coap_debug.c:90
COAP_STATIC_INLINE void coap_no_log(void)
Definition: coap_debug.h:79
const char * coap_package_name(void)
Get the library package name.
Definition: coap_debug.c:71
void(* coap_log_handler_t)(coap_log_t level, const char *message)
Logging callback handler definition.
Definition: coap_debug.h:209
@ COAP_LOG_INFO
Definition: coap_debug.h:57
@ COAP_LOG_OSCORE
Definition: coap_debug.h:59
@ COAP_LOG_EMERG
Definition: coap_debug.h:51
@ COAP_LOG_DTLS_BASE
Definition: coap_debug.h:60
@ COAP_LOG_NOTICE
Definition: coap_debug.h:56
@ COAP_LOG_DEBUG
Definition: coap_debug.h:58
@ COAP_LOG_ALERT
Definition: coap_debug.h:52
@ COAP_LOG_CRIT
Definition: coap_debug.h:53
@ COAP_LOG_ERR
Definition: coap_debug.h:54
@ COAP_LOG_WARN
Definition: coap_debug.h:55
#define COAP_STATIC_INLINE
Definition: libcoap.h:53
Multi-purpose address abstraction.
Definition: coap_address.h:148
structure for CoAP PDUs