Skip to content

Commit

Permalink
enh: Introduce iperf_set_test_logfile API function (esnet#871)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
srgnk authored and bmah888 committed Jun 5, 2019
1 parent fd46367 commit 255a9c7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/iperf_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ iperf_set_test_blksize(struct iperf_test *ipt, int blksize)
ipt->settings->blksize = blksize;
}

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)
{
Expand Down Expand Up @@ -1217,15 +1223,6 @@ iperf_parse_arguments(struct iperf_test *test, int argc, char **argv)
}
}

/* 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;
Expand Down Expand Up @@ -1350,6 +1347,20 @@ iperf_parse_arguments(struct iperf_test *test, int argc, char **argv)
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)
{
Expand Down
2 changes: 2 additions & 0 deletions src/iperf_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void iperf_set_test_reporter_interval( struct iperf_test* ipt, double reporter_i
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 );
Expand Down Expand Up @@ -267,6 +268,7 @@ int iperf_exchange_results(struct iperf_test *);
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);

Expand Down
4 changes: 4 additions & 0 deletions src/iperf_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ iperf_run_client(struct iperf_test * test)
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;
Expand Down
4 changes: 4 additions & 0 deletions src/iperf_server_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ iperf_run_server(struct iperf_test *test)
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;
Expand Down

0 comments on commit 255a9c7

Please sign in to comment.