diff --git a/configure.ac b/configure.ac index 773bcce7..375e4039 100644 --- a/configure.ac +++ b/configure.ac @@ -635,6 +635,7 @@ man/coap_context.txt man/coap_encryption.txt man/coap_handler.txt man/coap_observe.txt +man/coap_logging.txt man/coap_pdu_setup.txt man/coap_recovery.txt man/coap_resource.txt diff --git a/include/coap/coap_dtls.h b/include/coap/coap_dtls.h index 1910908f..45fcedb5 100644 --- a/include/coap/coap_dtls.h +++ b/include/coap/coap_dtls.h @@ -56,21 +56,6 @@ typedef struct coap_tls_version_t { */ coap_tls_version_t *coap_get_tls_library_version(void); -/** - * Sets the (D)TLS logging level to the specified @p level. - * Note: coap_log_level() will influence output if at a specified level. - * - * @param level The logging level to use - LOG_* - */ -void coap_dtls_set_log_level(int level); - -/** - * Get the current (D)TLS logging. - * - * @return The current log level (one of LOG_*). - */ -int coap_dtls_get_log_level(void); - struct coap_dtls_pki_t; /** @@ -605,5 +590,22 @@ void coap_dtls_startup(void); /** @} */ +/** + * @ingroup logging + * Sets the (D)TLS logging level to the specified @p level. + * Note: coap_log_level() will influence output if at a specified level. + * + * @param level The logging level to use - LOG_* + */ +void coap_dtls_set_log_level(int level); + +/** + * @ingroup logging + * Get the current (D)TLS logging. + * + * @return The current log level (one of LOG_*). + */ +int coap_dtls_get_log_level(void); + #endif /* COAP_DTLS_H */ diff --git a/include/coap/coap_session.h b/include/coap/coap_session.h index 3f7f6380..3645be9d 100644 --- a/include/coap/coap_session.h +++ b/include/coap/coap_session.h @@ -284,10 +284,11 @@ ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu); /** + * @ingroup logging * Get session description. * * @param session The CoAP session. - * @return description string + * @return description string. */ const char *coap_session_str(const coap_session_t *session); @@ -332,10 +333,11 @@ void coap_free_endpoint(coap_endpoint_t *ep); /** + * @ingroup logging * Get endpoint description. * * @param endpoint The CoAP endpoint. -* @return description string +* @return description string. */ const char *coap_endpoint_str(const coap_endpoint_t *endpoint); diff --git a/include/coap/debug.h b/include/coap/debug.h index 32f4b44a..8664acb8 100644 --- a/include/coap/debug.h +++ b/include/coap/debug.h @@ -10,52 +10,98 @@ #ifndef _COAP_DEBUG_H_ #define _COAP_DEBUG_H_ +/** + * @defgroup logging Logging Support + * API functions for logging support + * @{ + */ + #ifndef COAP_DEBUG_FD +/** + * Used for output for @c LOG_DEBUG to @c LOG_ERR. + */ #define COAP_DEBUG_FD stdout #endif #ifndef COAP_ERR_FD +/** + * Used for output for @c LOG_CRIT to @c LOG_EMERG. + */ #define COAP_ERR_FD stderr #endif #ifdef HAVE_SYSLOG_H #include +/** + * Logging type. One of LOG_* from @b syslog. + */ typedef short coap_log_t; #else /** Pre-defined log levels akin to what is used in \b syslog. */ typedef enum { - LOG_EMERG=0, - LOG_ALERT, - LOG_CRIT, - LOG_ERR, - LOG_WARNING, - LOG_NOTICE, - LOG_INFO, - LOG_DEBUG + LOG_EMERG=0, /**< Emergency */ + LOG_ALERT, /**< Alert */ + LOG_CRIT, /**< Critical */ + LOG_ERR, /**< Error */ + LOG_WARNING, /**< Warning */ + LOG_NOTICE, /**< Notice */ + LOG_INFO, /**< Information */ + LOG_DEBUG /**< Debug */ } coap_log_t; #endif -/** Returns the current log level. */ +/** + * Get the current logging level. + * + * @return One of the LOG_* values. + */ coap_log_t coap_get_log_level(void); -/** Sets the log level to the specified value. */ +/** + * Sets the log level to the specified value. + * + * @param level One of the LOG_* values. + */ void coap_set_log_level(coap_log_t level); +/** + * Logging call-back handler definition. + * + * @param level One of the LOG_* values. + * @param message Zero-terminated string message to log. + */ typedef void (*coap_log_handler_t) (coap_log_t level, const char *message); -/** Add a custom log callback, use NULL to reset default handler */ +/** + * Add a custom log callback handler. + * + * @param handler The logging handler to use or @p NULL to use default handler. + */ void coap_set_log_handler(coap_log_handler_t handler); -/** Returns a zero-terminated string with the name of this library. */ +/** + * Get the library package name. + * + * @return Zero-terminated string with the name of this library. + */ const char *coap_package_name(void); -/** Returns a zero-terminated string with the library version. */ +/** + * Get the library package version. + * + * @return Zero-terminated string with the library version. + */ const char *coap_package_version(void); /** * Writes the given text to @c COAP_ERR_FD (for @p level <= @c LOG_CRIT) or @c - * COAP_DEBUG_FD (for @p level >= @c LOG_WARNING). The text is output only when + * COAP_DEBUG_FD (for @p level >= @c LOG_ERR). The text is output only when * @p level is below or equal to the log level that set by coap_set_log_level(). + * + * Internal function. + * + * @param level One of the LOG_* values. + & @param format The format string to use. */ #if (defined(__GNUC__)) void coap_log_impl(coap_log_t level, @@ -65,12 +111,32 @@ void coap_log_impl(coap_log_t level, const char *format, ...); #endif #ifndef coap_log +/** + * Logging function. + * Writes the given text to @c COAP_ERR_FD (for @p level <= @c LOG_CRIT) or @c + * COAP_DEBUG_FD (for @p level >= @c LOG_ERR). The text is output only when + * @p level is below or equal to the log level that set by coap_set_log_level(). + * + * @param level One of the LOG_* values. + */ #define coap_log(level, ...) do { if ((level)<=(int)coap_get_log_level()) coap_log_impl((level), __VA_ARGS__); } while(0) #endif /* A set of convenience macros for common log levels. */ +/** + * Obsoleted. + * @deprecated Use coap_log(LOG_INFO, ...). + */ #define info(...) coap_log(LOG_INFO, __VA_ARGS__) +/** + * Obsoleted. + * @deprecated Use coap_log(LOG_WARNING, ...). + */ #define warn(...) coap_log(LOG_WARNING, __VA_ARGS__) +/** + * Obsoleted. + * @deprecated Use coap_log(LOG_DEBUG, ...). + */ #define debug(...) coap_log(LOG_DEBUG, __VA_ARGS__) #include "pdu.h" @@ -78,23 +144,62 @@ void coap_log_impl(coap_log_t level, const char *format, ...); /** * Defines the output mode for the coap_show_pdu() function. * - * @param use_fprintf 1 if the output is to use fprintf() (the default) - * 0 if the output is to use coap_log() + * @param use_fprintf @p 1 if the output is to use fprintf() (the default) + * @p 0 if the output is to use coap_log(). */ void coap_set_show_pdu_output(int use_fprintf); /** * Display the contents of the specified @p pdu. + * Note: The output method of coap_show_pdu() is dependent on the setting of + * coap_set_show_pdu_output(). * - * @param level The required minimum logging level - * @param pdu The PDU to decode + * @param level The required minimum logging level. + * @param pdu The PDU to decode. */ void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu); struct coap_address_t; -size_t coap_print_addr(const struct coap_address_t *, unsigned char *, size_t); +/** + * Print the address into the defined buffer. + * + * Internal Function. + * + * @param address The address to print. + * @param buffer The buffer to print into. + * @param size The size of the buffer to print into. + * + * @return The amount written into the buffer. + */ +size_t coap_print_addr(const struct coap_address_t *address, + unsigned char *buffer, size_t size); + +/** @} */ + +/** + * Set the packet loss level for testing. This can be in one of two forms. + * + * Percentage : 0% to 100%. Use the specified probability. + * 0% is send all packets, 100% is drop all packets. + * + * List: A comma seperated list of numbers or number ranges that are the + * packets to drop. + * + * @param loss_level The defined loss level (percentage or list). + * + * @return @c 1 If loss level set, @c 0 if there is an error. + */ int coap_debug_set_packet_loss(const char *loss_level); + +/** + * Check to see whether a packet should be sent or not. + * + * Internal function + * + * @return @c 1 if packet is to be sent, @c 0 if packet is to be dropped. + */ int coap_debug_send_packet(void); + #endif /* _COAP_DEBUG_H_ */ diff --git a/man/Makefile.am b/man/Makefile.am index a0206501..426d9490 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -14,6 +14,7 @@ TXT3 = coap_attribute.txt \ coap_context.txt \ coap_encryption.txt \ coap_handler.txt \ + coap_logging.txt \ coap_observe.txt \ coap_pdu_setup.txt \ coap_recovery.txt \ @@ -56,6 +57,8 @@ install-man: install-man3 install-man5 install-man7 @echo ".so man3/coap_pdu_setup.3" > coap_split_query.3 @echo ".so man3/coap_session.3" > coap_session_get_app_data.3 @echo ".so man3/coap_session.3" > coap_session_set_app_data.3 + @echo ".so man3/coap_logging.3" > coap_endpoint_str.3 + @echo ".so man3/coap_logging.3" > coap_session_str.3 $(INSTALL_DATA) *.3 "$(DESTDIR)$(man3dir)" CLEANFILES = *.3 *.5 *.7 *.xml diff --git a/man/coap.txt.in b/man/coap.txt.in index e3b6c17a..2a197803 100644 --- a/man/coap.txt.in +++ b/man/coap.txt.in @@ -35,8 +35,8 @@ examples directory. SEE ALSO -------- *coap_attribute*(3), *coap_context*(3), *coap_encryption*(3), *coap_handler*(3), -*coap_observe*(3), *coap_pdu_setup*(3), *coap_session*(3), *coap_recovery*(3), -*coap_resource*(3) and *coap_tls_library*(3) +*coap_logging*(3), *coap_observe*(3), *coap_pdu_setup*(3), *coap_recovery*(3), +*coap_resource*(3), *coap_session*(3) and *coap_tls_library*(3) For example executables, see *coap-client*(5), *coap-rd*(5) and *coap-server*(5) diff --git a/man/coap_context.txt.in b/man/coap_context.txt.in index 16697d37..3154e487 100644 --- a/man/coap_context.txt.in +++ b/man/coap_context.txt.in @@ -40,8 +40,6 @@ const coap_address_t *_listen_addr_, coap_proto_t _proto_);* *void coap_endpoint_set_default_mtu(coap_endpoint_t *_endpoint_, unsigned _mtu_);* -*const char *coap_endpoint_str(const coap_endpoint_t *_endpoint_);* - Link with *-lcoap-@LIBCOAP_API_VERSION@*, *-lcoap-@LIBCOAP_API_VERSION@-openssl* or *-lcoap-@LIBCOAP_API_VERSION@-tinydtls* depending on your (D)TLS library type. @@ -148,9 +146,6 @@ The *coap_endpoint_set_default_mtu*() function is used to set the MTU size (the maximum message size) of the data in a packet, excluding any IP or TCP/UDP overhead to _mtu_ for the _endpoint_. A sensible default is 1280. -The *coap_endpoint_str*() function returns a description string of the -_endpoint_. - RETURN VALUES ------------- *coap_new_context*() function returns a newly created context or @@ -162,9 +157,6 @@ NULL if there is a creation failure. *coap_new_endpoint*() function returns a newly created endpoint or NULL if there is a creation failure. -*coap_endpoint_str*() function returns a description string of the -_endpoint_. - EXAMPLES -------- *CoAP Server Non-Encrypted Setup* diff --git a/man/coap_logging.txt.in b/man/coap_logging.txt.in new file mode 100644 index 00000000..b2f0fb67 --- /dev/null +++ b/man/coap_logging.txt.in @@ -0,0 +1,165 @@ +// -*- mode:doc; -*- +// vim: set syntax=asciidoc,tw=0: + +coap_logging(3) +================= +:doctype: manpage +:man source: coap_logging +:man version: @PACKAGE_VERSION@ +:man manual: libcoap Manual + +NAME +---- +coap_logging, coap_log, coap_get_log_level, coap_set_log_level, +coap_set_log_handler, coap_package_name, coap_package_version, +coap_set_show_pdu_output, coap_show_pdu +- work with CoAP logging + +SYNOPSIS +-------- +*#include * + +*void coap_log(coap_log_t _level_, const char *_format_, ...);* + +*void coap_set_log_level(coap_log_t _level_);* + +*coap_log_t coap_get_log_level(void);* + +*void coap_dtls_set_log_level(int _level_);* + +*int coap_dtls_get_log_level(void);* + +*void coap_set_log_handler(coap_log_handler_t _handler_);* + +*const char *coap_package_name(void);* + +*const char *coap_package_version(void);* + +*void coap_set_show_pdu_output(int _use_fprintf_);* + +*void coap_show_pdu(coap_log_t _level_, const coap_pdu_t *_pdu_);* + +*const char *coap_endpoint_str(const coap_endpoint_t *_endpoint_);* + +*const char *coap_session_str(const coap_session_t *_session_);* + +Link with *-lcoap-@LIBCOAP_API_VERSION@*, *-lcoap-@LIBCOAP_API_VERSION@-openssl* +or *-lcoap-@LIBCOAP_API_VERSION@-tinydtls* depending on your (D)TLS library +type. + +DESCRIPTION +----------- +The logging sub-system supports logging at different levels, and depending on +the selected logging level, outputs the appropriate information. + +Logging by default is to stderr or stdout depending on the logging level of +the log entry. It ia possible to send the logging information to an +application logging call-back handler for processing by the application. + +The *coap_log*() funtion is used to log information at the appropriate _level_. +The rest of the parameters follow the standard *printf*() function format. + +Logging levels (*coap_log_t*) are defined by (the same as for *syslog*()), which +are numerically incrementing in value: + +*LOG_EMERG*:: +Emergency level. + +*LOG_ALERT*:: +Alert level. + +*LOG_CRIT*:: +Critical level. + +*LOG_ERR*:: +Error level. + +*LOG_WARNING*:: +Warning level (the default). + +*LOG_NOTICE*:: +Notice level. + +*LOG_INFO*:: +Information level. + +*LOG_DEBUG*:: +Debug level. + +The *coap_set_log_level*() function is used to set the current logging _level_ +for output by any subsequent *coap_log*() calls. Output is only logged if the +*coap_log*() _level_ definition is smaller than or equal to the curent logging +_level_. + +The *coap_get_log_level*() function is used to get the current logging level. + +The *coap_dtls_set_log_level*() function is used to set the logging _level_ +for output by the DTLS library for specific DTLS information. Usually, both +*coap_set_log_level*() and *coap_dtls_set_log_level*() would be set to the +same _level_ value. + +The *coap_dtls_get_log_level*() function is used to get the current logging +level for the DTLS library. + +The *coap_set_log_handler*()* function can be used to define an alternative +logging handler for processing the logging messages. The logging handler +prototype is defined as: + +[source, c] +---- +typedef void (*coap_log_handler_t) (coap_log_t level, const char *message); +---- + +The *coap_package_name*() function returns the name of this library. + +The *coap_package_version*() function returns the version of this library. + +The *coap_set_show_pdu_output*() function defines whether the output from +*coap_show_pdu*() is to be either sent to stdout/stderr, or output using +*coap_log*(). _use_fprintf_ is set to 1 for stdout/stderr (the default), and +_use_fprintf_ is set to 0 for *coap_log*(). + +The *coap_show_pdu*() function is used to decode the _pdu_, outputing as +appropriate for logging _level_. Where the output goes is dependent on +*coap_set_show_pdu_output*(). + +The *coap_endpoint_str*() function returns a description string of the +_endpoint_. + +The *coap_session_str*() function is used to get a string containing the +information about the _session_. + +RETURN VALUES +------------- + +The *coap_package_name*() and *coap_package_version*() return the appropriate +zero-terminated character string. + +The *coap_get_log_level*() function returns the current logging level. + +The *coap_dtls_get_log_level*() function returns the current logging level +for the DTLS library specifics. + +The *coap_endpoint_str*() function returns a description string of the +_endpoint_. + +The *coap_session_str*() function returns a description string of the +_session_. + +SEE ALSO +-------- +*coap_context*(3) and *coap_session*(3) + +FURTHER INFORMATION +------------------- +See "RFC7252: The Constrained Application Protocol (CoAP)" for further +information. + +BUGS +---- +Please report bugs on the mailing list for libcoap: +libcoap-developers@lists.sourceforge.net + +AUTHORS +------- +The libcoap project