Commit cedd81a0 authored by Leonardo Lai's avatar Leonardo Lai

add option to specify logfile

parent 9a713ebc
Ping host: Ping host:
sudo ./pingpong -c ../../config.ini -f ping sudo ./pingpong -c ../../config.ini -f ping -d 10000 -l pingpong.log
Pong host: Pong host:
sudo ./pingpong -c ../../config.ini -f pong sudo ./pingpong -c ../../config.ini -f pong
...@@ -27,6 +27,10 @@ typedef enum {PING, PONG} app_mode; ...@@ -27,6 +27,10 @@ typedef enum {PING, PONG} app_mode;
static app_mode mode = PING; static app_mode mode = PING;
static volatile int app_alive = 1; static volatile int app_alive = 1;
static int log_enabled = 0;
static char *log_file;
static FILE *log;
static unsigned delay = 1000000;
static const char *progname; static const char *progname;
static void signal_handler(int signum) static void signal_handler(int signum)
...@@ -44,6 +48,14 @@ static void ping_body(void) ...@@ -44,6 +48,14 @@ static void ping_body(void)
printf("PING mode\n"); printf("PING mode\n");
// Open log file
if (log_enabled) {
log = fopen(log_file, "w");
if (log == NULL) {
printf("Error opening log file: %s\n", log_file);
}
}
// Create a socket // Create a socket
int sock; int sock;
if ((sock = udpdk_socket(AF_INET, SOCK_DGRAM, 0)) < 0) { if ((sock = udpdk_socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
...@@ -83,9 +95,12 @@ static void ping_body(void) ...@@ -83,9 +95,12 @@ static void ping_body(void)
ts.tv_sec--; ts.tv_sec--;
} }
printf("Received pong; delta = %d.%09d seconds\n", (int)ts.tv_sec, (int)ts.tv_nsec); printf("Received pong; delta = %d.%09d seconds\n", (int)ts.tv_sec, (int)ts.tv_nsec);
if (log_file) {
fprintf(log, " %d%09d\n", (int)ts.tv_sec, (int)ts.tv_nsec);
}
} }
sleep(1); usleep(delay);
} }
} }
...@@ -126,8 +141,9 @@ static void pong_body(void) ...@@ -126,8 +141,9 @@ static void pong_body(void)
static void usage(void) static void usage(void)
{ {
printf("%s -c CONFIG -f FUNCTION \n" printf("%s -c CONFIG -f FUNCTION \n"
" -c CONFIG: .ini configuration file" " -c CONFIG: .ini configuration file\n"
" -f FUNCTION: 'ping' or 'pong'\n" " -f FUNCTION: 'ping' or 'pong'\n"
" -d DELAY: delay (microseconds) between two ping invocations\n"
, progname); , progname);
} }
...@@ -138,7 +154,7 @@ static int parse_app_args(int argc, char *argv[]) ...@@ -138,7 +154,7 @@ static int parse_app_args(int argc, char *argv[])
progname = argv[0]; progname = argv[0];
while ((c = getopt(argc, argv, "c:f:")) != -1) { while ((c = getopt(argc, argv, "c:f:d:l:")) != -1) {
switch (c) { switch (c) {
case 'c': case 'c':
// this is for the .ini cfg file needed by DPDK, not by the app // this is for the .ini cfg file needed by DPDK, not by the app
...@@ -153,6 +169,17 @@ static int parse_app_args(int argc, char *argv[]) ...@@ -153,6 +169,17 @@ static int parse_app_args(int argc, char *argv[])
return -1; return -1;
} }
break; break;
case 'd':
delay = atoi(optarg);
break;
case 'l':
log_enabled = 1;
log_file = strdup(optarg);
log = fopen(log_file, "w");
if (log == NULL) {
printf("Error opening log file: %s\n", log_file);
}
break;
default: default:
fprintf(stderr, "Unknown option `-%c'.\n", optopt); fprintf(stderr, "Unknown option `-%c'.\n", optopt);
usage(); usage();
...@@ -194,6 +221,9 @@ int main(int argc, char *argv[]) ...@@ -194,6 +221,9 @@ int main(int argc, char *argv[])
} }
pingpong_end: pingpong_end:
if (log_enabled) {
fclose(log);
}
udpdk_interrupt(0); udpdk_interrupt(0);
udpdk_cleanup(); udpdk_cleanup();
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment