From cf5dcbcdfe5f2a8a813396a7eca55e1a590afeb6 Mon Sep 17 00:00:00 2001 From: Benjamin Poirier Date: Wed, 28 Jul 2021 15:46:52 +0900 Subject: test-cache-mngr: Add option to print timestamps The format is the same as `ip -ts monitor` so the two can be compared. --- tests/test-cache-mngr.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test-cache-mngr.c b/tests/test-cache-mngr.c index d8537a1..c47c50e 100644 --- a/tests/test-cache-mngr.c +++ b/tests/test-cache-mngr.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include @@ -10,15 +12,32 @@ static int quit = 0; static int change = 1; +static int print_ts = 0; static struct nl_dump_params params = { .dp_type = NL_DUMP_LINE, }; +static void print_timestamp(FILE *fp) +{ + struct timeval tv; + char tshort[40]; + struct tm *tm; + + gettimeofday(&tv, NULL); + tm = localtime(&tv.tv_sec); + + strftime(tshort, sizeof(tshort), "%Y-%m-%dT%H:%M:%S", tm); + fprintf(fp, "[%s.%06ld] ", tshort, tv.tv_usec); +} + static void change_cb(struct nl_cache *cache, struct nl_object *obj, int action, void *data) { + if (print_ts) + print_timestamp(stdout); + if (action == NL_ACT_NEW) printf("NEW "); else if (action == NL_ACT_DEL) @@ -48,6 +67,7 @@ static void print_usage(FILE* stream, const char *name) " -i, --interval=TIME Dump cache content after TIME seconds when there is no\n" " change; 0 to disable. Default: 1\n" " -I, --iter Iterate over all address families when updating caches.\n" + " -t, --tshort Print a short timestamp before change messages.\n" " -h, --help Show this help text.\n" , name); } @@ -64,12 +84,13 @@ int main(int argc, char *argv[]) { "dump", no_argument, 0, 'd' }, { "interval", required_argument, 0, 'i' }, { "iter", no_argument, 0, 'I' }, + { "tshort", no_argument, 0, 't' }, { "help", 0, 0, 'h' }, { 0, 0, 0, 0 } }; int c; - c = getopt_long(argc, argv, "hf:di:I", long_opts, NULL); + c = getopt_long(argc, argv, "hf:di:It", long_opts, NULL); if (c == -1) break; @@ -105,6 +126,10 @@ int main(int argc, char *argv[]) iter = true; break; + case 't': + print_ts = true; + break; + case 'h': print_usage(stdout, argv[0]); exit(0); -- cgit v0.12