samples: net: mqtt_publisher: Use logging macros for output

Instead of printk(), use logging macros so that all the output
from IP stack and sample is nicely interleaved.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2019-09-25 12:26:30 +03:00 committed by Andrew Boie
commit f2af3b66ff

View file

@ -11,7 +11,6 @@ LOG_MODULE_REGISTER(net_mqtt_publisher_sample, LOG_LEVEL_DBG);
#include <net/socket.h>
#include <net/mqtt.h>
#include <sys/printk.h>
#include <string.h>
#include <errno.h>
@ -118,7 +117,7 @@ static void wait(int timeout)
{
if (nfds > 0) {
if (poll(fds, nfds, timeout) < 0) {
printk("poll error: %d\n", errno);
LOG_ERR("poll error: %d", errno);
}
}
}
@ -131,18 +130,17 @@ void mqtt_evt_handler(struct mqtt_client *const client,
switch (evt->type) {
case MQTT_EVT_CONNACK:
if (evt->result != 0) {
printk("MQTT connect failed %d\n", evt->result);
LOG_ERR("MQTT connect failed %d", evt->result);
break;
}
connected = true;
printk("[%s:%d] MQTT client connected!\n", __func__, __LINE__);
LOG_INF("MQTT client connected!");
break;
case MQTT_EVT_DISCONNECT:
printk("[%s:%d] MQTT client disconnected %d\n", __func__,
__LINE__, evt->result);
LOG_INF("MQTT client disconnected %d", evt->result);
connected = false;
clear_fds();
@ -151,23 +149,21 @@ void mqtt_evt_handler(struct mqtt_client *const client,
case MQTT_EVT_PUBACK:
if (evt->result != 0) {
printk("MQTT PUBACK error %d\n", evt->result);
LOG_ERR("MQTT PUBACK error %d", evt->result);
break;
}
printk("[%s:%d] PUBACK packet id: %u\n", __func__, __LINE__,
evt->param.puback.message_id);
LOG_INF("PUBACK packet id: %u", evt->param.puback.message_id);
break;
case MQTT_EVT_PUBREC:
if (evt->result != 0) {
printk("MQTT PUBREC error %d\n", evt->result);
LOG_ERR("MQTT PUBREC error %d", evt->result);
break;
}
printk("[%s:%d] PUBREC packet id: %u\n", __func__, __LINE__,
evt->param.pubrec.message_id);
LOG_INF("PUBREC packet id: %u", evt->param.pubrec.message_id);
const struct mqtt_pubrel_param rel_param = {
.message_id = evt->param.pubrec.message_id
@ -175,19 +171,19 @@ void mqtt_evt_handler(struct mqtt_client *const client,
err = mqtt_publish_qos2_release(client, &rel_param);
if (err != 0) {
printk("Failed to send MQTT PUBREL: %d\n", err);
LOG_ERR("Failed to send MQTT PUBREL: %d", err);
}
break;
case MQTT_EVT_PUBCOMP:
if (evt->result != 0) {
printk("MQTT PUBCOMP error %d\n", evt->result);
LOG_ERR("MQTT PUBCOMP error %d", evt->result);
break;
}
printk("[%s:%d] PUBCOMP packet id: %u\n", __func__, __LINE__,
evt->param.pubcomp.message_id);
LOG_INF("PUBCOMP packet id: %u",
evt->param.pubcomp.message_id);
break;
@ -243,8 +239,7 @@ static int publish(struct mqtt_client *client, enum mqtt_qos qos)
#define RC_STR(rc) ((rc) == 0 ? "OK" : "ERROR")
#define PRINT_RESULT(func, rc) \
printk("[%s:%d] %s: %d <%s>\n", __func__, __LINE__, \
(func), rc, RC_STR(rc))
LOG_INF("%s: %d <%s>", (func), rc, RC_STR(rc))
static void broker_init(void)
{
@ -411,7 +406,7 @@ static void publisher(void)
{
int i, rc;
printk("attempting to connect: ");
LOG_INF("attempting to connect: ");
rc = try_to_connect(&client_ctx);
PRINT_RESULT("try_to_connect", rc);
SUCCESS_OR_EXIT(rc);
@ -454,7 +449,7 @@ static void publisher(void)
rc = mqtt_input(&client_ctx);
PRINT_RESULT("mqtt_input", rc);
printk("\nBye!\n");
LOG_INF("Bye!");
}
void main(void)