Skip to content

Commit

Permalink
mosquitto_sub %j and %J timestamps are now in a ISO 8601 compatible f…
Browse files Browse the repository at this point in the history
…ormat.
  • Loading branch information
ralight committed May 6, 2020
1 parent e755827 commit b726e2f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions ChangeLog.txt
Expand Up @@ -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
Expand Down
21 changes: 14 additions & 7 deletions client/sub_client_output.c
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions man/mosquitto_rr.1.xml
Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions man/mosquitto_sub.1.xml
Expand Up @@ -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>

Expand Down

0 comments on commit b726e2f

Please sign in to comment.