enh: Introduce iperf_set_test_logfile API function (#871)
To be able to set the test->outfile to a different file other than default
if using libiperf API.
Since logfile is now opened in iperf_parse_arguments() and this function
may not be used if running iperf using API, define a dedicated function
iperf_open_logfile() and move the opening of logfile into
iperf_run_client() and iperf_run_server() to make sure logfile will be
opened if iperf_parse_arguments() was not called.
diff --git a/src/iperf_api.c b/src/iperf_api.c
index 014a560..4e8ed1f 100755
--- a/src/iperf_api.c
+++ b/src/iperf_api.c
@@ -391,6 +391,12 @@
}
void
+iperf_set_test_logfile(struct iperf_test *ipt, char *logfile)
+{
+ ipt->logfile = strdup(logfile);
+}
+
+void
iperf_set_test_rate(struct iperf_test *ipt, uint64_t rate)
{
ipt->settings->rate = rate;
@@ -1217,15 +1223,6 @@
}
}
- /* Set logging to a file if specified, otherwise use the default (stdout) */
- if (test->logfile) {
- test->outfile = fopen(test->logfile, "a+");
- if (test->outfile == NULL) {
- i_errno = IELOGFILE;
- return -1;
- }
- }
-
/* Check flag / role compatibility. */
if (test->role == 'c' && server_flag) {
i_errno = IESERVERONLY;
@@ -1350,6 +1347,20 @@
return 0;
}
+/*
+ * Open the file specified by test->logfile and set test->outfile to its' FD.
+ */
+int iperf_open_logfile(struct iperf_test *test)
+{
+ test->outfile = fopen(test->logfile, "a+");
+ if (test->outfile == NULL) {
+ i_errno = IELOGFILE;
+ return -1;
+ }
+
+ return 0;
+}
+
int
iperf_set_send_state(struct iperf_test *test, signed char state)
{
diff --git a/src/iperf_api.h b/src/iperf_api.h
index 7daf974..7483faa 100755
--- a/src/iperf_api.h
+++ b/src/iperf_api.h
@@ -138,6 +138,7 @@
void iperf_set_test_stats_interval( struct iperf_test* ipt, double stats_interval );
void iperf_set_test_state( struct iperf_test* ipt, signed char state );
void iperf_set_test_blksize( struct iperf_test* ipt, int blksize );
+void iperf_set_test_logfile( struct iperf_test* ipt, char *logfile );
void iperf_set_test_rate( struct iperf_test* ipt, uint64_t rate );
void iperf_set_test_pacing_timer( struct iperf_test* ipt, int pacing_timer );
void iperf_set_test_bytes( struct iperf_test* ipt, uint64_t bytes );
@@ -267,6 +268,7 @@
int iperf_init_test(struct iperf_test *);
int iperf_create_send_timers(struct iperf_test *);
int iperf_parse_arguments(struct iperf_test *, int, char **);
+int iperf_open_logfile(struct iperf_test *);
void iperf_reset_test(struct iperf_test *);
void iperf_reset_stats(struct iperf_test * test);
diff --git a/src/iperf_client_api.c b/src/iperf_client_api.c
index 46c283b..20ea6fd 100644
--- a/src/iperf_client_api.c
+++ b/src/iperf_client_api.c
@@ -454,6 +454,10 @@
struct timeval* timeout = NULL;
struct iperf_stream *sp;
+ if (test->logfile)
+ if (iperf_open_logfile(test) < 0)
+ return -1;
+
if (test->affinity != -1)
if (iperf_setaffinity(test, test->affinity) != 0)
return -1;
diff --git a/src/iperf_server_api.c b/src/iperf_server_api.c
index 079da89..56cab4b 100644
--- a/src/iperf_server_api.c
+++ b/src/iperf_server_api.c
@@ -398,6 +398,10 @@
struct timeval* timeout;
int flag;
+ if (test->logfile)
+ if (iperf_open_logfile(test) < 0)
+ return -1;
+
if (test->affinity != -1)
if (iperf_setaffinity(test, test->affinity) != 0)
return -2;