mosquitto_sub %j and %J timestamps are now in a ISO 8601 compatible format.

pull/1694/head
Roger A. Light 5 years ago
parent e755827f4f
commit b726e2f1ec

@ -46,6 +46,7 @@ Clients:
easily set for MQTT v5 clients.
- Add `--random-filter` to mosquitto_sub, to allow only a certain proportion
of received messages to be printed.
- mosquitto_sub %j and %J timestamps are now in a ISO 8601 compatible format.
1.6.9 - 20200227

@ -208,8 +208,9 @@ static int json_print_properties(cJSON *root, const mosquitto_property *properti
#endif
static int json_print(const struct mosquitto_message *message, const mosquitto_property *properties, const struct tm *ti, bool escaped, bool pretty)
static int json_print(const struct mosquitto_message *message, const mosquitto_property *properties, const struct tm *ti, int ns, bool escaped, bool pretty)
{
char buf[100];
#ifdef WITH_CJSON
cJSON *root;
cJSON *tmp;
@ -221,7 +222,11 @@ static int json_print(const struct mosquitto_message *message, const mosquitto_p
return MOSQ_ERR_NOMEM;
}
tmp = cJSON_CreateNumber(time(NULL));
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.000000Z%z", ti);
snprintf(&buf[strlen("2020-05-06T21:48:00.")], 9, "%06d", ns/1000);
buf[strlen("2020-05-06T21:48:00.000000")] = 'Z';
tmp = cJSON_CreateStringReference(buf);
if(tmp == NULL){
cJSON_Delete(root);
return MOSQ_ERR_NOMEM;
@ -304,10 +309,12 @@ static int json_print(const struct mosquitto_message *message, const mosquitto_p
return MOSQ_ERR_SUCCESS;
#else
char buf[100];
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.000000Z%z", ti);
snprintf(&buf[strlen("2020-05-06T21:48:00.")], 9, "%06d", ns/1000);
buf[strlen("2020-05-06T21:48:00.000000")] = 'Z';
strftime(buf, 100, "%s", ti);
printf("{\"tst\":%s,\"topic\":\"%s\",\"qos\":%d,\"retain\":%d,\"payloadlen\":%d,", buf, message->topic, message->qos, message->retain, message->payloadlen);
tmp = cJSON_CreateStringReference(buf);
printf("{\"tst\":\"%s\",\"topic\":\"%s\",\"qos\":%d,\"retain\":%d,\"payloadlen\":%d,", buf, message->topic, message->qos, message->retain, message->payloadlen);
if(message->qos > 0){
printf("\"mid\":%d,", message->mid);
}
@ -403,7 +410,7 @@ static void formatted_print(const struct mosq_config *lcfg, const struct mosquit
return;
}
}
if(json_print(message, properties, ti, true, lcfg->pretty) != MOSQ_ERR_SUCCESS){
if(json_print(message, properties, ti, ns, true, lcfg->pretty) != MOSQ_ERR_SUCCESS){
err_printf(lcfg, "Error: Out of memory.\n");
return;
}
@ -416,7 +423,7 @@ static void formatted_print(const struct mosq_config *lcfg, const struct mosquit
return;
}
}
rc = json_print(message, properties, ti, false, lcfg->pretty);
rc = json_print(message, properties, ti, ns, false, lcfg->pretty);
if(rc == MOSQ_ERR_NOMEM){
err_printf(lcfg, "Error: Out of memory.\n");
return;

@ -728,13 +728,13 @@
<listitem><para><option>%j</option> JSON output of message
parameters and timestamp, with a quoted and escaped
payload. For example
<code>{"tst":1470825369,"topic":"greeting","qos":0,"retain":0,"payload":"hello
<code>{"tst":"2020-05-06T22:12:00.000000+0100","topic":"greeting","qos":0,"retain":0,"payload":"hello
world"}</code></para></listitem>
<listitem><para><option>%J</option> JSON output of message
parameters and timestamp, with a non-quoted and
non-escaped payload - this means the payload must
itself be valid JSON. For example:
<code>{"tst":1470825369,"topic":"foo","qos":0,"retain":0,"payload":{"temperature":27.0,"humidity":57}}</code>.</para>
<code>{"tst":"2020-05-06T22:12:00.000000+0100","topic":"foo","qos":0,"retain":0,"payload":{"temperature":27.0,"humidity":57}}</code>.</para>
<para>If the payload is not valid JSON, then the error message "Error: Message payload is not valid JSON on topic
&lt;topic&gt;" will be printed to stderr.</para></listitem>
<listitem><para><option>%I</option> ISO-8601 format date and time, e.g. 2016-08-10T09:47:38+0100</para></listitem>

@ -857,13 +857,13 @@ mosquitto_sub -t 'bbc/#' -T bbc/bbc1 --remove-retained</programlisting>
<listitem><para><option>%j</option> JSON output of message
parameters and timestamp, with a quoted and escaped
payload. For example
<code>{"tst":1470825369,"topic":"greeting","qos":0,"retain":0,"payload":"hello
<code>{"tst":"2020-05-06T22:12:00.000000+0100","topic":"greeting","qos":0,"retain":0,"payload":"hello
world"}</code></para></listitem>
<listitem><para><option>%J</option> JSON output of message
parameters and timestamp, with a non-quoted and
non-escaped payload - this means the payload must
itself be valid JSON. For example:
<code>{"tst":1470825369,"topic":"foo","qos":0,"retain":0,"payload":{"temperature":27.0,"humidity":57}}</code>.</para>
<code>{"tst":"2020-05-06T22:12:00.000000+0100","topic":"foo","qos":0,"retain":0,"payload":{"temperature":27.0,"humidity":57}}</code>.</para>
<para>If the payload is not valid JSON, then the error message "Error: Message payload is not valid JSON on topic
&lt;topic&gt;" will be printed to stderr.</para></listitem>

Loading…
Cancel
Save