From 75073d0a8478441cc97a6efa10b566c5fb1dac81 Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Fri, 17 Apr 2020 20:57:17 +0100 Subject: New upstream version 1.4.0 --- src/log.c | 73 ++++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 22 deletions(-) (limited to 'src/log.c') diff --git a/src/log.c b/src/log.c index 982bdb7..d6f0934 100644 --- a/src/log.c +++ b/src/log.c @@ -7,57 +7,86 @@ #include #include #include +#include + #include "fido.h" #ifndef FIDO_NO_DIAGNOSTIC +#define XXDLEN 32 +#define XXDROW 128 +#define LINELEN 256 + #ifndef TLS #define TLS #endif static TLS int logging; +static TLS fido_log_handler_t *log_handler; + +static void +log_on_stderr(const char *str) +{ + fprintf(stderr, "%s", str); +} void fido_log_init(void) { logging = 1; + log_handler = log_on_stderr; } void -fido_log_xxd(const void *buf, size_t count) +fido_log_debug(const char *fmt, ...) { - const uint8_t *ptr = buf; - size_t i; + char line[LINELEN]; + va_list ap; + int r; - if (!logging) + if (!logging || log_handler == NULL) return; - fprintf(stderr, " "); - - for (i = 0; i < count; i++) { - fprintf(stderr, "%02x ", *ptr++); - if ((i + 1) % 16 == 0 && i + 1 < count) - fprintf(stderr, "\n "); - } - - fprintf(stderr, "\n"); - fflush(stderr); + va_start(ap, fmt); + r = vsnprintf(line, sizeof(line) - 1, fmt, ap); + va_end(ap); + if (r < 0 || (size_t)r >= sizeof(line) - 1) + return; + strlcat(line, "\n", sizeof(line)); + log_handler(line); } void -fido_log_debug(const char *fmt, ...) +fido_log_xxd(const void *buf, size_t count) { - va_list ap; + const uint8_t *ptr = buf; + char row[XXDROW]; + char xxd[XXDLEN]; - if (!logging) + if (!logging || log_handler == NULL || count == 0) return; - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); + *row = '\0'; - fprintf(stderr, "\n"); - fflush(stderr); + for (size_t i = 0; i < count; i++) { + *xxd = '\0'; + if (i % 16 == 0) + snprintf(xxd, sizeof(xxd), "%04zu: %02x", i, *ptr++); + else + snprintf(xxd, sizeof(xxd), " %02x", *ptr++); + strlcat(row, xxd, sizeof(row)); + if (i % 16 == 15 || i == count - 1) { + fido_log_debug("%s", row); + *row = '\0'; + } + } +} + +void +fido_set_log_handler(fido_log_handler_t *handler) +{ + if (handler != NULL) + log_handler = handler; } #endif /* !FIDO_NO_DIAGNOSTIC */ -- cgit v1.2.3