Silence stdout but still get JSON output via API
Context
- Version of iperf3:
iperf 3.9 (cJSON 1.7.13)
Linux localhost 5.13.19-200.fc34.x86_64 #1 SMP Sat Sep 18 16:32:24 UTC 2021 x86_64
Optional features available: CPU affinity setting, IPv6 flow label, SCTP, TCP congestion algorithm setting, sendfile / zerocopy, socket pacing, authentication
-
Hardware: x86_64
-
Operating system (and distribution, if any): GNU/Linux, Fedora 34
-
Other relevant information (for example, non-default compilers, libraries, cross-compiling, etc.):
Bug Report
-
Expected Behavior I want to silcence output via stdout, but still get JSON output via
iperf_get_test_json_output_string()API call. Silencing was done viaiperf_set_test_logfile("/dev/null"). -
Actual Behavior If output is being silenced, the
iperf_get_test_json_output_string()API call returns NULL -
Steps to Reproduce
//C++
test_ = iperf_new_test();
iperf_set_test_json_output(test_, 1);
iperf_set_test_logfile("/dev/null");
[...]
int rc = iperf_run_client(test_);
[...]
auto res = iperf_get_test_json_output_string(test_);
if (res == nullptr)
{
throw std::runtime_error("Iperf3: empty result");
}
will throw the exception.
- Possible Workaround
test_ = iperf_new_test();
iperf_set_test_json_output(test_, 1);
output_file_ = std::tmpnam(nullptr);
iperf_set_test_logfile(output_file_);
[...]
int rc = iperf_run_client(test_);
[...]
[open and read JSON from file output_file_]
I'm trying to reproduce this problem but without much success. My test case is to start with examples/mic.c and add the following two lines before the call to iperf_run_client(), which should roughly simulate the setup in your C++ code:
iperf_set_test_json_output( test, 1 );
iperf_set_test_logfile( test, "/dev/null" );
Then I added these lines after the call to iperf_run_client(), to do a check similar to yours:
char *res = iperf_get_test_json_output_string(test);
if ( res == NULL ) {
fprintf( stderr, "empty JSON result" );
}
else {
printf("got %zd bytes of JSON\n",
strlen(iperf_get_test_json_output_string(test)));
}
This seems to be working correctly in my testing so far, in other words I always get the "got xxx bytes of JSON" output.
I can reproduce this issue on Ubuntu 22.04, where installed version of iperf3 is iperf 3.9 (cJSON 1.7.13),
and the issue is gone after iperf3 is upgraded to version 3.12.
I believe this bug is fixed by 98d87bd7e82b98775d9e4c62235132caa54233ab . It will be okay to close this issue.