diff --git a/ChangeLog.txt b/ChangeLog.txt index 173b9b4c..eeaa2332 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -13,6 +13,8 @@ Broker: Clients: - Fix -L url parsing when `/topic` part is missing. +- Stop some error messages being printed even when `--quiet` was used. + Closes #1284. 1.6.2 - 20190430 diff --git a/client/client_shared.c b/client/client_shared.c index 94510154..0205ce8d 100644 --- a/client/client_shared.c +++ b/client/client_shared.c @@ -18,6 +18,7 @@ Contributors: #include #include +#include #include #include #include @@ -229,7 +230,7 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char * len = strlen(env) + strlen("/mosquitto_pub") + 1; loc = malloc(len); if(!loc){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); return 1; } if(pub_or_sub == CLIENT_PUB){ @@ -246,7 +247,7 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char * len = strlen(env) + strlen("/.config/mosquitto_pub") + 1; loc = malloc(len); if(!loc){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); return 1; } if(pub_or_sub == CLIENT_PUB){ @@ -257,8 +258,6 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char * snprintf(loc, len, "%s/.config/mosquitto_rr", env); } loc[len-1] = '\0'; - }else{ - fprintf(stderr, "Warning: Unable to locate configuration directory, default config not loaded.\n"); } } @@ -268,7 +267,7 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char * len = strlen(env) + strlen("\\mosquitto_pub.conf") + 1; loc = malloc(len); if(!loc){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); return 1; } if(pub_or_sub == CLIENT_PUB){ @@ -279,8 +278,6 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char * snprintf(loc, len, "%s\\mosquitto_rr.conf", env); } loc[len-1] = '\0'; - }else{ - fprintf(stderr, "Warning: Unable to locate configuration directory, default config not loaded.\n"); } #endif @@ -329,7 +326,7 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char * return 1; } if(cfg->password && !cfg->username){ - if(!cfg->quiet) fprintf(stderr, "Warning: Not using password since username not set.\n"); + err_printf(cfg, "Warning: Not using password since username not set.\n"); } #ifdef WITH_TLS if((cfg->certfile && !cfg->keyfile) || (cfg->keyfile && !cfg->certfile)){ @@ -347,23 +344,23 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char * #endif #ifdef FINAL_WITH_TLS_PSK if((cfg->cafile || cfg->capath) && cfg->psk){ - if(!cfg->quiet) fprintf(stderr, "Error: Only one of --psk or --cafile/--capath may be used at once.\n"); + fprintf(stderr, "Error: Only one of --psk or --cafile/--capath may be used at once.\n"); return 1; } if(cfg->psk && !cfg->psk_identity){ - if(!cfg->quiet) fprintf(stderr, "Error: --psk-identity required if --psk used.\n"); + fprintf(stderr, "Error: --psk-identity required if --psk used.\n"); return 1; } #endif if(cfg->clean_session == false && (cfg->id_prefix || !cfg->id)){ - if(!cfg->quiet) fprintf(stderr, "Error: You must provide a client id if you are using the -c option.\n"); + fprintf(stderr, "Error: You must provide a client id if you are using the -c option.\n"); return 1; } if(pub_or_sub == CLIENT_SUB){ if(cfg->topic_count == 0){ - if(!cfg->quiet) fprintf(stderr, "Error: You must specify a topic to subscribe to.\n"); + fprintf(stderr, "Error: You must specify a topic to subscribe to.\n"); return 1; } } @@ -371,39 +368,39 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char * if(!cfg->host){ cfg->host = strdup("localhost"); if(!cfg->host){ - if(!cfg->quiet) fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); return 1; } } rc = mosquitto_property_check_all(CMD_CONNECT, cfg->connect_props); if(rc){ - if(!cfg->quiet) fprintf(stderr, "Error in CONNECT properties: %s\n", mosquitto_strerror(rc)); + err_printf(cfg, "Error in CONNECT properties: %s\n", mosquitto_strerror(rc)); return 1; } rc = mosquitto_property_check_all(CMD_PUBLISH, cfg->publish_props); if(rc){ - if(!cfg->quiet) fprintf(stderr, "Error in PUBLISH properties: %s\n", mosquitto_strerror(rc)); + err_printf(cfg, "Error in PUBLISH properties: %s\n", mosquitto_strerror(rc)); return 1; } rc = mosquitto_property_check_all(CMD_SUBSCRIBE, cfg->subscribe_props); if(rc){ - if(!cfg->quiet) fprintf(stderr, "Error in SUBSCRIBE properties: %s\n", mosquitto_strerror(rc)); + err_printf(cfg, "Error in SUBSCRIBE properties: %s\n", mosquitto_strerror(rc)); return 1; } rc = mosquitto_property_check_all(CMD_UNSUBSCRIBE, cfg->unsubscribe_props); if(rc){ - if(!cfg->quiet) fprintf(stderr, "Error in UNSUBSCRIBE properties: %s\n", mosquitto_strerror(rc)); + err_printf(cfg, "Error in UNSUBSCRIBE properties: %s\n", mosquitto_strerror(rc)); return 1; } rc = mosquitto_property_check_all(CMD_DISCONNECT, cfg->disconnect_props); if(rc){ - if(!cfg->quiet) fprintf(stderr, "Error in DISCONNECT properties: %s\n", mosquitto_strerror(rc)); + err_printf(cfg, "Error in DISCONNECT properties: %s\n", mosquitto_strerror(rc)); return 1; } rc = mosquitto_property_check_all(CMD_WILL, cfg->will_props); if(rc){ - if(!cfg->quiet) fprintf(stderr, "Error in Will properties: %s\n", mosquitto_strerror(rc)); + err_printf(cfg, "Error in Will properties: %s\n", mosquitto_strerror(rc)); return 1; } @@ -436,7 +433,7 @@ int cfg_add_topic(struct mosq_config *cfg, int type, char *topic, const char *ar cfg->topic_count++; cfg->topics = realloc(cfg->topics, cfg->topic_count*sizeof(char *)); if(!cfg->topics){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); return 1; } cfg->topics[cfg->topic_count-1] = strdup(topic); @@ -551,7 +548,7 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c cfg->pub_mode = MSGMODE_FILE; cfg->file_input = strdup(argv[i+1]); if(!cfg->file_input){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); return 1; } } @@ -1081,14 +1078,14 @@ int client_opts_set(struct mosquitto *mosq, struct mosq_config *cfg) cfg->will_payloadlen, cfg->will_payload, cfg->will_qos, cfg->will_retain, cfg->will_props)){ - if(!cfg->quiet) fprintf(stderr, "Error: Problem setting will.\n"); + err_printf(cfg, "Error: Problem setting will.\n"); mosquitto_lib_cleanup(); return 1; } cfg->will_props = NULL; if(cfg->username && mosquitto_username_pw_set(mosq, cfg->username, cfg->password)){ - if(!cfg->quiet) fprintf(stderr, "Error: Problem setting username and password.\n"); + err_printf(cfg, "Error: Problem setting username and password.\n"); mosquitto_lib_cleanup(); return 1; } @@ -1097,48 +1094,48 @@ int client_opts_set(struct mosquitto *mosq, struct mosq_config *cfg) rc = mosquitto_tls_set(mosq, cfg->cafile, cfg->capath, cfg->certfile, cfg->keyfile, NULL); if(rc){ if(rc == MOSQ_ERR_INVAL){ - if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS options: File not found.\n"); + err_printf(cfg, "Error: Problem setting TLS options: File not found.\n"); }else{ - if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS options: %s.\n", mosquitto_strerror(rc)); + err_printf(cfg, "Error: Problem setting TLS options: %s.\n", mosquitto_strerror(rc)); } mosquitto_lib_cleanup(); return 1; } } if(cfg->insecure && mosquitto_tls_insecure_set(mosq, true)){ - if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS insecure option.\n"); + err_printf(cfg, "Error: Problem setting TLS insecure option.\n"); mosquitto_lib_cleanup(); return 1; } if(cfg->tls_engine && mosquitto_string_option(mosq, MOSQ_OPT_TLS_ENGINE, cfg->tls_engine)){ - if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS engine, is %s a valid engine?\n", cfg->tls_engine); + err_printf(cfg, "Error: Problem setting TLS engine, is %s a valid engine?\n", cfg->tls_engine); mosquitto_lib_cleanup(); return 1; } if(cfg->keyform && mosquitto_string_option(mosq, MOSQ_OPT_TLS_KEYFORM, cfg->keyform)){ - if(!cfg->quiet) fprintf(stderr, "Error: Problem setting key form, it must be one of 'pem' or 'engine'.\n"); + err_printf(cfg, "Error: Problem setting key form, it must be one of 'pem' or 'engine'.\n"); mosquitto_lib_cleanup(); return 1; } if(cfg->tls_engine_kpass_sha1 && mosquitto_string_option(mosq, MOSQ_OPT_TLS_ENGINE_KPASS_SHA1, cfg->tls_engine_kpass_sha1)){ - if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS engine key pass sha, is it a 40 character hex string?\n"); + err_printf(cfg, "Error: Problem setting TLS engine key pass sha, is it a 40 character hex string?\n"); mosquitto_lib_cleanup(); return 1; } if(cfg->tls_alpn && mosquitto_string_option(mosq, MOSQ_OPT_TLS_ALPN, cfg->tls_alpn)){ - if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS ALPN protocol.\n"); + err_printf(cfg, "Error: Problem setting TLS ALPN protocol.\n"); mosquitto_lib_cleanup(); return 1; } # ifdef FINAL_WITH_TLS_PSK if(cfg->psk && mosquitto_tls_psk_set(mosq, cfg->psk, cfg->psk_identity, NULL)){ - if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS-PSK options.\n"); + err_printf(cfg, "Error: Problem setting TLS-PSK options.\n"); mosquitto_lib_cleanup(); return 1; } # endif if((cfg->tls_version || cfg->ciphers) && mosquitto_tls_opts_set(mosq, 1, cfg->tls_version, cfg->ciphers)){ - if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS options, check the options are valid.\n"); + err_printf(cfg, "Error: Problem setting TLS options, check the options are valid.\n"); mosquitto_lib_cleanup(); return 1; } @@ -1161,7 +1158,7 @@ int client_id_generate(struct mosq_config *cfg) if(cfg->id_prefix){ cfg->id = malloc(strlen(cfg->id_prefix)+10); if(!cfg->id){ - if(!cfg->quiet) fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); mosquitto_lib_cleanup(); return 1; } @@ -1207,17 +1204,15 @@ int client_connect(struct mosquitto *mosq, struct mosq_config *cfg) rc = mosquitto_connect_bind_v5(mosq, cfg->host, port, cfg->keepalive, cfg->bind_address, cfg->connect_props); #endif if(rc>0){ - if(!cfg->quiet){ - if(rc == MOSQ_ERR_ERRNO){ + if(rc == MOSQ_ERR_ERRNO){ #ifndef WIN32 - err = strerror(errno); + err = strerror(errno); #else - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno, 0, (LPTSTR)&err, 1024, NULL); + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno, 0, (LPTSTR)&err, 1024, NULL); #endif - fprintf(stderr, "Error: %s\n", err); - }else{ - fprintf(stderr, "Unable to connect (%s).\n", mosquitto_strerror(rc)); - } + err_printf(cfg, "Error: %s\n", err); + }else{ + err_printf(cfg, "Unable to connect (%s).\n", mosquitto_strerror(rc)); } mosquitto_lib_cleanup(); return rc; @@ -1284,7 +1279,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) if(!strncmp(url, "socks5h://", strlen("socks5h://"))){ str = url + strlen("socks5h://"); }else{ - fprintf(stderr, "Error: Unsupported proxy protocol: %s\n", url); + err_printf(cfg, "Error: Unsupported proxy protocol: %s\n", url); return 1; } @@ -1311,7 +1306,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) len = i-start; host = malloc(len + 1); if(!host){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); goto cleanup; } memcpy(host, &(str[start]), len); @@ -1324,7 +1319,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) len = i-start; username_or_host = malloc(len + 1); if(!username_or_host){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); goto cleanup; } memcpy(username_or_host, &(str[start]), len); @@ -1344,7 +1339,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) len = i-start; password = malloc(len + 1); if(!password){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); goto cleanup; } memcpy(password, &(str[start]), len); @@ -1360,7 +1355,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) len = i-start; username = malloc(len + 1); if(!username){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); goto cleanup; } memcpy(username, &(str[start]), len); @@ -1378,7 +1373,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) * socks5h://username[:password]@host:port */ port = malloc(len + 1); if(!port){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); goto cleanup; } memcpy(port, &(str[start]), len); @@ -1390,7 +1385,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) username_or_host = NULL; port = malloc(len + 1); if(!port){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); goto cleanup; } memcpy(port, &(str[start]), len); @@ -1398,7 +1393,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) }else{ host = malloc(len + 1); if(!host){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(cfg, "Error: Out of memory.\n"); goto cleanup; } memcpy(host, &(str[start]), len); @@ -1407,7 +1402,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) } if(!host){ - fprintf(stderr, "Error: Invalid proxy.\n"); + err_printf(cfg, "Error: Invalid proxy.\n"); goto cleanup; } @@ -1420,7 +1415,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) if(port){ port_int = atoi(port); if(port_int < 1 || port_int > 65535){ - fprintf(stderr, "Error: Invalid proxy port %d\n", port_int); + err_printf(cfg, "Error: Invalid proxy port %d\n", port_int); goto cleanup; } free(port); @@ -1443,4 +1438,15 @@ cleanup: return 1; } +void err_printf(const struct mosq_config *cfg, const char *fmt, ...) +{ + va_list va; + + if(cfg->quiet) return; + + va_start(va, fmt); + vfprintf(stderr, fmt, va); + va_end(va); +} + #endif diff --git a/client/client_shared.h b/client/client_shared.h index 7e65f3d1..232fa909 100644 --- a/client/client_shared.h +++ b/client/client_shared.h @@ -126,4 +126,6 @@ int client_connect(struct mosquitto *mosq, struct mosq_config *cfg); int cfg_parse_property(struct mosq_config *cfg, int argc, char *argv[], int *idx); +void err_printf(const struct mosq_config *cfg, const char *fmt, ...); + #endif diff --git a/client/pub_client.c b/client/pub_client.c index cdf89741..60d652a3 100644 --- a/client/pub_client.c +++ b/client/pub_client.c @@ -142,36 +142,34 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flag break; } if(rc){ - if(!cfg.quiet){ - switch(rc){ - case MOSQ_ERR_INVAL: - fprintf(stderr, "Error: Invalid input. Does your topic contain '+' or '#'?\n"); - break; - case MOSQ_ERR_NOMEM: - fprintf(stderr, "Error: Out of memory when trying to publish message.\n"); - break; - case MOSQ_ERR_NO_CONN: - fprintf(stderr, "Error: Client not connected when trying to publish.\n"); - break; - case MOSQ_ERR_PROTOCOL: - fprintf(stderr, "Error: Protocol error when communicating with broker.\n"); - break; - case MOSQ_ERR_PAYLOAD_SIZE: - fprintf(stderr, "Error: Message payload is too large.\n"); - break; - case MOSQ_ERR_QOS_NOT_SUPPORTED: - fprintf(stderr, "Error: Message QoS not supported on broker, try a lower QoS.\n"); - break; - } + switch(rc){ + case MOSQ_ERR_INVAL: + err_printf(&cfg, "Error: Invalid input. Does your topic contain '+' or '#'?\n"); + break; + case MOSQ_ERR_NOMEM: + err_printf(&cfg, "Error: Out of memory when trying to publish message.\n"); + break; + case MOSQ_ERR_NO_CONN: + err_printf(&cfg, "Error: Client not connected when trying to publish.\n"); + break; + case MOSQ_ERR_PROTOCOL: + err_printf(&cfg, "Error: Protocol error when communicating with broker.\n"); + break; + case MOSQ_ERR_PAYLOAD_SIZE: + err_printf(&cfg, "Error: Message payload is too large.\n"); + break; + case MOSQ_ERR_QOS_NOT_SUPPORTED: + err_printf(&cfg, "Error: Message QoS not supported on broker, try a lower QoS.\n"); + break; } mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props); } }else{ - if(result && !cfg.quiet){ + if(result){ if(cfg.protocol_version == MQTT_PROTOCOL_V5){ - fprintf(stderr, "%s\n", mosquitto_reason_string(result)); + err_printf(&cfg, "%s\n", mosquitto_reason_string(result)); }else{ - fprintf(stderr, "%s\n", mosquitto_connack_string(result)); + err_printf(&cfg, "%s\n", mosquitto_connack_string(result)); } } } @@ -185,7 +183,7 @@ void my_publish_callback(struct mosquitto *mosq, void *obj, int mid, int reason_ last_mid_sent = mid; if(reason_code > 127){ - if(!cfg.quiet) fprintf(stderr, "Warning: Publish %d failed: %s.\n", mid, mosquitto_reason_string(reason_code)); + err_printf(&cfg, "Warning: Publish %d failed: %s.\n", mid, mosquitto_reason_string(reason_code)); } publish_count++; @@ -208,7 +206,7 @@ int pub_shared_init(void) { line_buf = malloc(line_buf_len); if(!line_buf){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(&cfg, "Error: Out of memory.\n"); return 1; } return 0; @@ -246,7 +244,7 @@ int pub_shared_loop(struct mosquitto *mosq) line_buf[buf_len_actual-1] = '\0'; rc2 = my_publish(mosq, &mid_sent, cfg.topic, buf_len_actual-1, line_buf, cfg.qos, cfg.retain); if(rc2){ - if(!cfg.quiet) fprintf(stderr, "Error: Publish returned %d, disconnecting.\n", rc2); + err_printf(&cfg, "Error: Publish returned %d, disconnecting.\n", rc2); mosquitto_disconnect_v5(mosq, MQTT_RC_DISCONNECT_WITH_WILL_MSG, cfg.disconnect_props); } break; @@ -256,7 +254,7 @@ int pub_shared_loop(struct mosquitto *mosq) read_len = 1024; buf2 = realloc(line_buf, line_buf_len); if(!buf2){ - fprintf(stderr, "Error: Out of memory.\n"); + err_printf(&cfg, "Error: Out of memory.\n"); return MOSQ_ERR_NOMEM; } line_buf = buf2; @@ -305,7 +303,7 @@ int pub_shared_loop(struct mosquitto *mosq) break; } if(rc){ - fprintf(stderr, "Error sending repeat publish: %s", mosquitto_strerror(rc)); + err_printf(&cfg, "Error sending repeat publish: %s", mosquitto_strerror(rc)); } } } @@ -457,12 +455,12 @@ int main(int argc, char *argv[]) if(cfg.pub_mode == MSGMODE_STDIN_FILE){ if(load_stdin()){ - fprintf(stderr, "Error loading input from stdin.\n"); + err_printf(&cfg, "Error loading input from stdin.\n"); goto cleanup; } }else if(cfg.file_input){ if(load_file(cfg.file_input)){ - fprintf(stderr, "Error loading input file \"%s\".\n", cfg.file_input); + err_printf(&cfg, "Error loading input file \"%s\".\n", cfg.file_input); goto cleanup; } } @@ -482,10 +480,10 @@ int main(int argc, char *argv[]) if(!mosq){ switch(errno){ case ENOMEM: - if(!cfg.quiet) fprintf(stderr, "Error: Out of memory.\n"); + err_printf(&cfg, "Error: Out of memory.\n"); break; case EINVAL: - if(!cfg.quiet) fprintf(stderr, "Error: Invalid id.\n"); + err_printf(&cfg, "Error: Invalid id.\n"); break; } goto cleanup; @@ -517,7 +515,7 @@ int main(int argc, char *argv[]) pub_shared_cleanup(); if(rc){ - fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc)); + err_printf(&cfg, "Error: %s\n", mosquitto_strerror(rc)); } return rc; diff --git a/client/pub_shared.c b/client/pub_shared.c index ead86a34..e1de3f0a 100644 --- a/client/pub_shared.c +++ b/client/pub_shared.c @@ -61,7 +61,7 @@ int load_stdin(void) rlen = fread(buf, 1, 1024, stdin); aux_message = realloc(cfg.message, pos+rlen); if(!aux_message){ - if(!cfg.quiet) fprintf(stderr, "Error: Out of memory.\n"); + err_printf(&cfg, "Error: Out of memory.\n"); free(cfg.message); return 1; } else @@ -74,7 +74,7 @@ int load_stdin(void) cfg.msglen = pos; if(!cfg.msglen){ - if(!cfg.quiet) fprintf(stderr, "Error: Zero length input.\n"); + err_printf(&cfg, "Error: Zero length input.\n"); return 1; } @@ -88,7 +88,7 @@ int load_file(const char *filename) fptr = fopen(filename, "rb"); if(!fptr){ - if(!cfg.quiet) fprintf(stderr, "Error: Unable to open file \"%s\".\n", filename); + err_printf(&cfg, "Error: Unable to open file \"%s\".\n", filename); return 1; } cfg.pub_mode = MSGMODE_FILE; @@ -96,22 +96,22 @@ int load_file(const char *filename) cfg.msglen = ftell(fptr); if(cfg.msglen > 268435455){ fclose(fptr); - if(!cfg.quiet) fprintf(stderr, "Error: File \"%s\" is too large (>268,435,455 bytes).\n", filename); + err_printf(&cfg, "Error: File \"%s\" is too large (>268,435,455 bytes).\n", filename); return 1; }else if(cfg.msglen == 0){ fclose(fptr); - if(!cfg.quiet) fprintf(stderr, "Error: File \"%s\" is empty.\n", filename); + err_printf(&cfg, "Error: File \"%s\" is empty.\n", filename); return 1; }else if(cfg.msglen < 0){ fclose(fptr); - if(!cfg.quiet) fprintf(stderr, "Error: Unable to determine size of file \"%s\".\n", filename); + err_printf(&cfg, "Error: Unable to determine size of file \"%s\".\n", filename); return 1; } fseek(fptr, 0, SEEK_SET); cfg.message = malloc(cfg.msglen); if(!cfg.message){ fclose(fptr); - if(!cfg.quiet) fprintf(stderr, "Error: Out of memory.\n"); + err_printf(&cfg, "Error: Out of memory.\n"); return 1; } pos = 0; diff --git a/client/rr_client.c b/client/rr_client.c index 5ecd3093..cbd32d5e 100644 --- a/client/rr_client.c +++ b/client/rr_client.c @@ -121,8 +121,8 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flag mosquitto_subscribe_v5(mosq, NULL, cfg.response_topic, cfg.qos, 0, cfg.subscribe_props); }else{ client_state = rr_s_disconnect; - if(result && !cfg.quiet){ - fprintf(stderr, "%s\n", mosquitto_connack_string(result)); + if(result){ + err_printf(&cfg, "%s\n", mosquitto_connack_string(result)); } mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props); } @@ -135,10 +135,8 @@ void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_c client_state = rr_s_ready_to_publish; }else{ client_state = rr_s_disconnect; - if(!cfg.quiet){ - fprintf(stderr, "%s\n", mosquitto_reason_string(granted_qos[0])); - mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props); - } + err_printf(&cfg, "%s\n", mosquitto_reason_string(granted_qos[0])); + mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props); } } @@ -282,7 +280,7 @@ int main(int argc, char *argv[]) } rc = mosquitto_property_check_all(CMD_PUBLISH, cfg.publish_props); if(rc){ - if(!cfg.quiet) fprintf(stderr, "Error in PUBLISH properties: Duplicate response topic.\n"); + err_printf(&cfg, "Error in PUBLISH properties: Duplicate response topic.\n"); goto cleanup; } @@ -294,10 +292,10 @@ int main(int argc, char *argv[]) if(!mosq){ switch(errno){ case ENOMEM: - if(!cfg.quiet) fprintf(stderr, "Error: Out of memory.\n"); + err_printf(&cfg, "Error: Out of memory.\n"); break; case EINVAL: - if(!cfg.quiet) fprintf(stderr, "Error: Invalid id and/or clean_session.\n"); + err_printf(&cfg, "Error: Invalid id and/or clean_session.\n"); break; } goto cleanup; diff --git a/client/sub_client.c b/client/sub_client.c index c970537c..a10757c6 100644 --- a/client/sub_client.c +++ b/client/sub_client.c @@ -35,7 +35,7 @@ Contributors: #include #include "client_shared.h" -static struct mosq_config cfg; +struct mosq_config cfg; bool process_messages = true; int msg_count = 0; struct mosquitto *mosq = NULL; @@ -124,11 +124,11 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flag mosquitto_unsubscribe_v5(mosq, NULL, cfg.unsub_topics[i], cfg.unsubscribe_props); } }else{ - if(result && !cfg.quiet){ + if(result){ if(cfg.protocol_version == MQTT_PROTOCOL_V5){ - fprintf(stderr, "%s\n", mosquitto_reason_string(result)); + err_printf(&cfg, "%s\n", mosquitto_reason_string(result)); }else{ - fprintf(stderr, "%s\n", mosquitto_connack_string(result)); + err_printf(&cfg, "%s\n", mosquitto_connack_string(result)); } } mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props); @@ -307,10 +307,10 @@ int main(int argc, char *argv[]) if(!mosq){ switch(errno){ case ENOMEM: - if(!cfg.quiet) fprintf(stderr, "Error: Out of memory.\n"); + err_printf(&cfg, "Error: Out of memory.\n"); break; case EINVAL: - if(!cfg.quiet) fprintf(stderr, "Error: Invalid id and/or clean_session.\n"); + err_printf(&cfg, "Error: Invalid id and/or clean_session.\n"); break; } goto cleanup; diff --git a/client/sub_client_output.c b/client/sub_client_output.c index c4ff376a..a7cdefb2 100644 --- a/client/sub_client_output.c +++ b/client/sub_client_output.c @@ -37,6 +37,7 @@ Contributors: #include #include "client_shared.h" +extern struct mosq_config cfg; static int get_time(struct tm **ti, long *ns) { @@ -60,7 +61,7 @@ static int get_time(struct tm **ti, long *ns) *ns = tv.tv_usec*1000; #else if(clock_gettime(CLOCK_REALTIME, &ts) != 0){ - fprintf(stderr, "Error obtaining system time.\n"); + err_printf(&cfg, "Error obtaining system time.\n"); return 1; } s = ts.tv_sec; @@ -69,7 +70,7 @@ static int get_time(struct tm **ti, long *ns) *ti = localtime(&s); if(!(*ti)){ - fprintf(stderr, "Error obtaining system time.\n"); + err_printf(&cfg, "Error obtaining system time.\n"); return 1; } @@ -130,7 +131,7 @@ static void json_print(const struct mosquitto_message *message, const struct tm } -static void formatted_print(const struct mosq_config *cfg, const struct mosquitto_message *message) +static void formatted_print(const struct mosq_config *lcfg, const struct mosquitto_message *message) { int len; int i; @@ -139,13 +140,13 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt char strf[3]; char buf[100]; - len = strlen(cfg->format); + len = strlen(lcfg->format); for(i=0; iformat[i] == '%'){ + if(lcfg->format[i] == '%'){ if(i < len-1){ i++; - switch(cfg->format[i]){ + switch(lcfg->format[i]){ case '%': fputc('%', stdout); break; @@ -153,7 +154,7 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt case 'I': if(!ti){ if(get_time(&ti, &ns)){ - fprintf(stderr, "Error obtaining system time.\n"); + err_printf(lcfg, "Error obtaining system time.\n"); return; } } @@ -165,7 +166,7 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt case 'j': if(!ti){ if(get_time(&ti, &ns)){ - fprintf(stderr, "Error obtaining system time.\n"); + err_printf(lcfg, "Error obtaining system time.\n"); return; } } @@ -175,7 +176,7 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt case 'J': if(!ti){ if(get_time(&ti, &ns)){ - fprintf(stderr, "Error obtaining system time.\n"); + err_printf(lcfg, "Error obtaining system time.\n"); return; } } @@ -213,7 +214,7 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt case 'U': if(!ti){ if(get_time(&ti, &ns)){ - fprintf(stderr, "Error obtaining system time.\n"); + err_printf(lcfg, "Error obtaining system time.\n"); return; } } @@ -231,24 +232,24 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt break; } } - }else if(cfg->format[i] == '@'){ + }else if(lcfg->format[i] == '@'){ if(i < len-1){ i++; - if(cfg->format[i] == '@'){ + if(lcfg->format[i] == '@'){ fputc('@', stdout); }else{ if(!ti){ if(get_time(&ti, &ns)){ - fprintf(stderr, "Error obtaining system time.\n"); + err_printf(lcfg, "Error obtaining system time.\n"); return; } } strf[0] = '%'; - strf[1] = cfg->format[i]; + strf[1] = lcfg->format[i]; strf[2] = 0; - if(cfg->format[i] == 'N'){ + if(lcfg->format[i] == 'N'){ printf("%09ld", ns); }else{ if(strftime(buf, 100, strf, ti) != 0){ @@ -257,10 +258,10 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt } } } - }else if(cfg->format[i] == '\\'){ + }else if(lcfg->format[i] == '\\'){ if(i < len-1){ i++; - switch(cfg->format[i]){ + switch(lcfg->format[i]){ case '\\': fputc('\\', stdout); break; @@ -295,10 +296,10 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt } } }else{ - fputc(cfg->format[i], stdout); + fputc(lcfg->format[i], stdout); } } - if(cfg->eol){ + if(lcfg->eol){ fputc('\n', stdout); } fflush(stdout);