libcoap 4.3.2rc2

coap_debug.h
Go to the documentation of this file.
1/*
2 * coap_debug.h -- debug utilities
3 *
4 * Copyright (C) 2010-2011,2014-2023 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
50 typedef enum {
51 COAP_LOG_EMERG = 0, /* 0 */
52 COAP_LOG_ALERT, /* 1 */
53 COAP_LOG_CRIT, /* 2 */
54 COAP_LOG_ERR, /* 3 */
55 COAP_LOG_WARN, /* 4 */
56 COAP_LOG_NOTICE, /* 5 */
57 COAP_LOG_INFO, /* 6 */
58 COAP_LOG_DEBUG, /* 7 */
59 COAP_LOG_OSCORE, /* 8 */
60 COAP_LOG_DTLS_BASE,
61#define COAP_LOG_CIPHERS COAP_LOG_DTLS_BASE /* For backward compatability */
62} coap_log_t;
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
78 COAP_STATIC_INLINE void
79 coap_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
172 coap_log_t coap_get_log_level(void);
173
179 void coap_set_log_level(coap_log_t level);
180
186 void coap_dtls_set_log_level(coap_log_t level);
187
193 coap_log_t coap_dtls_get_log_level(void);
194
202 typedef void (*coap_log_handler_t)(coap_log_t level, const char *message);
203
210 void coap_set_log_handler(coap_log_handler_t handler);
211
217 const char *coap_package_name(void);
218
224 const char *coap_package_version(void);
225
231 const char *coap_package_build(void);
232
244#if (defined(__GNUC__))
245 void coap_log_impl(coap_log_t level,
246 const char *format, ...) __attribute__((format(printf, 2, 3)));
247#else
248 void coap_log_impl(coap_log_t level, const char *format, ...);
249#endif
250
251#ifndef coap_log
252#ifdef WITH_CONTIKI
253#include <stdio.h>
254
255#ifndef LOG_CONF_LEVEL_COAP
256#define LOG_CONF_LEVEL_COAP 0 /* = LOG_LEVEL_NONE */
257#endif
258
259 void coap_print_contiki_prefix(coap_log_t level);
260
261#define coap_log(level, ...) do { \
262 if (LOG_CONF_LEVEL_COAP && \
263 ((int)((level)) <= (int)coap_get_log_level())) { \
264 coap_print_contiki_prefix(level); \
265 printf(__VA_ARGS__); \
266 } \
267 } while(0)
268#else /* !WITH_CONTIKI */
277#define coap_log(level, ...) do { \
278 if ((int)((level))<=(int)coap_get_log_level()) \
279 coap_log_impl((level), __VA_ARGS__); \
280 } while(0)
281#endif /* !WITH_CONTIKI */
282#endif
283
284#ifndef coap_dtls_log
293#define coap_dtls_log(level, ...) do { \
294 if ((int)((level))<=(int)coap_dtls_get_log_level()) \
295 coap_log_impl((level)+COAP_LOG_DTLS_BASE, __VA_ARGS__); \
296 } while(0)
297#endif
298
299#include "coap_pdu.h"
300
307 void coap_set_show_pdu_output(int use_fprintf);
308
317 void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu);
318
324 void coap_show_tls_version(coap_log_t level);
325
335 char *coap_string_tls_version(char *buffer, size_t bufsize);
336
345 char *coap_string_tls_support(char *buffer, size_t bufsize);
346
358 size_t coap_print_addr(const coap_address_t *address,
359 unsigned char *buffer, size_t size);
360
373 const char *coap_print_ip_addr(const coap_address_t *address,
374 char *buffer, size_t size);
375
391 int coap_debug_set_packet_loss(const char *loss_level);
392
400 int coap_debug_send_packet(void);
401
402
403#endif /* COAP_DEBUG_H_ */
coap_debug_set_packet_loss
int coap_debug_set_packet_loss(const char *loss_level)
Set the packet loss level for testing.
Definition: coap_debug.c:1244
coap_debug_send_packet
int coap_debug_send_packet(void)
Check to see whether a packet should be sent or not.
Definition: coap_debug.c:1285
coap_pdu.h
Pre-defined constants that reflect defaults for CoAP.
coap_set_log_handler
void coap_set_log_handler(coap_log_handler_t handler)
Add a custom log callback handler.
Definition: coap_debug.c:1182
coap_get_log_level
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:91
coap_string_tls_version
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:1060
coap_show_pdu
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition: coap_debug.c:703
coap_string_tls_support
char * coap_string_tls_support(char *buffer, size_t bufsize)
Build a string containing the current (D)TLS library support.
Definition: coap_debug.c:1152
coap_log_t
coap_log_t
Logging type.
Definition: coap_debug.h:50
coap_dtls_get_log_level
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
Definition: coap_notls.c:139
coap_package_version
const char * coap_package_version(void)
Get the library package version.
Definition: coap_debug.c:72
coap_set_log_level
void coap_set_log_level(coap_log_t level)
Sets the log level to the specified value.
Definition: coap_debug.c:96
coap_dtls_set_log_level
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
Definition: coap_notls.c:134
coap_log_impl
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:1187
coap_print_addr
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:218
coap_package_build
const char * coap_package_build(void)
Get the library package build.
Definition: coap_debug.c:77
coap_print_ip_addr
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:364
coap_show_tls_version
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:1053
coap_set_show_pdu_output
void coap_set_show_pdu_output(int use_fprintf)
Defines the output mode for the coap_show_pdu() function.
Definition: coap_debug.c:86
coap_no_log
COAP_STATIC_INLINE void coap_no_log(void)
Definition: coap_debug.h:79
coap_package_name
const char * coap_package_name(void)
Get the library package name.
Definition: coap_debug.c:67
coap_log_handler_t
void(* coap_log_handler_t)(coap_log_t level, const char *message)
Logging callback handler definition.
Definition: coap_debug.h:202
COAP_LOG_INFO
@ COAP_LOG_INFO
Definition: coap_debug.h:57
COAP_LOG_OSCORE
@ COAP_LOG_OSCORE
Definition: coap_debug.h:59
COAP_LOG_EMERG
@ COAP_LOG_EMERG
Definition: coap_debug.h:51
COAP_LOG_DTLS_BASE
@ COAP_LOG_DTLS_BASE
Definition: coap_debug.h:60
COAP_LOG_NOTICE
@ COAP_LOG_NOTICE
Definition: coap_debug.h:56
COAP_LOG_DEBUG
@ COAP_LOG_DEBUG
Definition: coap_debug.h:58
COAP_LOG_ALERT
@ COAP_LOG_ALERT
Definition: coap_debug.h:52
COAP_LOG_CRIT
@ COAP_LOG_CRIT
Definition: coap_debug.h:53
COAP_LOG_ERR
@ COAP_LOG_ERR
Definition: coap_debug.h:54
COAP_LOG_WARN
@ COAP_LOG_WARN
Definition: coap_debug.h:55
COAP_STATIC_INLINE
#define COAP_STATIC_INLINE
Definition: libcoap.h:53
coap_address_t
Multi-purpose address abstraction.
Definition: coap_address.h:109
coap_pdu_t
structure for CoAP PDUs
Definition: coap_pdu_internal.h:119