• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * coap_debug.h -- debug utilities
3  *
4  * Copyright (C) 2010-2011,2014 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
10 #ifndef COAP_DEBUG_H_
11 #define COAP_DEBUG_H_
12 
13 /**
14  * @defgroup logging Logging Support
15  * API functions for logging support
16  * @{
17  */
18 
19 #ifndef COAP_DEBUG_FD
20 /**
21  * Used for output for @c LOG_DEBUG to @c LOG_ERR.
22  */
23 #define COAP_DEBUG_FD stdout
24 #endif
25 
26 #ifndef COAP_ERR_FD
27 /**
28  * Used for output for @c LOG_CRIT to @c LOG_EMERG.
29  */
30 #define COAP_ERR_FD stderr
31 #endif
32 
33 #ifdef HAVE_SYSLOG_H
34 #include <syslog.h>
35 /**
36  * Logging type.  One of LOG_* from @b syslog.
37  */
38 typedef short coap_log_t;
39 /*
40    LOG_DEBUG+2 gives ciphers in GnuTLS
41    Use COAP_LOG_CIPHERS to output Cipher Info in OpenSSL etc.
42  */
43 #define COAP_LOG_CIPHERS (LOG_DEBUG+2)
44 #else
45 /** Pre-defined log levels akin to what is used in \b syslog
46     with LOG_CIPHERS added. */
47 typedef enum {
48   LOG_EMERG=0, /**< Emergency */
49   LOG_ALERT,   /**< Alert */
50   LOG_CRIT,    /**< Critical */
51   LOG_ERR,     /**< Error */
52   LOG_WARNING, /**< Warning */
53   LOG_NOTICE,  /**< Notice */
54   LOG_INFO,    /**< Information */
55   LOG_DEBUG,   /**< Debug */
56   COAP_LOG_CIPHERS=LOG_DEBUG+2 /**< CipherInfo */
57 } coap_log_t;
58 #endif
59 
60 /**
61  * Get the current logging level.
62  *
63  * @return One of the LOG_* values.
64  */
65 coap_log_t coap_get_log_level(void);
66 
67 /**
68  * Sets the log level to the specified value.
69  *
70  * @param level One of the LOG_* values.
71  */
72 void coap_set_log_level(coap_log_t level);
73 
74 /**
75  * Logging call-back handler definition.
76  *
77  * @param level One of the LOG_* values.
78  * @param message Zero-terminated string message to log.
79  */
80 typedef void (*coap_log_handler_t) (coap_log_t level, const char *message);
81 
82 /**
83  * Add a custom log callback handler.
84  *
85  * @param handler The logging handler to use or @p NULL to use default handler.
86  */
87 void coap_set_log_handler(coap_log_handler_t handler);
88 
89 /**
90  * Get the library package name.
91  *
92  * @return Zero-terminated string with the name of this library.
93  */
94 const char *coap_package_name(void);
95 
96 /**
97  * Get the library package version.
98  *
99  * @return Zero-terminated string with the library version.
100  */
101 const char *coap_package_version(void);
102 
103 /**
104  * Writes the given text to @c COAP_ERR_FD (for @p level <= @c LOG_CRIT) or @c
105  * COAP_DEBUG_FD (for @p level >= @c LOG_ERR). The text is output only when
106  * @p level is below or equal to the log level that set by coap_set_log_level().
107  *
108  * Internal function.
109  *
110  * @param level One of the LOG_* values.
111  & @param format The format string to use.
112  */
113 #if (defined(__GNUC__))
114 void coap_log_impl(coap_log_t level,
115               const char *format, ...) __attribute__ ((format(printf, 2, 3)));
116 #else
117 void coap_log_impl(coap_log_t level, const char *format, ...);
118 #endif
119 
120 #ifndef coap_log
121 /**
122  * Logging function.
123  * Writes the given text to @c COAP_ERR_FD (for @p level <= @c LOG_CRIT) or @c
124  * COAP_DEBUG_FD (for @p level >= @c LOG_ERR). The text is output only when
125  * @p level is below or equal to the log level that set by coap_set_log_level().
126  *
127  * @param level One of the LOG_* values.
128  */
129 #define coap_log(level, ...) do { \
130   if ((int)((level))<=(int)coap_get_log_level()) \
131      coap_log_impl((level), __VA_ARGS__); \
132 } while(0)
133 #endif
134 
135 #include "pdu.h"
136 
137 /**
138  * Defines the output mode for the coap_show_pdu() function.
139  *
140  * @param use_fprintf @p 1 if the output is to use fprintf() (the default)
141  *                    @p 0 if the output is to use coap_log().
142  */
143 void coap_set_show_pdu_output(int use_fprintf);
144 
145 /**
146  * Display the contents of the specified @p pdu.
147  * Note: The output method of coap_show_pdu() is dependent on the setting of
148  * coap_set_show_pdu_output().
149  *
150  * @param level The required minimum logging level.
151  * @param pdu The PDU to decode.
152  */
153 void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu);
154 
155 /**
156  * Display the current (D)TLS library linked with and built for version.
157  *
158  * @param level The required minimum logging level.
159  */
160 void coap_show_tls_version(coap_log_t level);
161 
162 /**
163  * Build a string containing the current (D)TLS library linked with and
164  * built for version.
165  *
166  * @param buffer The buffer to put the string into.
167  * @param bufsize The size of the buffer to put the string into.
168  *
169  * @return A pointer to the provided buffer.
170  */
171 char *coap_string_tls_version(char *buffer, size_t bufsize);
172 
173 struct coap_address_t;
174 
175 /**
176  * Print the address into the defined buffer.
177  *
178  * Internal Function.
179  *
180  * @param address The address to print.
181  * @param buffer The buffer to print into.
182  * @param size The size of the buffer to print into.
183  *
184  * @return The amount written into the buffer.
185  */
186 size_t coap_print_addr(const struct coap_address_t *address,
187                        unsigned char *buffer, size_t size);
188 
189 /** @} */
190 
191 /**
192  * Set the packet loss level for testing.  This can be in one of two forms.
193  *
194  * Percentage : 0% to 100%.  Use the specified probability.
195  * 0% is send all packets, 100% is drop all packets.
196  *
197  * List: A comma separated list of numbers or number ranges that are the
198  * packets to drop.
199  *
200  * @param loss_level The defined loss level (percentage or list).
201  *
202  * @return @c 1 If loss level set, @c 0 if there is an error.
203  */
204 int coap_debug_set_packet_loss(const char *loss_level);
205 
206 /**
207  * Check to see whether a packet should be sent or not.
208  *
209  * Internal function
210  *
211  * @return @c 1 if packet is to be sent, @c 0 if packet is to be dropped.
212  */
213 int coap_debug_send_packet(void);
214 
215 
216 #endif /* COAP_DEBUG_H_ */
217