---
name: "coap_logging"
section: "3"
platform: "linux"
package: "libcoap3t64"
version: "4.3.4-1.1+deb13u3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/coap_logging"
---

# coap\_logging(3)

Work with CoAP logging

## NAME

coap\_logging, coap\_log, coap\_log\_emerg, coap\_log\_alert, coap\_log\_crit, coap\_log\_err, coap\_log\_warn, coap\_log\_info, coap\_log\_notice, coap\_log\_debug, coap\_log\_oscore, coap\_get\_log\_level, coap\_set\_log\_level, coap\_dtls\_log, coap\_dtls\_get\_log\_level, coap\_dtls\_set\_log\_level, coap\_set\_log\_handler, coap\_package\_name, coap\_package\_version, coap\_package\_build, coap\_set\_show\_pdu\_output, coap\_show\_pdu, coap\_endpoint\_str, coap\_session\_str, coap\_print\_addr, coap\_print\_ip\_addr - Work with CoAP logging

## SYNOPSIS

**#include \<coap3/coap.h\>**

**void coap\_log(coap\_log\_t** *level***, const char \****format***,** *...***);**

**void coap\_log\_emerg(const char \****format***,** *...***);**

**void coap\_log\_alert(const char \****format***,** *...***);**

**void coap\_log\_crit(const char \****format***,** *...***);**

**void coap\_log\_err(const char \****format***,** *...***);**

**void coap\_log\_warn(const char \****format***,** *...***);**

**void coap\_log\_info(const char \****format***,** *...***);**

**void coap\_log\_notice(const char \****format***,** *...***);**

**void coap\_log\_debug(const char \****format***,** *...***);**

**void coap\_log\_oscore(const char \****format***,** *...***);**

**void coap\_set\_log\_level(coap\_log\_t** *level***);**

**coap\_log\_t coap\_get\_log\_level(void);**

**void coap\_dtls\_log(coap\_log\_t** *level***, const char \****format***,** *...***);**

**void coap\_dtls\_set\_log\_level(coap\_log\_t** *level***);**

**coap\_log\_t 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);**

**const char \*coap\_package\_build(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***);**

**size\_t coap\_print\_addr(const coap\_address\_t \****address***, unsigned char \****buffer***, size\_t** *length***);**

**const char \*coap\_print\_ip\_addr(const coap\_address\_t \****address***, char \****buffer***, size\_t** *length***);**

For specific (D)TLS library support, link with **-lcoap-3-notls**, **-lcoap-3-gnutls**, **-lcoap-3-openssl**, **-lcoap-3-mbedtls** or **-lcoap-3-tinydtls**. Otherwise, link with **-lcoap-3** to get the default (D)TLS library support.

## 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 callback handler for processing by the application.

Logging levels (**coap\_log\_t**) are defined as follows (based on the **syslog**() names and values):

**COAP\_LOG\_EMERG**

Emergency level (0).

**COAP\_LOG\_ALERT**

Alert level (1).

**COAP\_LOG\_CRIT**

Critical level (2).

**COAP\_LOG\_ERR**

Error level (3).

**COAP\_LOG\_WARN**

Warning level (the default) (4).

**COAP\_LOG\_NOTICE**

Notice level (5).

**COAP\_LOG\_INFO**

Information level (6).

**COAP\_LOG\_DEBUG**

Debug level (7).

With additional level:

**COAP\_LOG\_OSCORE**

Debug OSCORE information (8).

**NOTE:** The maximum logging level in the libcoap library may have been updated by the use of *./configure --enable-max-logging-level=X* (where X is 0 to 8 inclusive) which may disable some of the higher logging levels to save code space.

## FUNCTIONS

**Function: coap\_log()**

The **coap\_log**() function is used to log information at the appropriate *level*. The rest of the parameters follow the standard **printf**() function format. Where possible, the coap\_log\_\*() functions should be used instead.

**Function: coap\_log\_emerg()**

The **coap\_log\_emerg**() function provides a wrapper to the **coap\_log**() function with *level* set to COAP\_LOG\_EMERG. The parameters follow the standard **printf**() function format.

**Function: coap\_log\_alert()**

The **coap\_log\_alert**() function provides a wrapper to the **coap\_log**() function with *level* set to COAP\_LOG\_ALERT. The parameters follow the standard **printf**() function format.

**Function: coap\_log\_crit()**

The **coap\_log\_crit**() function provides a wrapper to the **coap\_log**() function with *level* set to COAP\_LOG\_CRIT. The parameters follow the standard **printf**() function format.

**Function: coap\_log\_err()**

The **coap\_log\_err**() function provides a wrapper to the **coap\_log**() function with *level* set to COAP\_LOG\_ERR. The parameters follow the standard **printf**() function format.

