From 3ea06395430b17f2d707f0b5e7660a5f254b2ff7 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 29 Jan 2021 15:06:53 +0000 Subject: [PATCH] server: Print some useful statistics about open connections Signed-off-by: Michael Tremer --- src/server.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/server.c b/src/server.c index 6725389..c6672f1 100644 --- a/src/server.c +++ b/src/server.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "logging.h" @@ -36,8 +37,35 @@ struct fireperf_server_stats { unsigned int connections; }; +static char __timespec[20]; + +static const char* format_timespec(const struct timespec* t) { + // Convert to local time + struct tm* tm = localtime(&t->tv_sec); + + size_t s = strftime(__timespec, sizeof(__timespec), "%F %T", tm); + + if (s) + return __timespec; + + return NULL; +} + static int dump_stats(struct fireperf_config* conf, const struct fireperf_server_stats* stats) { - DEBUG(conf, "Printing statistics...\n"); + struct timespec now; + + // Fetch the time + int r = clock_gettime(CLOCK_REALTIME, &now); + if (r) { + ERROR(conf, "Could not fetch the time: %s\n", strerror(errno)); + return 1; + } + + // Format timestamp + const char* timestamp = format_timespec(&now); + + INFO(conf, "--- %s --------------------\n", timestamp); + INFO(conf, " %-20s: %20u\n", "Open Connection(s)", stats->connections); return 0; } -- 2.47.2