![]() |
Paho Asynchronous MQTT C Client Library
|
Runtime tracing can be controlled by environment variables or API calls. #### Environment variables Tracing is switched on by setting the MQTT_C_CLIENT_TRACE environment variable. A value of ON, or stdout, prints to stdout, any other value is interpreted as a file name to use. The amount of trace detail is controlled with the MQTT_C_CLIENT_TRACE_LEVEL environment variable - valid values are ERROR, PROTOCOL, MINIMUM, MEDIUM and MAXIMUM (from least to most verbose). The variable MQTT_C_CLIENT_TRACE_MAX_LINES limits the number of lines of trace that are output to a file. Two files are used at most, when they are full, the last one is overwritten with the new trace entries. The default size is 1000 lines. #### Trace API calls MQTTAsync_traceCallback() is used to set a callback function which is called whenever trace information is available. This will be the same information as that printed if the environment variables were used to control the trace. The MQTTAsync_setTraceLevel() calls is used to set the maximum level of trace entries that will be passed to the callback function. The levels are: 1. ::MQTTASYNC_TRACE_MAXIMUM 2. ::MQTTASYNC_TRACE_MEDIUM 3. ::MQTTASYNC_TRACE_MINIMUM 4. ::MQTTASYNC_TRACE_PROTOCOL 5. ::MQTTASYNC_TRACE_ERROR 6. ::MQTTASYNC_TRACE_SEVERE 7. ::MQTTASYNC_TRACE_FATAL Selecting ::MQTTASYNC_TRACE_MAXIMUM will cause all trace entries at all levels to be returned. Choosing ::MQTTASYNC_TRACE_ERROR will cause ERROR, SEVERE and FATAL trace entries to be returned to the callback function. ### MQTT Packet Tracing A feature that can be very useful is printing the MQTT packets that are sent and received. To achieve this, use the following environment variable settings:
The output you should see looks like this:
where the fields are:
This is an extract of a default level trace of a call to connect:
where the fields are:
Setting the trace level to maximum causes memory allocations and frees to be traced along with the default trace entries, with messages like the following:
When the last MQTT client object is destroyed, if the trace is being recorded and all memory allocated by the client library has not been freed, an error message will be written to the trace. This can help with fixing memory leaks. The message will look like this: