Add `--pretty` option to mosquitto_sub/rr

If active, this produces formatted JSON output rather than the normal
minimised output.
pull/1480/head
Roger A. Light 6 years ago
parent e5237ae7e5
commit 9e4226622f

@ -24,6 +24,8 @@ Clients:
- Use cJSON library for producing JSON output, where available. Closes #1222. - Use cJSON library for producing JSON output, where available. Closes #1222.
- Add support for outputting MQTT v5 property information to mosquitto_sub/rr - Add support for outputting MQTT v5 property information to mosquitto_sub/rr
JSON output. JSON output.
- Add `--pretty` option to mosquitto_sub/rr for formatted/unformatted JSON
output.
1.6.7 - 20190925 1.6.7 - 20190925

@ -748,6 +748,11 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
} }
} }
i++; i++;
}else if(!strcmp(argv[i], "--pretty")){
if(pub_or_sub == CLIENT_PUB){
goto unknown_option;
}
cfg->pretty = true;
}else if(!strcmp(argv[i], "-P") || !strcmp(argv[i], "--pw")){ }else if(!strcmp(argv[i], "-P") || !strcmp(argv[i], "--pw")){
if(i==argc-1){ if(i==argc-1){
fprintf(stderr, "Error: -P argument given but no password specified.\n\n"); fprintf(stderr, "Error: -P argument given but no password specified.\n\n");

@ -89,8 +89,8 @@ struct mosq_config {
# endif # endif
#endif #endif
bool clean_session; bool clean_session;
char **topics; /* sub */ char **topics; /* sub, rr */
int topic_count; /* sub */ int topic_count; /* sub, rr */
bool exit_after_sub; /* sub */ bool exit_after_sub; /* sub */
bool no_retain; /* sub */ bool no_retain; /* sub */
bool retained_only; /* sub */ bool retained_only; /* sub */
@ -102,7 +102,8 @@ struct mosq_config {
bool verbose; /* sub */ bool verbose; /* sub */
bool eol; /* sub */ bool eol; /* sub */
int msg_count; /* sub */ int msg_count; /* sub */
char *format; /* sub */ char *format; /* sub, rr */
bool pretty; /* sub, rr */
int timeout; /* sub */ int timeout; /* sub */
int sub_opts; /* sub */ int sub_opts; /* sub */
#ifdef WITH_SOCKS #ifdef WITH_SOCKS

@ -217,6 +217,8 @@ void print_usage(void)
printf(" -W : Specifies a timeout in seconds how long to wait for a response.\n"); printf(" -W : Specifies a timeout in seconds how long to wait for a response.\n");
#endif #endif
printf(" --help : display this message.\n"); printf(" --help : display this message.\n");
printf(" --pretty : print formatted output rather than minimised output when using the\n");
printf(" JSON output format option.\n");
printf(" --quiet : don't print error messages.\n"); printf(" --quiet : don't print error messages.\n");
printf(" --unix : connect to a broker through a unix domain socket instead of a TCP socket,\n"); printf(" --unix : connect to a broker through a unix domain socket instead of a TCP socket,\n");
printf(" e.g. /tmp/mosquitto.sock\n"); printf(" e.g. /tmp/mosquitto.sock\n");

@ -238,6 +238,8 @@ void print_usage(void)
printf(" -W : Specifies a timeout in seconds how long to process incoming MQTT messages.\n"); printf(" -W : Specifies a timeout in seconds how long to process incoming MQTT messages.\n");
#endif #endif
printf(" --help : display this message.\n"); printf(" --help : display this message.\n");
printf(" --pretty : print formatted output rather than minimised output when using the\n");
printf(" JSON output format option.\n");
printf(" --quiet : don't print error messages.\n"); printf(" --quiet : don't print error messages.\n");
printf(" --retained-only : only handle messages with the retained flag set, and exit when the\n"); printf(" --retained-only : only handle messages with the retained flag set, and exit when the\n");
printf(" first non-retained message is received.\n"); printf(" first non-retained message is received.\n");

@ -208,7 +208,7 @@ static int json_print_properties(cJSON *root, const mosquitto_property *properti
#endif #endif
static int json_print(const struct mosquitto_message *message, const mosquitto_property *properties, const struct tm *ti, bool escaped) static int json_print(const struct mosquitto_message *message, const mosquitto_property *properties, const struct tm *ti, bool escaped, bool pretty)
{ {
#ifdef WITH_CJSON #ifdef WITH_CJSON
cJSON *root; cJSON *root;
@ -292,8 +292,11 @@ static int json_print(const struct mosquitto_message *message, const mosquitto_p
cJSON_AddItemToObject(root, "payload", tmp); cJSON_AddItemToObject(root, "payload", tmp);
} }
//json_str = cJSON_PrintUnformatted(root); if(pretty){
json_str = cJSON_Print(root); json_str = cJSON_Print(root);
}else{
json_str = cJSON_PrintUnformatted(root);
}
cJSON_Delete(root); cJSON_Delete(root);
fputs(json_str, stdout); fputs(json_str, stdout);
@ -363,7 +366,7 @@ static void formatted_print(const struct mosq_config *lcfg, const struct mosquit
return; return;
} }
} }
if(json_print(message, properties, ti, true) != MOSQ_ERR_SUCCESS){ if(json_print(message, properties, ti, true, lcfg->pretty) != MOSQ_ERR_SUCCESS){
err_printf(lcfg, "Error: Out of memory.\n"); err_printf(lcfg, "Error: Out of memory.\n");
return; return;
} }
@ -376,7 +379,7 @@ static void formatted_print(const struct mosq_config *lcfg, const struct mosquit
return; return;
} }
} }
rc = json_print(message, properties, ti, false); rc = json_print(message, properties, ti, false, lcfg->pretty);
if(rc == MOSQ_ERR_NOMEM){ if(rc == MOSQ_ERR_NOMEM){
err_printf(lcfg, "Error: Out of memory.\n"); err_printf(lcfg, "Error: Out of memory.\n");
return; return;

@ -46,6 +46,7 @@
<arg><option>-I</option> <replaceable>client-id-prefix</replaceable></arg> <arg><option>-I</option> <replaceable>client-id-prefix</replaceable></arg>
<arg><option>-k</option> <replaceable>keepalive-time</replaceable></arg> <arg><option>-k</option> <replaceable>keepalive-time</replaceable></arg>
<arg><option>-N</option></arg> <arg><option>-N</option></arg>
<arg><option>--pretty</option></arg>
<arg><option>-q</option> <replaceable>message-QoS</replaceable></arg> <arg><option>-q</option> <replaceable>message-QoS</replaceable></arg>
<arg><option>-R</option></arg> <arg><option>-R</option></arg>
<arg><option>-S</option></arg> <arg><option>-S</option></arg>
@ -396,6 +397,16 @@
See also the <option>--username</option> option.</para> See also the <option>--username</option> option.</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>--pretty</option></term>
<listitem>
<para>
When using the JSON output format %j or %J, the default
is to print in an unformatted fashion. Specifying
<option>--pretty</option> prints messages in a prettier,
more human readable format.</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><option>--proxy</option></term> <term><option>--proxy</option></term>
<listitem> <listitem>

@ -41,6 +41,7 @@
<arg><option>-I</option> <replaceable>client-id-prefix</replaceable></arg> <arg><option>-I</option> <replaceable>client-id-prefix</replaceable></arg>
<arg><option>-k</option> <replaceable>keepalive-time</replaceable></arg> <arg><option>-k</option> <replaceable>keepalive-time</replaceable></arg>
<arg><option>-N</option></arg> <arg><option>-N</option></arg>
<arg><option>--pretty</option></arg>
<arg><option>-q</option> <replaceable>message-QoS</replaceable></arg> <arg><option>-q</option> <replaceable>message-QoS</replaceable></arg>
<arg><option>--remove-retained</option></arg> <arg><option>--remove-retained</option></arg>
<group choice='opt'> <group choice='opt'>
@ -405,6 +406,16 @@
See also the <option>--username</option> option.</para> See also the <option>--username</option> option.</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>--pretty</option></term>
<listitem>
<para>
When using the JSON output format %j or %J, the default
is to print in an unformatted fashion. Specifying
<option>--pretty</option> prints messages in a prettier,
more human readable format.</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><option>--proxy</option></term> <term><option>--proxy</option></term>
<listitem> <listitem>

Loading…
Cancel
Save