**Function: coap\_log\_warn()**

The **coap\_log\_warn**() function provides a wrapper to the **coap\_log**() function with *level* set to COAP\_LOG\_WARN. The parameters follow the standard **printf**() function format.

**Function: coap\_log\_notice()**

The **coap\_log\_notice**() function provides a wrapper to the **coap\_log**() function with *level* set to COAP\_LOG\_NOTICE. The parameters follow the standard **printf**() function format.

**Function: coap\_log\_info()**

The **coap\_log\_info**() function provides a wrapper to the **coap\_log**() function with *level* set to COAP\_LOG\_INFO. The parameters follow the standard **printf**() function format.

**Function: coap\_log\_debug()**

The **coap\_log\_debug**() function provides a wrapper to the **coap\_log**() function with *level* set to COAP\_LOG\_DEBUG. The parameters follow the standard **printf**() function format.

**Function: coap\_log\_oscore()**

The **coap\_log\_oscore**() function provides a wrapper to the **coap\_log**() function with *level* set to COAP\_LOG\_OSCORE. The parameters follow the standard **printf**() function format.

**Function: coap\_set\_log\_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 current logging *level*.

**Function: coap\_get\_log\_level()**

The **coap\_get\_log\_level**() function is used to get the current logging level.

**Function: coap\_dtls\_log()**

The **coap\_dtls\_log**() function is used to log (D)TLS library information at the appropriate *level*. The rest of the parameters follow the standard **printf**() function format.

**Function: coap\_dtls\_set\_log\_level()**

The **coap\_dtls\_set\_log\_level**() function is used to set the current logging *level* for output by any subsequent **coap\_dtls\_log**() calls. Output is only logged if the **coap\_dtls\_log**() *level* definition is smaller than or equal to the current DTLS logging *level*.

**Function: coap\_dtls\_get\_log\_level()**

The **coap\_dtls\_get\_log\_level**() function is used to get the current logging level for the DTLS library.

**Function: coap\_set\_log\_handler()**

The **coap\_set\_log\_handler**()\* function can be used to define an alternative logging handler for processing any logging messages. The logging handler prototype is defined as:

```
typedef void (*coap_log_handler_t) (coap_log_t level, const char *message);
```

**Function: coap\_package\_name()**

The **coap\_package\_name**() function returns the name of this library.

**Function: coap\_package\_version()**

The **coap\_package\_version**() function returns the version of this library.

**Function: coap\_package\_build()**

The **coap\_package\_build**() function returns the git information (as in "git describe --tags") for the build of this library or version of this library if git is not used.

**Function: coap\_set\_show\_pdu\_output()**

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**().

**Function: coap\_show\_pdu()**

The **coap\_show\_pdu**() function is used to decode the *pdu*, outputting as appropriate for logging *level*. Where the output goes is dependent on **coap\_set\_show\_pdu\_output**().

**NOTE:** If *pdu* has not been associated with a CoAP session (i.e. not a received PDU or coap\_send() not yet called), then the output assumes that this *pdu* is of type unreliable.

**Function: coap\_endpoint\_str()**

The **coap\_endpoint\_str**() function returns a description string of the *endpoint*.

**Function: coap\_session\_str()**

The **coap\_session\_str**() function is used to get a string containing the information about the *session*.

**Function: coap\_print\_addr()**

The **coap\_print\_addr**() function returns the length of the description string containing the IP address and port from *address*, updating *buffer* which has a maximum length *length*.

**Function: coap\_print\_ip\_addr()**

The **coap\_print\_ip\_addr**() function returns a description string of the IP address only for *address*. *buffer* is updated, which has a maximum length of *length*.

## RETURN VALUES

**coap\_package\_name**(), **coap\_package\_version**() and **coap\_package\_build**() return the appropriate zero-terminated character string.

**coap\_get\_log\_level**() returns the current logging level.

**coap\_dtls\_get\_log\_level**() returns the current logging level for the DTLS library specifics.

**coap\_endpoint\_str**() returns a description string of the *endpoint*.

**coap\_session\_str**() returns a description string of the *session*.

**coap\_print\_addr**() returns the length of *buffer* that has been updated with an ascii readable IP address and port for *address*.

**coap\_print\_ip\_addr**() returns a pointer to the ascii readable IP address only for *address*.

## SEE ALSO

[**coap\_context**(3)](/coap_context/3.md) and [**coap\_session**(3)](/coap_session/3.md)

## 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 or raise an issue on GitHub at https://github.com/obgm/libcoap/issues

## AUTHORS

The libcoap project \<libcoap-developers@lists.sourceforge.net\>
