nvttest: Use nvt_log for printing output

This commit is contained in:
Kim Woelders
2023-02-17 06:41:23 +01:00
parent f02de1ae99
commit 674ed45ebf
2 changed files with 33 additions and 15 deletions
+2
View File
@@ -5,3 +5,5 @@ nvttest_SOURCES = \
nvttest.c
nvttest_CPPFLAGS = -I$(top_builddir) -I$(top_srcdir) $(CFLAGS_WARN)
nvttest_LDADD = $(top_builddir)/lib/libnvt.la
+30 -14
View File
@@ -13,6 +13,11 @@
#include <sys/ioctl.h>
#include <asm/termbits.h>
#include "lib/nvt_log.h"
#define HELP \
"Usage: nvttest -dqr [-n loops] [-t threads]\n"
static const char *dev = "/dev/ttyNVT0";
@@ -32,13 +37,13 @@ static void *_worker(void *arg)
for (iloop = 0; iloop < topts->nloop; iloop++)
{
printf("Thread %d/%d run %d/%d\n",
nvt_log(LOG_INFO, "Thread %d/%d run %d/%d\n",
topts->ithread, topts->nthread, iloop + 1, topts->nloop);
{
fd = open(dev, O_RDWR);
if (fd < 0)
{
printf("Open '%s' failed: %m\n", dev);
nvt_log(LOG_ERR, "Open '%s' failed: %m\n", dev);
break;
}
@@ -46,13 +51,13 @@ static void *_worker(void *arg)
err = ioctl(fd, TCGETS, &ios);
if (err != 0)
{
printf("ioctl(TCGETS) failed: %m\n");
nvt_log(LOG_ERR, "ioctl(TCGETS) failed: %m\n");
break;
}
err = ioctl(fd, TCSETS, &ios);
if (err != 0)
{
printf("ioctl(TCSETS) failed: %m\n");
nvt_log(LOG_ERR, "ioctl(TCSETS) failed: %m\n");
break;
}
close(fd);
@@ -68,7 +73,7 @@ static void *_worker(void *arg)
fd = open(dev, O_RDWR);
if (fd < 0)
{
printf("Open '%s' failed: %m\n", dev);
nvt_log(LOG_ERR, "Open '%s' failed: %m\n", dev);
break;
}
@@ -78,12 +83,12 @@ static void *_worker(void *arg)
nw = write(fd, buf, len);
if (nw < 0)
{
printf("write failed: %m\n");
nvt_log(LOG_ERR, "write failed: %m\n");
break;
}
if (nw < len)
{
printf("write short: %u/%u\n", nw, len);
nvt_log(LOG_ERR, "write short: %u/%u\n", nw, len);
break;
}
}
@@ -104,36 +109,47 @@ int main(int argc, char **argv)
int ithr, nthr;
topts_t topts = { }, *popts;
pthread_t *ptids;
int debug, quiet;
debug = quiet = 0;
nthr = 1;
topts.nloop = 1;
while ((opt = getopt(argc, argv, "n:rt:")) != -1)
while ((opt = getopt(argc, argv, "dn:qrt:")) != -1)
{
switch (opt)
{
default: /* '?' */
fprintf(stderr, HELP);
exit(EXIT_FAILURE);
case 'd':
debug += 1;
break;
case 'n':
topts.nloop = atoi(optarg);
break;
case 'q':
quiet = 1;
break;
case 'r':
topts.opt_rand_delay = true;
break;
case 't':
nthr = atoi(optarg);
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-n nloop]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
nvt_log_level(quiet, debug);
nvt_log_dest(NVT_LOG_STDOUT);
topts.nthread = nthr;
ptids = malloc(nthr * sizeof(pthread_t));
for (ithr = 0; ithr < nthr; ithr++)
{
printf("Create worker thread %d/%d\n", ithr + 1, nthr);
nvt_log(LOG_INFO, "Create worker thread %d/%d\n", ithr + 1, nthr);
popts = malloc(sizeof(topts_t));
*popts = topts;
@@ -142,7 +158,7 @@ int main(int argc, char **argv)
err = pthread_create(&ptids[ithr], NULL, _worker, popts);
if (err)
{
printf("Failed to create thread %d/%d\n", ithr, nthr);
nvt_log(LOG_ERR, "Failed to create thread %d/%d\n", ithr, nthr);
break;
}
}
@@ -152,7 +168,7 @@ int main(int argc, char **argv)
if (ptids[ithr] == 0)
break;
pthread_join(ptids[ithr], NULL);
printf("Joined worker thread %d/%d\n", ithr + 1, nthr);
nvt_log(LOG_INFO, "Joined worker thread %d/%d\n", ithr + 1, nthr);
}
return 0;