paho c客户端如何输出log

来源:互联网 发布:都有什么数据库 编辑:程序博客网 时间:2024/05/21 20:59
paho c客户端是phao提供的一个针对mosquitto协议的客户端实现版本,以动态库的形式提供,使用C语言实现。

直接使用的话,无法看到paho输出的log,有时候一些问题的分析和定位需要追踪log,这就需要自己进行一些设置,我是在windows下使用phao,使用的工具是vs2013,这里主要针对这个环境进行讲解。
首先,我们查看phao的官方文档,看是否提供了log输出功能,我们在官方的网站:http://www.eclipse.org/paho/files/mqttdoc/Cclient/查找log相关的部分:
点击红框标注处
点击红框标注处,我们可以进入log输出页面,可以看到对Tracing设置的一些说明,可以看到时间log设置非常简单,

Runtime tracing is controlled by environment variables.

Tracing is switched on by setting MQTT_C_CLIENT_TRACE. 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.

这里要求我们设置环境变量MQTT_C_CLIENT_TRACE和MQTT_C_CLIENT_TRACE_LEVEL,设置内容为:
MQTT_C_CLIENT_TRACE=ON
MQTT_C_CLIENT_TRACE_LEVEL=PROTOCOL

MQTT_C_CLIENT_TRACE控制log的打开, MQTT_C_CLIENT_TRACE_LEVEL设置log的等级,等级表示如下:

log等级 说明 ERROR ERROR log输出较少,出现ERROR情况下打印log PROTOCOL 只打印mosquitto协议相关的log MINIMUM 输出全部log MEDIUM 输出全部log MAXIMUM 输出全部log,包括内存分配和释放

后三种log输出内容基本一致,log可以输出,这时另外一个问题出现了:如何在vs2013中设置环境变量?
可以在windows下手动设置环境变量,但是这种方法对程序员来说,有点不合适,有没有更好的方法,当然有!

#define setenv(a, b, c) _putenv_s(a, b)setenv("MQTT_C_CLIENT_TRACE", "ON", 1); // same as 'stdout'setenv("MQTT_C_CLIENT_TRACE_LEVEL", "MAXIMUM", 1);  //ERROR, PROTOCOL, MINIMUM, MEDIUM and MAXIMUM

实际上使用的是Windows的库函数:_putenv_s
详细使用可以参照链接:https://msdn.microsoft.com/zh-cn/library/eyw7eyfw.aspx

在自己使用paho库的地方这样设置,就会把log输出出来,具体的log见下:
这里写图片描述
说明:1. date 2. time 3. thread id 4. function nesting level 5. function entry (>) or exit (<) 6. function name : line of source code file 7. return value (if there is one)

使用时请注意,MT和MD的编译设置要统一,否则无法输出log

以上,就是本文的全部内容,希望对大家有帮助!

0 0
原创粉丝点击