diff --git a/ChangeLog.txt b/ChangeLog.txt index fb4c258a..455a3a76 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -24,6 +24,8 @@ Clients: - Use cJSON library for producing JSON output, where available. Closes #1222. - Add support for outputting MQTT v5 property information to mosquitto_sub/rr JSON output. +- Add `--pretty` option to mosquitto_sub/rr for formatted/unformatted JSON + output. 1.6.7 - 20190925 diff --git a/client/client_shared.c b/client/client_shared.c index b7d7dedb..79829929 100644 --- a/client/client_shared.c +++ b/client/client_shared.c @@ -748,6 +748,11 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c } } 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")){ if(i==argc-1){ fprintf(stderr, "Error: -P argument given but no password specified.\n\n"); diff --git a/client/client_shared.h b/client/client_shared.h index 4c7edd93..4ef56d28 100644 --- a/client/client_shared.h +++ b/client/client_shared.h @@ -89,8 +89,8 @@ struct mosq_config { # endif #endif bool clean_session; - char **topics; /* sub */ - int topic_count; /* sub */ + char **topics; /* sub, rr */ + int topic_count; /* sub, rr */ bool exit_after_sub; /* sub */ bool no_retain; /* sub */ bool retained_only; /* sub */ @@ -102,7 +102,8 @@ struct mosq_config { bool verbose; /* sub */ bool eol; /* sub */ int msg_count; /* sub */ - char *format; /* sub */ + char *format; /* sub, rr */ + bool pretty; /* sub, rr */ int timeout; /* sub */ int sub_opts; /* sub */ #ifdef WITH_SOCKS diff --git a/client/rr_client.c b/client/rr_client.c index cd8282d5..319e33d7 100644 --- a/client/rr_client.c +++ b/client/rr_client.c @@ -217,6 +217,8 @@ void print_usage(void) printf(" -W : Specifies a timeout in seconds how long to wait for a response.\n"); #endif 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(" --unix : connect to a broker through a unix domain socket instead of a TCP socket,\n"); printf(" e.g. /tmp/mosquitto.sock\n"); diff --git a/client/sub_client.c b/client/sub_client.c index 4e8f0f44..70485fc9 100644 --- a/client/sub_client.c +++ b/client/sub_client.c @@ -238,6 +238,8 @@ void print_usage(void) printf(" -W : Specifies a timeout in seconds how long to process incoming MQTT messages.\n"); #endif 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(" --retained-only : only handle messages with the retained flag set, and exit when the\n"); printf(" first non-retained message is received.\n"); diff --git a/client/sub_client_output.c b/client/sub_client_output.c index 41f1cd65..80840740 100644 --- a/client/sub_client_output.c +++ b/client/sub_client_output.c @@ -208,7 +208,7 @@ 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) +static int json_print(const struct mosquitto_message *message, const mosquitto_property *properties, const struct tm *ti, bool escaped, bool pretty) { #ifdef WITH_CJSON cJSON *root; @@ -292,8 +292,11 @@ static int json_print(const struct mosquitto_message *message, const mosquitto_p cJSON_AddItemToObject(root, "payload", tmp); } - //json_str = cJSON_PrintUnformatted(root); - json_str = cJSON_Print(root); + if(pretty){ + json_str = cJSON_Print(root); + }else{ + json_str = cJSON_PrintUnformatted(root); + } cJSON_Delete(root); fputs(json_str, stdout); @@ -363,7 +366,7 @@ static void formatted_print(const struct mosq_config *lcfg, const struct mosquit 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"); return; } @@ -376,7 +379,7 @@ static void formatted_print(const struct mosq_config *lcfg, const struct mosquit return; } } - rc = json_print(message, properties, ti, false); + rc = json_print(message, properties, ti, false, lcfg->pretty); if(rc == MOSQ_ERR_NOMEM){ err_printf(lcfg, "Error: Out of memory.\n"); return; diff --git a/man/mosquitto_rr.1.xml b/man/mosquitto_rr.1.xml index e073fa4b..cbb57fe7 100644 --- a/man/mosquitto_rr.1.xml +++ b/man/mosquitto_rr.1.xml @@ -46,6 +46,7 @@ client-id-prefix keepalive-time + message-QoS @@ -396,6 +397,16 @@ See also the option. + + + + + When using the JSON output format %j or %J, the default + is to print in an unformatted fashion. Specifying + prints messages in a prettier, + more human readable format. + + diff --git a/man/mosquitto_sub.1.xml b/man/mosquitto_sub.1.xml index 3ee457c9..12af1335 100644 --- a/man/mosquitto_sub.1.xml +++ b/man/mosquitto_sub.1.xml @@ -41,6 +41,7 @@ client-id-prefix keepalive-time + message-QoS @@ -405,6 +406,16 @@ See also the option. + + + + + When using the JSON output format %j or %J, the default + is to print in an unformatted fashion. Specifying + prints messages in a prettier, + more human readable format. + +