Better handling of non-exit errors in JSON.

Also converted one more error to the unified call.
This commit is contained in:
Jef Poskanzer 2013-02-07 13:30:58 -08:00
parent bf501899e7
commit eb0206e686
2 changed files with 13 additions and 17 deletions

View File

@ -16,27 +16,18 @@
#include "iperf.h"
#include "iperf_api.h"
static void
iperf_verr(struct iperf_test *test, const char *format, va_list argp)
{
char str[1000];
vsnprintf(str, sizeof(str), format, argp);
if (test != NULL && test->json_output && test->json_top != NULL) {
cJSON_AddStringToObject(test->json_top, "error", str);
iperf_json_finish(test);
} else
fprintf(stderr, "iperf3: %s\n", str);
}
/* Do a printf to stderr. */
void
iperf_err(struct iperf_test *test, const char *format, ...)
{
va_list argp;
char str[1000];
va_start(argp, format);
iperf_verr(test, format, argp);
if (test != NULL && test->json_output && test->json_top != NULL)
cJSON_AddStringToObject(test->json_top, "error", str);
else
fprintf(stderr, "iperf3: %s\n", str);
va_end(argp);
}
@ -45,9 +36,14 @@ void
iperf_errexit(struct iperf_test *test, const char *format, ...)
{
va_list argp;
char str[1000];
va_start(argp, format);
iperf_verr(test, format, argp);
if (test != NULL && test->json_output && test->json_top != NULL) {
cJSON_AddStringToObject(test->json_top, "error", str);
iperf_json_finish(test);
} else
fprintf(stderr, "iperf3: %s\n", str);
va_end(argp);
exit(1);
}

View File

@ -34,6 +34,7 @@
#include <netinet/tcp.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#include "iperf.h"
#include "iperf_api.h"
@ -79,9 +80,8 @@ save_tcpinfo(struct iperf_stream *sp, struct iperf_interval_results *irp)
socklen_t tcp_info_length = sizeof(struct tcp_info);
if (getsockopt(sp->socket, IPPROTO_TCP, TCP_INFO, (void *)&irp->tcpInfo, &tcp_info_length) < 0)
perror("getsockopt");
iperf_err(sp->test, "getsockopt - %s", strerror(errno));
#endif
return;
}
/*************************************************************/