summaryrefslogtreecommitdiff
path: root/src/log.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/log.c')
-rw-r--r--src/log.c73
1 files changed, 51 insertions, 22 deletions
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 @@
7#include <stdarg.h> 7#include <stdarg.h>
8#include <stdio.h> 8#include <stdio.h>
9#include <stdlib.h> 9#include <stdlib.h>
10#include <string.h>
11
10#include "fido.h" 12#include "fido.h"
11 13
12#ifndef FIDO_NO_DIAGNOSTIC 14#ifndef FIDO_NO_DIAGNOSTIC
13 15
16#define XXDLEN 32
17#define XXDROW 128
18#define LINELEN 256
19
14#ifndef TLS 20#ifndef TLS
15#define TLS 21#define TLS
16#endif 22#endif
17 23
18static TLS int logging; 24static TLS int logging;
25static TLS fido_log_handler_t *log_handler;
26
27static void
28log_on_stderr(const char *str)
29{
30 fprintf(stderr, "%s", str);
31}
19 32
20void 33void
21fido_log_init(void) 34fido_log_init(void)
22{ 35{
23 logging = 1; 36 logging = 1;
37 log_handler = log_on_stderr;
24} 38}
25 39
26void 40void
27fido_log_xxd(const void *buf, size_t count) 41fido_log_debug(const char *fmt, ...)
28{ 42{
29 const uint8_t *ptr = buf; 43 char line[LINELEN];
30 size_t i; 44 va_list ap;
45 int r;
31 46
32 if (!logging) 47 if (!logging || log_handler == NULL)
33 return; 48 return;
34 49
35 fprintf(stderr, " "); 50 va_start(ap, fmt);
36 51 r = vsnprintf(line, sizeof(line) - 1, fmt, ap);
37 for (i = 0; i < count; i++) { 52 va_end(ap);
38 fprintf(stderr, "%02x ", *ptr++); 53 if (r < 0 || (size_t)r >= sizeof(line) - 1)
39 if ((i + 1) % 16 == 0 && i + 1 < count) 54 return;
40 fprintf(stderr, "\n "); 55 strlcat(line, "\n", sizeof(line));
41 } 56 log_handler(line);
42
43 fprintf(stderr, "\n");
44 fflush(stderr);
45} 57}
46 58
47void 59void
48fido_log_debug(const char *fmt, ...) 60fido_log_xxd(const void *buf, size_t count)
49{ 61{
50 va_list ap; 62 const uint8_t *ptr = buf;
63 char row[XXDROW];
64 char xxd[XXDLEN];
51 65
52 if (!logging) 66 if (!logging || log_handler == NULL || count == 0)
53 return; 67 return;
54 68
55 va_start(ap, fmt); 69 *row = '\0';
56 vfprintf(stderr, fmt, ap);
57 va_end(ap);
58 70
59 fprintf(stderr, "\n"); 71 for (size_t i = 0; i < count; i++) {
60 fflush(stderr); 72 *xxd = '\0';
73 if (i % 16 == 0)
74 snprintf(xxd, sizeof(xxd), "%04zu: %02x", i, *ptr++);
75 else
76 snprintf(xxd, sizeof(xxd), " %02x", *ptr++);
77 strlcat(row, xxd, sizeof(row));
78 if (i % 16 == 15 || i == count - 1) {
79 fido_log_debug("%s", row);
80 *row = '\0';
81 }
82 }
83}
84
85void
86fido_set_log_handler(fido_log_handler_t *handler)
87{
88 if (handler != NULL)
89 log_handler = handler;
61} 90}
62 91
63#endif /* !FIDO_NO_DIAGNOSTIC */ 92#endif /* !FIDO_NO_DIAGNOSTIC */