From b85c64b441e560f27f87fa26381600c2cbfe17b8 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 23 Sep 2020 14:32:52 -0400 Subject: Generalize logging --- src/log.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/log.c (limited to 'src/log.c') diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..d4d4e2a --- /dev/null +++ b/src/log.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include "log.h" + +static void +server_logf(FILE *f, const char *fmt, va_list ap) +{ + fprintf(f, "\t"); + vfprintf(f, fmt, ap); +} + +static void +client_logf(FILE *f, struct sockaddr *addr, const char *fmt, va_list ap) +{ + char abuf[INET6_ADDRSTRLEN]; + const char *addrs = inet_ntop(addr->sa_family, + addr->sa_data, abuf, sizeof(abuf)); + assert(addrs); + + fprintf(f, "%s\t", addrs); + vfprintf(f, fmt, ap); +} + +void +server_log(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + server_logf(stdout, fmt, ap); + va_end(ap); +} + +void +client_log(struct sockaddr *addr, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + client_logf(stdout, addr, fmt, ap); + va_end(ap); +} + +void +server_error(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + server_logf(stderr, fmt, ap); + va_end(ap); +} + +void +client_error(struct sockaddr *addr, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + client_logf(stderr, addr, fmt, ap); + va_end(ap); +} -- cgit v1.2.3